英文:
Java / C socket, sending multiple variables and receiving it on the client side in differend variables
问题
这是我翻译的内容:
这是处理客户端的函数。我希望能够按照以下顺序发送并在客户端的不同变量中存储:
- 文件名
- 文件大小
- 二进制文件内容
另一方面,我在 Windows 10 上有一个接收非阻塞套接字的函数:
这个函数用于接收客户端的文件。我遇到的问题是,当我从 Java 服务器接收输入到 C 客户端时,文件名缓冲区变量中包含一些文件大小的输入,而文件大小包含一些文件内容。
我不明白如何在不使用Thread.sleep()
方法的情况下解决这个问题,等待直到我在filename_buffer
中接收到所有内容,等待套接字缓冲区清空并发送文件大小变量,等待所有内容清空,然后发送文件内容。
一个例子是这样的:
有一个文件[] = "AAAAAA";
dOut.write("send_this.jpg".getBytes());
dOut.write(String(file.length).getBytes());
dOut.write(file) //(作为字节);
客户端接收到的内容是:
filename_buffer = "send_this.jpg";
size = 6AAAAAA;
receivedFile = "AA..";
使用Thread.sleep()
时,一切都按预期工作,但我不太确定这是否是正确的做法。
英文:
So I am trying to write a program that uses some form of FTP and other things, this is only early stage and I have encountered some problems with the write method in java and the recv function in C.
I dont really know how to explain it so I attached the code and one example.
public void run() {
try {
System.out.println(getName() + " Waiting...");
synchronized (serverSocket) {
client = serverSocket.accept();
}
System.out.println("Starting Client Connetion with " + getName());
bufferedReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
dOut = new DataOutputStream(client.getOutputStream());
dOut.write(fileName.getBytes());
dOut.flush();
String idk = String.valueOf(fileArray.length);
dOut.write(idk.getBytes());
dOut.flush();
Thread.sleep(500);
dOut.write(fileArray);
dOut.flush();
System.out.println("Connection with " + getName() + " has ended");
} catch(IOException | InterruptedException e) {
System.out.println("CLIENT ERROR");
} finally {
clientMap[threadNumber] = false;
try {
client.close();
bufferedReader.close();
dOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
this is the function that handles the clients, I want to be able to send in this order and store in different variables on the client side.
- The file name
- File Size
- binary file content
on the other hand i have the receiving non-blocking socket on windows 10 :
void receiveFile(SOCKET ClientSocket) {
char filename_buffer[20];
int fileSize = 0;
unsigned char* receivedFile;
int iResult = 0;
int count = 0;
int total = 0;
////////////////////RECEIVE THE NAME OF THE FILE/////////////////////////////
memset(filename_buffer, '\0', 20);
iResult = recv(ClientSocket, filename_buffer, 20, 0);
if (iResult == -1) {
printf("Recv Name : error");
closesocket(ClientSocket);
return NULL;
}
printf("File name : %s\nName Length : %d\n", filename_buffer, strlen(filename_buffer));
////////////////////RECEIVE THE SIZE OF THE FILE/////////////////////////////
char size[10];
memset(size, '\0', 10);
iResult = recv(ClientSocket, size, 10, 0);
if (iResult == -1) {
printf("Recv size : error\n");
closesocket(ClientSocket);
return NULL;
}
fileSize = arrayToInt(size);
if (fileSize == 0) {
printf("File Size : error");
closesocket(ClientSocket);
return NULL;
}
printf("File size : %d\n", fileSize);
////////////////////RECEIVE THE CONTENT OF THE FILE/////////////////////////////
receivedFile = calloc(fileSize, sizeof(unsigned char));
while ((count = recv(ClientSocket, receivedFile, fileSize, 0)) > 0) {
total += count;
}
if (count == -1) {
printf("Recv : error\n");
closesocket(ClientSocket);
return NULL;
}
printf("File content : \n");
for (int i = 0; i < fileSize;i++) {
printf("%c", receivedFile[i]);
}
printf("\nTotal file bytes received : %d\n", total);
closesocket(ClientSocket);
writeBinaryFile(filename_buffer, receivedFile, fileSize);
printf("Successful write...\n");
}
The idea of using 2 different languages is pure didactic, I kinda love C but I need to learn a higher level language.
The problem I have is that when I receive the input from the Java-server on the C-client its all messed up the filename_buffer variable contains some of the size input in it and the size contains some of the file content.
I do not understand how to solve this without using the Thread.sleep()
method and wait until I receive everything in filename_buffer, wait for the socket buffer to clear and send the file size variable, wait for everything to clear and then send the file content.
One example is this :
having file[] = "AAAAAA";
dOut.write("send_this.jpg".getBytes());
dOut.write(String(file.length).getBytes());
dOut.write(file) // (as bytes);
// The client receives
filename_buffer = "send_this.jpg";
size = 6AAAAAA;
receivedFile = "AA..";
And using Thread.sleep everything works as expected but I am not so sure its the right way to do this.
专注分享java语言的经验与见解,让所有开发者获益!
评论