我试图通过tcpClient连接发送数据来操作接收输入并执行所需操作的设备。在服务器上,数据以循环方式接收,值以数组形式设置。代码看起来像这样:
client = server.available();
//Receive Data
while(client.available()){
for(int i = 7; i<11; ++i){
String msg = client.readStringUntil('\n');
senddata[i] = msg.toInt();
msg ="";
}
我的客户端功能如下所示:
static void Connect(String server, String message)
{
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 85;
TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("\n Press Enter to continue...");
Console.Read();
}
我希望能够在关闭连接之前连续写入数据。基本上是这样的:
int[] data = new int[4];
data[0] = 1;
data[0] = 1;
data[0] = 1;
data[0] = 1;
for (int i = 0; i < data.Length; i++)
{
Connect("192.168.1.125", data[i].ToString());
}
目前,数据已发送且连接已关闭,服务器无法处理更多数据。我尝试了不同的解决方案,但没有任何运气。
修改
我尝试在循环中运行以下代码,将数据写入流:
for (int i = 0; i < 4; i++)
{
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
}
// Close everything.
stream.Close();
client.Close();
我收到以下错误:
Additional information: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.
答案 0 :(得分:0)
您当前的Connect()方法打开连接,执行一次写入/读取操作并关闭连接。你应该移动连接开放&amp;关闭该方法的代码,只留下写/读逻辑。
如果您不关闭连接,则不写入数据的原因是使用Nagle算法。有关详细信息,请参阅此answer。要解决此问题,只需将TcpClient.NoDelay设置为true,如下面的代码所示:
static void WriteData(NetworkStream stream, String message)
{
try
{
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
}
static void ClientCode()
{
int[] data = new int[4];
data[0] = 1;
data[0] = 1;
data[0] = 1;
data[0] = 1;
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 85;
TcpClient client = new TcpClient(server, port);
client.NoDelay = true;
// Get a client stream for reading and writing.
NetworkStream stream = client.GetStream();
for (int i = 0; i < data.Length; i++)
{
WriteData(stream, data[i].ToString());
}
// Close everything.
stream.Close();
client.Close();
}