我有一个表单控制台应用程序,它为发送和接收执行 TCP 套接字连接。我需要帮助才能获得对 STRING 的以下回复。以下代码确实将响应写入控制台,但我也想
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("RECEIVED :"));
我想得到类似的东西
String GETSTRING;
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));
GETSTRING = *WHATEVER RESPONSE RECEIVED ABOVE*
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("RECEIVED :"));
==================================================== =====
第 2 部分 我怎样才能让它总是“等待”来自另一个客户端的响应,并且不会向控制台发送垃圾邮件?
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
TcpListener myList = new TcpListener(ipAd, 8001);
myList.Start();
Console.WriteLine("The local End point is :" +
myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
while(***something here***){
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("RECEIVED :"));
}