Home
Manage Your Code
Snippet: network socket client (C#)
Title: network socket client Language: C#
Description: This is a client using typical a socket connection. Also look at the "network socket server" snippet. Views: 122
Author: Alexandru Ionescu Date Added: 1/27/2008
Copy Code  
1try {
2			TcpClient tcpclnt = new TcpClient();
3			Console.WriteLine("Connecting.....");
4			
5			tcpclnt.Connect("192.168.0.2",8001); // use the ipaddress as in the server program

6			
7			Console.WriteLine("Connected");
8			Console.Write("Enter the string to be transmitted : ");
9			
10			String str=Console.ReadLine();
11			Stream stm = tcpclnt.GetStream();
12						
13			ASCIIEncoding asen= new ASCIIEncoding();
14			byte[] ba=asen.GetBytes(str);
15			Console.WriteLine("Transmitting.....");
16			
17			stm.Write(ba,0,ba.Length);
18			
19			byte[] bb=new byte[100];
20			int k=stm.Read(bb,0,100);
21			
22			for (int i=0;i<k;i++)
23				Console.Write(Convert.ToChar(bb[i]));
24			
25			tcpclnt.Close();
26}
27		
28	catch (Exception e) 
29        {
30			Console.WriteLine("Error..... " + e.StackTrace);
31        }
32
Notes
Also look at the "network socket server" snippet.