473,480 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

need help in multi-threaded client server communication

4 New Member
hello,,now i m working on multi-threaded client server socket program in which there is one server and multiple clients.........these both are just giving acknowledge to each other that server started and client 1 started ,client 2 started but these programs are not communicating with each other,,means i want proper communication between both,,what should i do????also i want to know how to do this using udp protocol?

server program is

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Threading;
  3. using System.Net.Sockets;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. TcpListener serverSocket = new TcpListener(8888);
  13. TcpClient clientSocket = default(TcpClient);
  14. int counter = 0;
  15.  
  16. serverSocket.Start();
  17. Console.WriteLine(" >> " + "Server Started");
  18.  
  19. counter = 0;
  20. while (true)
  21. {
  22. counter += 1;
  23. clientSocket = serverSocket.AcceptTcpClient();
  24. Console.WriteLine(" >> " + "Client No:" + Convert.ToString(counter) + " started!");
  25. handleClinet client = new handleClinet();
  26. client.startClient(clientSocket, Convert.ToString(counter));
  27. }
  28.  
  29. clientSocket.Close();
  30. serverSocket.Stop();
  31. Console.WriteLine(" >> " + "exit");
  32. Console.ReadLine();
  33. }
  34. }
  35.  
  36. //Class to handle each client request separatly
  37. public class handleClinet
  38. {
  39. TcpClient clientSocket;
  40. string clNo;
  41. public void startClient(TcpClient inClientSocket, string clineNo)
  42. {
  43. this.clientSocket = inClientSocket;
  44. this.clNo = clineNo;
  45. Thread ctThread = new Thread(doChat);
  46. ctThread.Start();
  47. }
  48. private void doChat()
  49. {
  50. int requestCount = 0;
  51. byte[] bytesFrom = new byte[10025];
  52. string dataFromClient = null;
  53. Byte[] sendBytes = null;
  54. string serverResponse = null;
  55. string rCount = null;
  56. requestCount = 0;
  57.  
  58. while ((true))
  59. {
  60. try
  61. {
  62. requestCount = requestCount + 1;
  63. NetworkStream networkStream = clientSocket.GetStream();
  64. networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
  65. dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
  66. dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
  67. Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient);
  68.  
  69. rCount = Convert.ToString(requestCount);
  70. serverResponse = "Server to clinet(" + clNo + ") " + rCount;
  71. sendBytes = Encoding.ASCII.GetBytes(serverResponse);
  72. networkStream.Write(sendBytes, 0, sendBytes.Length);
  73. networkStream.Flush();
  74. Console.WriteLine(" >> " + serverResponse);
  75. }
  76. catch (Exception ex)
  77. {
  78. Console.WriteLine(" >> " + ex.ToString());
  79. }
  80. }
  81. }
  82. }
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89. and client program is,
  90.  
  91. using System;
  92. using System.Windows.Forms;
  93. using System.Net.Sockets;
  94. using System.Text;
  95.  
  96. namespace WindowsApplication1
  97. {
  98. public partial class Form1 : Form
  99. {
  100. System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
  101. NetworkStream serverStream;
  102.  
  103. public Form1()
  104. {
  105. InitializeComponent();
  106. }
  107.  
  108. private void Form1_Load(object sender, EventArgs e)
  109. {
  110. msg("Client Started");
  111. clientSocket.Connect("127.0.0.1", 8888);
  112. label1.Text = "Client Socket Program - Server Connected ...";
  113. }
  114.  
  115. private void button1_Click(object sender, EventArgs e)
  116. {
  117. NetworkStream serverStream = clientSocket.GetStream();
  118. byte[] outStream = System.Text.Encoding.ASCII.GetBytes("Message from Client$");
  119. serverStream.Write(outStream, 0, outStream.Length);
  120. serverStream.Flush();
  121.  
  122. byte[] inStream = new byte[10025];
  123. serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
  124. string returndata = System.Text.Encoding.ASCII.GetString(inStream);
  125. msg("Data from Server : " + returndata);
  126. }
  127.  
  128. public void msg(string mesg)
  129. {
  130. textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;
  131. }
  132. }
  133. }
Jul 2 '10 #1
0 1287

Sign in to post your reply or Sign up for a free account.

Similar topics

8
4925
by: Matthias Braun | last post by:
Help! Probably it is too easy, but I am not so experienced in MySQL up to now. I have two tables table1&table2 with the following columns: col1 double, col2 int(11), col3 date I want to...
0
3741
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
8131
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
872
by: Danny | last post by:
I set this up so the users can select more than one record so they can update records a bunch at a time. only one field is modified. How can I edit this table easily instead of scrolling through...
5
3230
by: dkelly925 | last post by:
Is there a way to add an If Statement to the following code so if data in a field equals "x" it will launch one report and if it equals "y" it would open another report. Anyone know how to modify...
23
5284
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
3
2094
by: ben.r.wood | last post by:
I am not entirely sure, but after scanning the web believe I need to use multi-dimensional arrays for my problem - although I have not seen any examples of what I am trying to achieve. I have a...
6
2895
by: shalinikonatam | last post by:
Hello Every One I need multi file upload progress bar with out using perl. If any one knows plz help me. Thanks in advance.
5
3511
by: George Maicovschi | last post by:
As multi-threading is not built in PHP I've been using a hack letting the Apache server handle the multi-threading issues, but I'm really curious of other approaches to this issue. If anyone has...
7
4779
by: usr123 | last post by:
Hi, I need a multi select combo box on ASP page. Scenario: on page there is a table with few columns. In multi select box, i need all the table columns,let user select a few, and show only...
0
7044
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7045
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2985
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.