C# 网络问题
我正在尝试创建一个程序,允许用户上传他们写入在线服务器的某些第三方代码的 JAR 文件,然后接收返回的字符串消息作为响应。
在线服务器使用 Java 和标准 TCP 套接字网络进行编码。客户端使用了一个附加软件,这意味着我必须使用 C# 作为文件上传器。我编写的代码包含在下面。在此程序中,文件上传器工作正常,但由于某种原因,客户端在到达 input.ReadLine() 时挂起,它应该从服务器接收字符串消息响应。
public static string sendFile(string filepath) {
String response = "";
// Get the file
Stream fileStream = File.OpenRead(filepath);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
try {
// Create the connection
TCPClient client = new TCPClient("127.0.0.1", 21000);
NetworkStream stream = client.GetStream();
// Send the file to the server
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
// Receive a single string response from the server
if (stream.CanRead) {
StreamReader input = new StreamReader(stream);
inputString = input.ReadLine();
}
input.Close();
stream.Close();
client.Close();
}
catch (IOException e) {
print ("Error: " + e);
}
// Return the response message string
return inputString;
}
我还尝试使用 StreamWriter 实现上面的代码,而不是直接从 NetworkStream 本身编写。不幸的是,StreamWriter 类没有发送字节数组的方法(只有字符数组)。我想知道问题是否是由我直接调用 NetworkStream 的 Write 方法而不是使用 StreamWriter 引起的。
如果有人知道为什么上面的代码不起作用,请告诉我。或者,如果您有不同的解决方案,允许我使用相同的 TCPClient 连接发送文件(字节数组)并接收回字符串消息,那么也请随意提及。
问候,
米达维。
I'm trying creating a program that allows users to upload JAR Files for some third-party code they've written to an online server and then receive a String message back in response.
The online server is coded using Java with standard TCP Socket networking. The client uses a piece of additional software, which means I have to use C# for the File uploader. The code I've written is included below. In this program the File uploader works fine, but for some reason the client hangs when it reaches input.ReadLine() where it is supposed to receive the String message response from the server.
public static string sendFile(string filepath) {
String response = "";
// Get the file
Stream fileStream = File.OpenRead(filepath);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
try {
// Create the connection
TCPClient client = new TCPClient("127.0.0.1", 21000);
NetworkStream stream = client.GetStream();
// Send the file to the server
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
// Receive a single string response from the server
if (stream.CanRead) {
StreamReader input = new StreamReader(stream);
inputString = input.ReadLine();
}
input.Close();
stream.Close();
client.Close();
}
catch (IOException e) {
print ("Error: " + e);
}
// Return the response message string
return inputString;
}
I have also tried implementing the code above using a StreamWriter rather than writing directly from the NetworkStream itself. Unfortunately the StreamWriter class doesn't have a method for sending a byte array, (only a char array). I'm wondering whether the problem is being caused by the fact I'm calling the Write method of the NetworkStream directly rather than using a StreamWriter.
If anybody has any idea why the code above isn't working then please let me know. Alternatively, if you have a different solution that would allow me to send a file (byte array) and receive back a string message using the same TCPClient connection then please also feel free to mention it.
Regards,
Midavi.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
readline 挂起,因为它只有在从服务器成功读取一行时才会返回,这是使用阻塞套接字的缺点。请确保您的服务器准确发送一行(以“\n”结尾的字符串
The readline hangs because it will only return when it has successfully read a line from the server, this is the disadvantage of using blocking sockets. Please make sure your server is accually sending a line( string ending with "\n"
您的流是否以行尾结束?
Readline 将阻塞,直到流结束或接收到行结束符。如果您的上传者没有终止字符串,它可能会像您所说的那样。
Is your stream terminated with an end of line?
Readline will block until the stream ends or receives the end of line character. If your uploader doesn't terminate the string it could act like you're saying.
查看 networkComms.net,这是一个开源 C# 网络通信库。 此处展示了最基本的功能的简短示例不太复杂!您可能遇到的大多数问题都已得到解决,这可能会节省您一些时间。
Checkout networkComms.net, an open source C# network communication library. A short example demonstrating the most basic functionality here, hopefully not overly complex! Most of the problems you might come across will already have been solved and it might save you some time.
Readline 等待行尾\r\n,但在java中默认情况下仅行尾\n。这可能是阻止您的通信的差异,但为了确保您需要监听网络活动,或者您可以使用不同的方法来读取数据。
您可以尝试使用
read
方法:Readline
wait for end of line \r\n but in java the end of line is by default \n only. This can be the difference that can block your comunication, but to be sure you need to snoop the network activity or you can use a different method to read data.You can try to use the
read
method: