-C#的socket通信TCP
1.
namespace WindowsFormsApp1
{delegate void adduserinfodel(string userinfo);public partial class Form1 : Form{public Form1(){InitializeComponent();mydel += addnf;textBox1.Text = "192.168.0.6";textBox2.Text = "666";}adduserinfodel mydel;Thread th;Socket _socket;private void button1_Click(object sender, EventArgs e){_socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);IPAddress address = IPAddress.Parse(this.textBox1.Text.Trim());IPEndPoint endPoint = new IPEndPoint(address,Convert.ToInt32(this.textBox2.Text.Trim()));try {_socket.Bind(endPoint);MessageBox.Show("创建OK");}catch(Exception ex) {MessageBox.Show("创建失败");} _socket.Listen(100);th = new Thread(listenfuntion);th.Start();}private void listenfuntion(){while (true){Socket socketclient= _socket.Accept();string info = socketclient.RemoteEndPoint.ToString();Invoke(mydel, info);Thread th = new Thread(receiveinfo);th.IsBackground = true;th.Start(socketclient);}}private void receiveinfo(object obj){Socket sckclient = obj as Socket;if (sckclient != null){byte[] arr = new byte[1024 * 1024 * 5];int len = -1;len = sckclient.Receive(arr);if (len == 0) {}else{}}}private void addnf(string a){listBox1.Items.Add(a);}}
}