C#即时通讯(局域网QQ)
一:服务器端:
private void Form1_Load(object sender, EventArgs e){Control.CheckForIllegalCrossThreadCalls = false;}private void button1_Click(object sender, EventArgs e){//ip地址IPAddress ip = IPAddress.Parse(textBox1.Text);// IPAddress ip = IPAddress.Any;//端口号IPEndPoint point = new IPEndPoint(ip, int.Parse(textBox2.Text));//创建监听用的Socket//使用IPv4地址,流式socket方式,tcp协议传递数据Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建好socket后,必须告诉socket绑定的IP地址和端口号。//让socket监听pointtry{//socket监听哪个端口socket.Bind(point);//同一个时间点过来10个客户端,排队socket.Listen(10);ShowMsg("服务器开始监听");Thread thread = new Thread(AcceptInfo);thread.IsBackground = true;thread.Start(socket);}catch (Exception ex){ShowMsg(ex.Message);}}//记录通信用的SocketDictionary<string, Socket> dic = new Dictionary<string, Socket>();// private Socket client;void AcceptInfo(object o){Socket socket = o as Socket;while (true){//通信用sockettry{//创建通信用的SocketSocket tSocket = socket.Accept();string point = tSocket.RemoteEndPoint.ToString();ShowMsg(point + "连接成功!");comboBox1.Items.Add(point);dic.Add(point, tSocket);//接收消息Thread th = new Thread(ReceiveMsg);th.IsBackground = true;th.Start(tSocket);}catch (Exception ex){ShowMsg(ex.Message);break;}}}void ShowMsg(string msg){textBox3.AppendText(msg + "\r\n");}//接收消息void ReceiveMsg(object o){Socket client = o as Socket;while (true){//接收客户端发送过来的数据try{//定义byte数组存放从客户端接收过来的数据byte[] buffer = new byte[1024 * 1024];//将接收过来的数据放到buffer中,并返回实际接受数据的长度int n = client.Receive(buffer);//将字节转换成字符串string words = Encoding.UTF8.GetString(buffer, 0, n);ShowMsg(client.RemoteEndPoint.ToString() + ":" + words);}catch (Exception ex){ShowMsg(ex.Message);break;}}}private void button2_Click(object sender, EventArgs e){try{ShowMsg(textBox4.Text);string ip = comboBox1.Text;byte[] buffer = Encoding.UTF8.GetBytes(textBox4.Text);dic[ip].Send(buffer);// client.Send(buffer);}catch (Exception ex){ShowMsg(ex.Message);}}
二:客户端
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);private void button2_Click(object sender, EventArgs e){//客户端给服务器发消息if (client != null){try{ShowMsg(textBox4.Text);byte[] buffer = Encoding.UTF8.GetBytes(textBox4.Text);client.Send(buffer);}catch (Exception ex){ShowMsg(ex.Message);}}}private void button1_Click(object sender, EventArgs e){//连接到的目标IPIPAddress ip = IPAddress.Parse(textBox1.Text);//IPAddress ip = IPAddress.Any;//连接到目标IP的哪个应用(端口号!)IPEndPoint point = new IPEndPoint(ip, int.Parse(textBox2.Text));try{//连接到服务器client.Connect(point);ShowMsg("连接成功");ShowMsg("服务器" + client.RemoteEndPoint.ToString());ShowMsg("客户端:" + client.LocalEndPoint.ToString());//连接成功后,就可以接收服务器发送的信息了Thread th = new Thread(ReceiveMsg);th.IsBackground = true;th.Start();}catch (Exception ex){ShowMsg(ex.Message);}}void ShowMsg(string msg){textBox3.AppendText(msg + "\r\n");}//接收服务器的消息void ReceiveMsg(){while (true){try{byte[] buffer = new byte[1024 * 1024];int n = client.Receive(buffer);string s = Encoding.UTF8.GetString(buffer, 0, n);ShowMsg(client.RemoteEndPoint.ToString() + ":" + s);}catch (Exception ex){ShowMsg(ex.Message);break;}}}private void Form1_Load(object sender, EventArgs e){Control.CheckForIllegalCrossThreadCalls = false;}
参考自:https://www.cnblogs.com/asdyzh/p/9839775.html