0

I'm implementing Client/Server File send and receive.

Implementation:Client in C and Server in Java.

Part of the C code being sent:

long count;
FILE *file;
char *file_data;

file=fopen("test.txt","rb");
fseek (file , 0 , SEEK_END);
count = ftell (file);
rewind (file);

file_data=(char*)malloc(sizeof(char)*count);
fread(file_data,1,count+1,file);
fclose(file);

if ((numbytes = send(sockfd, file_data, strlen(file_data)+1 , 0)) == -1) 
{
 perror("client: send");
 exit(1);
}

Part of Java code receiving:

public String receiveFile() 
{
   String fileName="";
   try 
   {
    int bytesRead;
    InputStream in = clientSocket.getInputStream();
    DataInputStream clientData = new DataInputStream(in);
    fileName=clientData.readUTF();
   }
} 

After readUTF() function is used, the server hangs up or is in infinite loop and doesnt proceed further. I've tried BufferedReader with readLine(). There is an error that "no suitable constructor found for BufferedReader(InputStream) & readLine() gives a warning. Any other alternative besides BufferedReader??

4

1 回答 1

1

readUTF()读取由writeUTF(). 您没有发送它,因此服务器无法读取它。使用read(byte[]),readFully(),new BufferedReader(newInputStreamReader(in));

如果您使用readLine(),则需要发送换行符。在任何一种情况下,您都不需要发送尾随的空值。

于 2013-11-07T02:07:30.780 回答