UDP发送消息的问题
各位大侠,小弟刚学c#不久,编了一个小小的程序,这个窗体程序可以运行,但是点击Button1的时候却没有任何反应,甚至没有响应,变成了一个死程序。请问,我这里出现了什么问题呢?由于不会上传图片:窗体是由2个TextBox控件和一个Button控件组成,在TextBox2上输入消息,在TextBox1上接受消息!希望众大侠多多帮忙!!感谢不尽!!
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.Net;
using System.Net.Sockets;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int length;
string str;
byte[] bytes=new byte[1024];
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint myHost = new IPEndPoint(IPAddress.Any, 3050);
socket.Bind(myHost);
IPEndPoint remote = new IPEndPoint(IPAddress.Parse("196.128.0.102"), 3050);
socket.Connect(remote);
bytes = System.Text.Encoding.Unicode.GetBytes(TextBox2.Text);
socket.Send(bytes);
txtMain1.Text = TextBox1.Text + "\n" + System.DateTime.Now.ToString() + "\n" + "你说:" + TextBox2.Text;
while (true)
{
length = socket.Receive(bytes);
str = System.Text.Encoding.Unicode.GetString(bytes, 0, length);
TextBox1.Text = TextBox1.Text + str+"\n";
str = TextBox2.Text;
bytes = System.Text.Encoding.Unicode.GetBytes(str);
socket.Send(bytes);
}
}
}
}