using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace reqiqiu_server{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } Thread threadWatch = null; //负责监听客户端的线程 Socket socketWatch = null; //负责监听客户端的套接字 //创建一个负责和客户端通信的套接字 List<Socket> socConnections = new List<Socket>(); List<Thread> dictThread = new List<Thread>(); private void button1_Click(object sender, EventArgs e) { socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //定义一个套接字用于监听客户端发来的信息 包含3个参数(IP4寻址协议,流式连接,TCP协议) IPAddress ipaddress = IPAddress.Parse(textBox1.Text.Trim()); //服务端发送信息 需要1个IP地址和端口号,获取文本框输入的IP地址 IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(textBox2.Text.Trim())); //将IP地址和端口号绑定到网络节点endpoint上,获取文本框上输入的端口号 socketWatch.Bind(endpoint); //监听绑定的网络节点 socketWatch.Listen(20); //将套接字的监听队列长度限制为20 threadWatch = new Thread(WatchConnecting); //创建一个监听线程 threadWatch.IsBackground = true; //将窗体线程设置为与后台同步 threadWatch.Start(); //启动线程 textBox4.AppendText("开始监听客户端传来的信息!" "\r\n"); //启动线程后 txtMsg文本框显示相应提示 } private void WatchConnecting() { while (true) //持续不断监听客户端发来的请求 { Socket socConnection = socketWatch.Accept(); textBox4.AppendText("客户端连接成功" "\r\n"); //创建一个通信线程 ParameterizedThreadStart pts = new ParameterizedThreadStart(ServerRecMsg); Thread thr = new Thread(pts); thr.IsBackground = true; socConnections.Add(socConnection); //启动线程 thr.Start(socConnection); dictThread.Add(thr); } } private void ServerRecMsg(object obj) { throw new NotImplementedException(); } private void button2_Click(object sender, EventArgs e) { //调用 ServerSendMsg方法 发送信息到客户端 ServerSendMsg(textBox3.Text.Trim()); } /// <summary> /// 发送信息到客户端的方法 /// </summary> /// <param name="sendMsg">发送的字符串信息</param> private void ServerSendMsg(string sendMsg) { //将输入的字符串转换成 机器可以识别的字节数组 byte[] arrSendMsg = Encoding.UTF8.GetBytes(sendMsg); //向客户端发送字节数组信息 foreach (Socket socConnection in socConnections) { socConnection.Send(arrSendMsg); } //将发送的字符串信息附加到文本框txtMsg上 textBox4.AppendText("So-flash:" GetCurrentTime() "\r\n" sendMsg "\r\n"); //} } private string GetCurrentTime() { throw new NotImplementedException(); } }}
评论