button_click下的代码如下
try
{
int port = Convert.ToInt32(txtPort.Text.Trim());
string host = txtIPaddress.Text.Trim();
IPAddress ip = IPAddress.Parse(host);
//IPAddress ipp = new IPAddress("127.0.0.1");
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建Socket
c.Connect(ipe);//连接到服务器
pictureBox1.Visible = true;
pictureBox2.Visible = false;
string sendStr = "PM01";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
c.Send(bs, bs.Length, 0); string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
txtRec.Text = recvStr + Environment.NewLine;
c.Close();
}
catch (ArgumentNullException ex)
{
Console.WriteLine("argumentNullException: {0}", ex);
}
catch (SocketException ex)
{
Console.WriteLine("SocketException:{0}", ex);
}
Console.WriteLine("Press Enter to Exit");
}
点击button后会接受服务器端的数据,随后服务器端发数据,就接受不到了,当再次点击button时才会接收数据,为什么???请高手指导
try
{
int port = Convert.ToInt32(txtPort.Text.Trim());
string host = txtIPaddress.Text.Trim();
IPAddress ip = IPAddress.Parse(host);
//IPAddress ipp = new IPAddress("127.0.0.1");
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建Socket
c.Connect(ipe);//连接到服务器
pictureBox1.Visible = true;
pictureBox2.Visible = false;
string sendStr = "PM01";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
c.Send(bs, bs.Length, 0); string recvStr = "";
byte[] recvBytes = new byte[1024];
int bytes;
bytes = c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
txtRec.Text = recvStr + Environment.NewLine;
c.Close();
}
catch (ArgumentNullException ex)
{
Console.WriteLine("argumentNullException: {0}", ex);
}
catch (SocketException ex)
{
Console.WriteLine("SocketException:{0}", ex);
}
Console.WriteLine("Press Enter to Exit");
}
点击button后会接受服务器端的数据,随后服务器端发数据,就接受不到了,当再次点击button时才会接收数据,为什么???请高手指导
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货