服务端:
namespace WindowsFormsApplication2
{ public partial class Form1 : Form {List<CocketSend> CocketSendArr = new List<CocketSend>();
public Form1()
{ InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; }private void button1_Click(object sender, EventArgs e)
{ //当点击的时候穿件一个负责IP地址监听的socket Socket socketWatch = new Socket(SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Any; //创建端口号 IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(this.textBox1.Text)); socketWatch.Bind(point); Show("监听成功");socketWatch.Listen(10);
Thread rh = new Thread(new ParameterizedThreadStart(ListenClientConnect));
rh.Start(socketWatch); }/// <summary>
/// 等待客户端链接并且创建与之通信的socket /// </summary> private void ListenClientConnect(object o) { //负责和客户端通信的socket Socket socketWatch = o as Socket; while (true) {//等待客户端链接 ,建立负责通信的socket
Socket socektSend = socketWatch.Accept(); //将通信的socket保存到集合中 CocketSend csModel = new CocketSend(); csModel.RemoteEndPoint = socektSend.RemoteEndPoint.ToString(); csModel.socektSend = socektSend; CocketSendArr.Add(csModel); ComboBoxBind(); Show(socektSend.RemoteEndPoint.ToString() + " 链接成功");Thread th = new Thread(new ParameterizedThreadStart(Recive));
th.Start(socektSend); }}
//服务器端不停的接收来的消息 void Recive(object o) { Socket socektSend = o as Socket; while (true) { try { //客户端链接成功后。服务器接收来自客户端的消息 byte[] buffer = new byte[1024 * 1024 * 2]; int lR = socektSend.Receive(buffer); if (lR == 0) //判断客户端下线 { foreach (var item in CocketSendArr) { if (item.RemoteEndPoint == socektSend.RemoteEndPoint.ToString()) { CocketSendArr.Remove(item); } } ComboBoxBind(); break; } string str = Encoding.UTF8.GetString(buffer, 0, lR); Show(string.Format("{0}:{1}\r\n", socektSend.RemoteEndPoint.ToString(), str)); } catch { } } } public void Show(string str) { textBox2.AppendText(string.Format("{0}\r\n", str)); ; }private void button2_Click(object sender, EventArgs e)
{string strCode = this.comboBox1.SelectedItem.ToString();
Socket sModel = null;
foreach (var item in CocketSendArr) { if (item.RemoteEndPoint == strCode) { sModel = item.socektSend; break; } } if (sModel != null) { string str = textBox3.Text; byte[] buffer = Encoding.UTF8.GetBytes(str); sModel.Send(buffer); } }public void ComboBoxBind()
{ this.comboBox1.Items.Clear(); foreach (var item in CocketSendArr) { this.comboBox1.Items.Add(item.RemoteEndPoint); } } public class CocketSend { public string RemoteEndPoint { get; set; } public Socket socektSend { get; set; } } }}
客户端:
namespace WindowsFormsApplication_c
{ public partial class Form1 : Form { Socket socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); public Form1() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; }private void button1_Click(object sender, EventArgs e)
{string ipStr = textBox1.Text;
int pointI = Convert.ToInt32(textBox2.Text);//创建客户端的Socket
IPAddress ip = IPAddress.Parse(ipStr); IPEndPoint point = new IPEndPoint(ip, pointI); //与远程服务器建立链接 socketSend.Connect(point); textBox3.AppendText("建立链接\r\n");Thread th = new Thread(new ThreadStart(Rccive));
th.Start(); }private void button2_Click(object sender, EventArgs e)
{ string str = textBox3.Text; byte[] buffer = Encoding.UTF8.GetBytes(str); socketSend.Send(buffer); textBox3.Text = string.Empty; }/// <summary>
/// 不停接收数据 /// </summary> void Rccive() {while (true)
{ try { byte[] buffer = new byte[1024 * 1024 * 3]; int l = socketSend.Receive(buffer); if (l == 0) { break; }string str = Encoding.UTF8.GetString(buffer);
textBox4.AppendText(str); textBox4.AppendText("\r\n"); } catch { } } } }}