标签:
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.Threading; using System.Net.Sockets; using System.Net; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Socket client; private void button2_Click(object sender, EventArgs e) { client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(this.ip.Text); IPEndPoint prot = new IPEndPoint(ip, Convert.ToInt32(this.port.Text)); client.Connect(prot); //服务器端口 listin("连接成功"); Thread th = new Thread(sends); th.Start(); } public void listin(string msg) { this.textBox2.AppendText(msg + "\r\n"); } void sends() { while (true) { Byte[] buf = new Byte[1024 * 1024 * 3]; int result = client.Receive(buf); if (result == 0) { break; } string str = Encoding.UTF8.GetString(buf, 0, result); listin(client.RemoteEndPoint.ToString() + ":" + str); } } private void Form1_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; } private void button1_Click(object sender, EventArgs e) { string str = this.textBox1.Text.Trim(); Byte[] buf = System.Text.Encoding.UTF8.GetBytes(str); client.Send(buf); } } }
标签:
原文地址:http://www.cnblogs.com/mengluo/p/5648809.html