namespace WindowsApplication1
{
public partial class Form1 : Form
{
private IPAddress myIP = IPAddress.Parse("172.0.0.1");
private IPEndPoint MyServer;
private Socket sock;
private bool check = true;
private Socket accSock;
private void accp()
{
MyServer = new IPEndPoint(myIP,Int32.Parse(textBox2.Text));
sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sock.Bind(MyServer);
sock.Listen(50);
//textBox3.AppendText("主机"+textBox1.Text+"端口"+textBox2.Text+"开始监听\r\n");
accSock = sock.Accept();
if (accSock.Connected)
{
textBox3.AppendText("与客户建立连接。");
while (check)
{
Byte[] Rec = new Byte[64];
NetworkStream netStream = new NetworkStream(accSock);
netStream.Read(Rec,0,Rec.Length);
string RecMessage = System.Text.Encoding.BigEndianUnicode.GetString(Rec);
richTextBox1.AppendText(RecMessage+"\r\n");
}
}
} public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
try
{
myIP = IPAddress.Parse(textBox1.Text);
}
catch
{
MessageBox.Show("您输入的IP格式错误");
}
try
{
Thread thread = new Thread(new ThreadStart(accp));
thread.Start();
}
catch(Exception ee)
{
textBox3.AppendText(ee.Message);
}
}
}
}
《c#网络核心编程》里的同步套接字的服务器代码,我敲了几次,本以为是自己出错了,但仔细对照了,没有发现问题,,运行就报错,那位高手帮我看看是哪里的问题啊,谢谢了,,,
{
public partial class Form1 : Form
{
private IPAddress myIP = IPAddress.Parse("172.0.0.1");
private IPEndPoint MyServer;
private Socket sock;
private bool check = true;
private Socket accSock;
private void accp()
{
MyServer = new IPEndPoint(myIP,Int32.Parse(textBox2.Text));
sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sock.Bind(MyServer);
sock.Listen(50);
//textBox3.AppendText("主机"+textBox1.Text+"端口"+textBox2.Text+"开始监听\r\n");
accSock = sock.Accept();
if (accSock.Connected)
{
textBox3.AppendText("与客户建立连接。");
while (check)
{
Byte[] Rec = new Byte[64];
NetworkStream netStream = new NetworkStream(accSock);
netStream.Read(Rec,0,Rec.Length);
string RecMessage = System.Text.Encoding.BigEndianUnicode.GetString(Rec);
richTextBox1.AppendText(RecMessage+"\r\n");
}
}
} public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
try
{
myIP = IPAddress.Parse(textBox1.Text);
}
catch
{
MessageBox.Show("您输入的IP格式错误");
}
try
{
Thread thread = new Thread(new ThreadStart(accp));
thread.Start();
}
catch(Exception ee)
{
textBox3.AppendText(ee.Message);
}
}
}
}
《c#网络核心编程》里的同步套接字的服务器代码,我敲了几次,本以为是自己出错了,但仔细对照了,没有发现问题,,运行就报错,那位高手帮我看看是哪里的问题啊,谢谢了,,,
我的QQ:5556055
看http://topic.csdn.net/u/20080826/20/303a5b46-ac45-4b1d-b748-26069c7bcecb.html
线程不能够直接操作GUI的控件。1你要写一个委托
public delegate void AppendTextHandler(string text);2接下来写一个函数public void SetText(string msg)
{
try{
//使用form1的invoke方法
this.Invoke(new AppendTextHandler(_SetText),new object[]{msg});
}catch
{}
}3写实现函数
private void _SetText(string msg)
{
textBox3.AppendText(msg);
}4在线程的调用处
...
sock.Listen(50);
//textBox3.AppendText("主机"+textBox1.Text+"端口"+textBox2.Text+"开始监听\r\n");
SetText("主机"+textBox1.Text+"端口"+textBox2.Text+"开始监听\r\n");
accSock = sock.Accept();
...
我怎么没看出来会有错哩,你这样试下!