Socket简单的接收和发送问题,请高手答!
接收:
一开始我监听:
private void Form1_Load(object sender, EventArgs e)
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(getip()), 10);
socket.Bind(ipe);
socket.Listen(10);
isStart = true;
Thread thread = new Thread(new ThreadStart(get));
thread.Start();
}
我新开了一个线程,不断地接收数据
private void get()
{
byte[] buff = new byte[1024];
int len = 0;
while (isStart)
{
Thread.Sleep(200);
len = socket.Available;
if (len <= 0)
{
continue;
}
socket.Receive(buff, len, SocketFlags.None);
string date = Encoding.Default.GetString(buff, 0, len);
this.richTextBox1.AppendText(date);
}
}
发送:
private void button2_Click(object sender, EventArgs e)
{
string s = "hehe,我来啦! ";