标签:param 接收 gets com 用户 根据 star odi user
服务器端代码:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Net.Sockets; 8 using System.Net; 9 using System.Text; 10 using System.Threading.Tasks; 11 using System.Windows.Forms; 12 using System.Threading; 13 using System.IO; 14 15 namespace socket通信 16 { 17 public partial class SERVER : Form 18 { 19 public SERVER() 20 { 21 InitializeComponent(); 22 } 23 Socket socketSend; 24 private void Form1_Load(object sender, EventArgs e) 25 { 26 this.ActiveControl = this.btnListen; 27 txtaidip.Text = GetIpAdress(); 28 //txtport.Text = "80"; 29 Control.CheckForIllegalCrossThreadCalls = false; 30 } 31 //将远程连接的IP地址和Socket存放到集合中 32 Dictionary<string, Socket> diSocket = new Dictionary<string, Socket>(); 33 private void button1_Click(object sender, EventArgs e) 34 { 35 try 36 { 37 //创建一个socket,负责监听IP地址和端口号 38 Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 39 IPAddress ip = IPAddress.Parse(txtaidip.Text.Trim()); //提供一个IP地址 40 IPEndPoint point = new IPEndPoint(ip, 80); //端口号,其中包括了IP地址 41 42 43 socketWatch.Bind(point); //绑定IP地址和端口 44 ShowMsg("监听成功"); 45 socketWatch.Listen(10); //设置监听队列,最多允许同时10个连接,(0表示不限制) 46 47 Thread th = new Thread(Listen); 48 th.IsBackground = true; 49 th.Start(socketWatch); //socketWatch是Listen()的参数 50 } 51 catch 52 { 53 54 } 55 56 57 58 } 59 60 private void ShowMsg(string str) 61 { 62 txtReceMsg.AppendText(str + "\r\n"); 63 } 64 65 /// <summary> 66 /// //获取本机IP地址 67 /// </summary> 68 /// <returns></returns> 69 private string GetIpAdress() 70 { 71 try 72 { 73 string HostName = Dns.GetHostName(); //得到主机名 74 IPHostEntry IpEntry = Dns.GetHostEntry(HostName); 75 for (int i = 0; i < IpEntry.AddressList.Length; i++) 76 { 77 //从IP地址列表中筛选出IPv4类型的IP地址 78 //AddressFamily.InterNetwork表示此IP为IPv4, 79 //AddressFamily.InterNetworkV6表示此地址为IPv6类型 80 if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork) 81 { 82 return IpEntry.AddressList[i].ToString(); 83 } 84 } 85 return ""; 86 } 87 catch (Exception ex) 88 { 89 return ex.Message; 90 } 91 } 92 93 /// <summary> 94 /// 解决只能连接一个用户的问题 95 /// </summary> 96 private void Listen(Object o) 97 { 98 Socket socketWatch = o as Socket; 99 while (true) 100 { 101 try 102 { 103 //接下来等待客户端的连接 104 socketSend = socketWatch.Accept(); //为客户端创建一个负责通信的新的socket并接收来自客户端的请求 105 //将新的socket存放进集合中 106 diSocket.Add(socketSend.RemoteEndPoint.ToString(), socketSend); 107 //将远程连接的IP地址和端口号存入下拉框 108 cboUsers.Items.Add(socketSend.RemoteEndPoint.ToString()); 109 ShowMsg(socketSend.RemoteEndPoint.ToString() + ":连接成功"); //192.168.0.103:连接成功 110 Thread th = new Thread(ReceiveSend); 111 //开启一个新线程不停接收来自客户端的消息 112 th.IsBackground = true; 113 th.Start(socketSend); 114 } 115 catch 116 { 117 118 } 119 120 121 } 122 } 123 /// <summary> 124 /// 服务器端不停的接收客户端发送过来的消息 125 /// </summary> 126 /// <param name="o"></param> 127 private void ReceiveSend(Object o) 128 { 129 Socket socketSend = o as Socket; 130 while (true) 131 { 132 try 133 { 134 //客户端连接成功后,服务器应该接受客户端发出的消息 135 byte[] buffer = new byte[1024*1024 * 1]; 136 int r = socketSend.Receive(buffer); // 实际接收到的有效字节数 137 if (r == 0) 138 { 139 break; 140 } 141 string str = Encoding.UTF8.GetString(buffer, 0, r); //将字节流转换为string【str就是客户端向服务器发送的字符串】 142 ShowMsg(socketSend.RemoteEndPoint.ToString() + ":" + str); //远程客户端连接:接收客户端发送的内容 143 144 } 145 catch 146 { 147 148 } 149 } 150 } 151 152 private void btnSendMsg_Click(object sender, EventArgs e) 153 { //将服务器端的信息发送给客户端 154 string str = txtSendMsg.Text.Trim(); 155 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str); 156 //socketSend.Send(buffer); 157 //获得用户在下拉框中选中的IP地址,根据IP地址找到对应的socket,然后发送消息 158 string ip = cboUsers.SelectedIndex.ToString(); 159 diSocket[ip].Send(buffer); 160 txtReceMsg.AppendText(GetIpAdress() + ">>" + txtSendMsg.Text.Trim() + "\r\n"); 161 txtSendMsg.Text = ""; //清空文本框 162 } 163 164 //在文本框中输入信息后按【Enter】键,触发【发送消息】事件 165 private void txtSendMsg_KeyPress(object sender, KeyPressEventArgs e) 166 { 167 if (e.KeyChar == ‘\r‘) 168 { 169 btnSendMsg_Click(sender, e); 170 } 171 } 172 } 173 }
客户端代码:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.Net.Sockets; 11 using System.Net; 12 using System.Threading; 13 14 namespace socket通信客户端 15 { 16 public partial class CLIENT : Form 17 { 18 public CLIENT() 19 { 20 InitializeComponent(); 21 } 22 23 Socket socketSend; 24 /// <summary> 25 /// //获取本机IP地址 26 /// </summary> 27 /// <returns></returns> 28 private string GetIpAdress() 29 { 30 try 31 { 32 string HostName = Dns.GetHostName(); //得到主机名 33 IPHostEntry IpEntry = Dns.GetHostEntry(HostName); 34 for (int i = 0; i < IpEntry.AddressList.Length; i++) 35 { 36 //从IP地址列表中筛选出IPv4类型的IP地址 37 //AddressFamily.InterNetwork表示此IP为IPv4, 38 //AddressFamily.InterNetworkV6表示此地址为IPv6类型 39 if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork) 40 { 41 return IpEntry.AddressList[i].ToString(); 42 } 43 } 44 return ""; 45 } 46 catch (Exception ex) 47 { 48 return ex.Message; 49 } 50 } 51 52 private void Form1_Load(object sender, EventArgs e) 53 { 54 //txtlocalip.Text = GetIpAdress(); 55 //txtport.Text = "8080"; 56 Control.CheckForIllegalCrossThreadCalls = false; 57 this.ActiveControl = this.btnConnection; 58 59 } 60 61 private void btnConnection_Click(object sender, EventArgs e) 62 { 63 try 64 { 65 //创建一个socket用来连接服务器 66 socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 67 IPAddress ip = IPAddress.Parse(txtaidip.Text.Trim()); //获取IP地址 68 IPEndPoint ipep = new IPEndPoint(ip, Convert.ToInt32(txtport.Text.Trim())); 69 70 //获得要连接的远程服务器的IP地址和端口号 71 socketSend.Connect(ipep); 72 showMSG("连接成功"); 73 //创建一个线程,用来不断接收服务器端发送的信息 74 Thread th = new Thread(ReceiveMessage); 75 th.IsBackground = true; //后台线程 76 th.Start(socketSend); //socketSend是Start()的参数 77 } 78 catch 79 { 80 81 } 82 83 } 84 85 private void showMSG(string str) 86 { 87 txtReceMsg.AppendText(str + "\r\n"); 88 } 89 90 private void btnSendMsg_Click(object sender, EventArgs e) 91 { 92 string str = txtSendMsg.Text.Trim(); 93 byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str); 94 socketSend.Send(buffer); 95 txtReceMsg.AppendText(GetIpAdress() + ">>" + txtSendMsg.Text.Trim() + "\r\n"); 96 txtSendMsg.Text = ""; 97 } 98 99 private void txtMsg_KeyPress(object sender, KeyPressEventArgs e) 100 { 101 if (e.KeyChar == ‘\r‘) 102 { 103 this.btnSendMsg_Click(sender, e); 104 } 105 } 106 /// <summary> 107 /// 不停地接收服务器端发送来的信息 108 /// </summary> 109 /// <param name="o"></param> 110 private void ReceiveMessage(Object o) 111 { 112 Socket socketSend = o as Socket; 113 try 114 { 115 while (true) 116 { 117 byte[] buffer = new byte[1024 * 1024 * 1]; 118 int r = socketSend.Receive(buffer); 119 if (r == 0) 120 { 121 break; 122 } 123 string str = Encoding.UTF8.GetString(buffer, 0, r); 124 showMSG(socketSend.RemoteEndPoint.ToString() + ":" + str); 125 126 } 127 } 128 catch 129 { 130 131 } 132 133 134 } 135 } 136 137 138 }
标签:param 接收 gets com 用户 根据 star odi user
原文地址:https://www.cnblogs.com/decoct-tea/p/12375172.html