/*服务端*/
1 using System; 2 using System.Collections.Generic; 3 using System.Collections; 4 using System.Collections.Specialized; 5 using System.Linq; 6 using System.Text; 7 using System.Net.Sockets; 8 using System.Net; 9 using System.Threading; 10 namespace ChatSever 11 { class Sever 12 { 13 static void Main(string[] args) 14 { 15 Hashtable clientTable = new Hashtable();//存放连接的转发表 16 IPAddress ip = Dns.GetHostAddresses(Dns.GetHostName())[0];//返回主机的ip地址 17 int port=9999; 18 int maxsize=1024; 19 TcpListener listener = new TcpListener(ip, port); 20 listener.Start(); 21 Console.WriteLine("服务器已启动,正在监听....../n"); 22 Console.WriteLine(string.Format("服务器IP:{0}/t端口号:{1}/n",ip,port)); 23 while (true) 24 { 25 byte[] packetBuff=new byte[maxsize]; 26 Socket newClient = listener.AcceptSocket(); 27 newClient.Receive(packetBuff); 28 string userName = Encoding.Unicode.GetString(packetBuff).TrimEnd('/0'); 29 if (clientTable.Count != 0 && clientTable.ContainsKey(userName))//验证是否为唯一用户 30 { 31 newClient.Send(Encoding.Unicode.GetBytes("Failed")); 32 continue; 33 } 34 else 35 { 36 newClient.Send(Encoding.Unicode.GetBytes("Successful")); 37 38 } 39 //将新的连接加入转发表并创建线程为其服务 40 clientTable.Add(userName,newClient); 41 string strlog = string.Format("[系统消息]用户{0}在{1}连接.... 当前在线人数:{2}/r/n/r/n",userName ,DateTime.Now ,clientTable.Count); 42 Console.WriteLine(strlog); 43 Thread thread = new Thread(new ParameterizedThreadStart(Sever.ThreadFunc)); 44 thread.Start(userName); 45 //向所有客户端发送系统消息 46 foreach (DictionaryEntry de in clientTable) 47 { 48 string clientName = de.Key as string; 49 Socket clientSkt = de.Value as Socket; 50 if (!clientName.Equals(userName)) 51 { 52 clientSkt.Send(Encoding.Unicode.GetBytes(strlog)); 53 } 54 } 55 } 56 } 57 58 static void ThreadFunc(object obj) 59 { 60 //代码----启动新的线程监听来自客户端的信息 61 62 } 63 } 64 }
/*客户端:*/
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Net; 6 using System.Net.Sockets; 7 namespace CustomProgram 8 { 9 class Custom 10 { 11 static void Main(string[] args) 12 { 13 byte [] data=new byte[1024]; 14 Socket newClient = new Socket(AddressFamily.InterNetwork ,SocketType.Stream,ProtocolType.Tcp); 15 Console.WriteLine("请输入服务器的地址:"); 16 string ipadd = Console.ReadLine(); 17 Console.WriteLine("请输入服务的端口号:"); 18 int port = Convert.ToInt32(Console.ReadLine()); 19 IPEndPoint ipend = new IPEndPoint(IPAddress .Parse(ipadd) ,port); 20 try 21 { 22 newClient.Connect(ipend);//连接服务器; 23 } 24 catch(SocketException e) 25 { 26 Console.WriteLine("连接服务器失败!"); 27 Console.WriteLine(e.Message); 28 return; 29 } 30 int rec = newClient.Receive(data); 31 string stringdata = Encoding.ASCII.GetString(data,0,rec); 32 Console.WriteLine(stringdata); 33 while (true) 34 { 35 string input = Console.ReadLine(); 36 if (input.ToUpper() == "EXIT") 37 break; 38 newClient.Send(Encoding.ASCII .GetBytes(input)); 39 data =new byte[1024]; 40 rec = newClient.Receive(data); 41 stringdata = Encoding.ASCII.GetString(data,0,rec); 42 Console.WriteLine("{0}来自服务器消息:{1}",DateTime.Now,stringdata); 43 } 44 Console.WriteLine("断开连接!"); 45 newClient.Shutdown(SocketShutdown.Both); 46 newClient.Close(); 47 } 48 } 49 }
一上完成之后,右击项目解决方案的属性把项目设为多启动,同时运行客户端与服务端即可;
希望这个实例能对刚学C#网络编程的人有所帮助!