使用C#中的TCPClient从特定端口发送TCP请求?

时间:2019-07-01 17:36:51

标签: c# tcp client-server

我能够发送TCP请求,但是我的服务器要求每个请求都来自同一台计算机和同一端口。 我需要能够从特定的客户端端口发送TCP请求!

//---create a TCPClient object at the IP and port no.---
TcpClient client = new TcpClient(SERVER_IP, PORT_NO);

NetworkStream nwStream = client.GetStream();
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(textToSend);

//---send the text---
Console.WriteLine("Sending : " + textToSend);
nwStream.Write(bytesToSend, 0, bytesToSend.Length);

//---read back the text---
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));

0 个答案:

没有答案