改成这样成么?
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();
//accp();
}
catch (Exception ee)
{
textBox3.AppendText(ee.Message);
}
}
public delegate void MyInvoke();
private void accp()
{
MyInvoke mi = new MyInvoke(showServerMessage);
BeginInvoke(mi);
}
public void showServerMessage()
{
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");
}
}
}
还是有错误啊!
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();
//accp();
}
catch (Exception ee)
{
textBox3.AppendText(ee.Message);
}
}
public delegate void MyInvoke();
private void accp()
{
MyInvoke mi = new MyInvoke(showServerMessage);
BeginInvoke(mi);
}
public void showServerMessage()
{
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");
}
}
}
还是有错误啊!