使用相同的网络流和套接字连接从C#客户端向Java服务器发送多字节[]时挂起
本文关键字:服务器 Java 多字节 挂起 客户端 网络 连接 套接字 | 更新日期: 2023-09-27 18:25:48
我编写了一个文件上传器,旨在将一个程序代码文件从C#客户端发送到Java服务器。服务器要求在接受文件上载之前提交有效的用户名和密码。
独立地,安全性(用户名和密码)和文件上传器都能正常工作。然而,当我尝试将两者结合时,在从服务器收到"true"布尔响应(指示正确的用户名和密码)后,代码在C#客户端上冻结。附带了来自客户端和服务器的相关代码。
C#客户端
public static string sendValidatedFile(string username, string password, string path) {
string inputString = "NotSent";
try {
TcpClient client = new TcpClient("127.0.0.1", 42000);
StreamReader input = new StreamReader(stream);
Stream securityStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(username + "_" + password + "_'n"));
byte[] bufferA = new byte[securityStream.Length];
securityStream.Read(bufferA, 0, bufferA.Length);
stream.Write(bufferA, 0, bufferA.Length);
stream.Flush();
inputString = input.ReadToEnd();
bool result = bool.Parse(inputString);
if (result) {
print("Login Accepted");
Stream fileStream = File.OpenRead(path);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
// This is where the code seems to lock up
stream.Write(buffer, 0, buffer.Length);
stream.Flush();
inputString = input.ReadToEnd();
}
else {
inputString = "Invalid Username or Password";
}
input.Close();
stream.Close();
client.Close();
}
catch (SocketException e) {
print("Error" + e);
}
catch (IOException e) {
print("Error" + e);
}
return inputString;
}
Java服务器
public void run() {
try {
// Get username and password
byte[] byteArrayJAR = new byte[filesize];
byte[] byteArraySecurity = new byte[filesize];
InputStream input = socket.getInputStream();
PrintWriter output = new PrintWriter(socket.getOutputStream());
int bytesSecurity = input.read(byteArraySecurity, 0, byteArraySecurity.length);
int currentByte1 = bytesSecurity;
if (input.available() > 0) {
do {
bytesSecurity = input.read(
byteArraySecurity,
currentByte1,
(byteArraySecurity.length - currentByte1));
if (bytesSecurity >= 0) {
currentByte1 += bytesSecurity;
}
}
while (bytesSecurity > -1);
}
String securityString = new String(byteArraySecurity).trim();
String[] authenticationString = securityString.split("_");
String username = authenticationString[0];
String password = authenticationString[1];
// Validate the username and password with a stored database
if (security.validateUser(username, password)) {
// Inforn the client their username and password were accepted
output.println(true);
output.flush();
// Get the program code file
int bytesRead = input.read(byteArrayJAR, 0, byteArrayJAR.length);
int currentByte = bytesRead;
if (input.available() > 0) {
do {
bytesRead = input.read(
byteArrayJAR,
currentByte,
(byteArrayJAR.length - currentByte));
if (bytesRead >= 0) {
currentByte += bytesRead;
}
}
while (bytesRead > -1);
}
// Inform the client that the code was received
output.println("Success");
output.flush();
}
else {
// Inform the client their username or password was incorrect
output.println(false);
output.flush();
}
// Disconnect from client
output.flush();
output.close();
input.close();
socket.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
有人能看到上面的任何错误,可能导致我的代码挂起吗?C#客户端在两个实例上都有传输的数据。在C#中,两个字节数组不能从同一个网络流发送是有原因的吗?
我很感激任何人能提供任何帮助,让上面的代码在不挂起的情况下工作。
问候,
Midavi。
您正在使用输入。ReadToEnd()两次。溪流只有一端;我怀疑第一个不合适,应该用更直接的读取来代替——或者充其量是ReadLine()。
如果这里的对话依赖于客户端和服务器之间的多条消息,您可能还想检查nagle是否已启用,因为可能其中一条消息仍处于缓冲状态,并且尚未实际发送(Flush()对网络流不起任何作用)-您可能必须为套接字设置NoDelay(或等效值)。默认行为是缓冲小消息,以防止大量小数据包充斥网络。