找传奇、传世资源到传世资源站!

socket实现多客户端与服务器连接通信

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

一个服务器,多客户端,客户端可以向服务器发送信息,服务器可以同时向多个客户端发送信息
socket实现多客户端与服务器连接通信 C#语言基础-第1张socket实现多客户端与服务器连接通信 C#语言基础-第2张using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.Threading;namespace TCPClient{ public partial class Form1 : Form { public Socket newclient; public bool Connected; public Thread myThread; public delegate void MyInvoke(string str); public Form1() { InitializeComponent(); } public void Connect() { byte[] data = new byte[1024]; newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string ipadd = serverIP.Text.Trim(); int port = Convert.ToInt32(serverPort.Text.Trim()); IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd), port); try { newclient.Connect(ie); connect.Enabled = false; Connected = true; } catch(SocketException e) { MessageBox.Show("连接服务器失败 " e.Message); return; } ThreadStart myThreaddelegate = new ThreadStart(ReceiveMsg); myThread = new Thread(myThreaddelegate); myThread.Start(); } public void ReceiveMsg() { while (true) { byte[] data = new byte[1024]; int recv = newclient.Receive(data); string stringdata = Encoding.UTF8.GetString(data, 0, recv); showMsg(stringdata "\r\n"); //receiveMsg.AppendText(stringdata "\r\n"); } } public void showMsg(string msg) { { //在线程里以安全方式调用控件 if (receiveMsg.InvokeRequired) { MyInvoke _myinvoke = new MyInvoke(showMsg); receiveMsg.Invoke(_myinvoke, new object[] { msg }); } else { receiveMsg.AppendText(msg); } } } private void SendMsg_Click(object sender, EventArgs e) { int m_length = mymessage.Text.Length; byte[] data=new byte[m_length]; data = Encoding.UTF8.GetBytes(mymessage.Text); int i = newclient.Send(data); showMsg("我说:" mymessage.Text "\r\n"); //receiveMsg.AppendText("我说:" mymessage.Text "\r\n"); mymessage.Text = ""; //newclient.Shutdown(SocketShutdown.Both); } private void connect_Click(object sender, EventArgs e) { Connect(); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复