2024-11-23 08:30:06
关闭socket.Close()后,socket对象就为null,再次打开socket时会发生异常。稍微修改一下:
private void tcpStart_Click(object sender, EventArgs e)
{
//IPAddress ipAddr = IPAddress.Parse("172.24.54.19");
IPAddress ipAddr = IPAddress.Parse(IP);
IPEndPoint ipEnd = new IPEndPoint(ipAddr, 9603);
//创建socket实例!加上这句!!!!!
socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp)
try
{
socket.Connect(ipEnd);
btnStart.Enabled = true;
MessageBox.Show("TCP连接成功!!");
}
catch
{
MessageBox.Show("TCP连接失败!!");
}
}