java - Strange behavior in TCP transmission -


my server-client application consists of following modules:

client (written in java) connects server , sends file;

server (written in c) receives file , sends char array message.

the problem after receiving file, server unable send message or client unable receive it.

here code server application:

int main(int argc , char *argv[]) {     wsadata wsa;     socket s , new_socket;     struct sockaddr_in server , client;     int c, bytecount, nr_transf, rest_byte, i, bytesread;     int recv_size, file_size;     char message[1000];          char buffer[1000];     int buffer_len = 1000;     file *f = fopen("out.jpg", "wb");      printf("\ninitialising winsock...");     if (wsastartup(makeword(2, 2), &wsa) != 0)     {         printf("failed. error code : %d", wsagetlasterror());         return 1;     }      printf("initialised.\n");      //create socket     if((s = socket(af_inet, sock_stream, 0 )) == invalid_socket)     {         printf("could not create socket : %d" , wsagetlasterror());         getch();         return 0;     }      printf("socket created.\n");      //prepare sockaddr_in structure     server.sin_family = af_inet;     server.sin_addr.s_addr = inaddr_any;     server.sin_port = htons(8888);      //bind     if(bind(s, (struct sockaddr*)&server, sizeof(server)) == socket_error)     {         printf("bind failed error code : %d" , wsagetlasterror());         getch();         return 0;     }      puts("bind done");      //listen incoming connections     listen(s, 3);      //accept , incoming connection     printf("waiting incoming connections...");      c = sizeof(struct sockaddr_in);     new_socket = accept(s, (struct sockaddr*)&client, &c);     if (new_socket == invalid_socket)   {         printf("accept failed error code : %d", wsagetlasterror());         getch();         return 0;     }      printf("connection accepted");      //receive image     while((bytesread = recv(new_socket, buffer, buffer_len, 0)) > 0)     {         fwrite(buffer, 1, bytesread, f);     }      fclose(f);     printf("\nreceive finished!");       //send messsage     char my_message[100];     strcpy(my_message, "hello world!");      send(new_socket, my_message, strlen(my_message), 0);      closesocket(s);     wsacleanup();      getch();     return 0; } 

and code client application:

public class myclass {     public static void main(string[] args)      {         string filename = "1.jpg", receivemessage;         file a_file = new file(filename);           int j;         outputstream output = null;         inputstream input = null;         objectinputstream in = null;         socket socket = null;          try           {                        // create socket             socket = new socket("192.168.0.122", 8888);              fileinputstream fileinputstream = new fileinputstream(filename);             byte[] buffer = new byte[1000];             int bytesread = 0;             output = socket.getoutputstream();                  input = socket.getinputstream();                   while((bytesread = fileinputstream.read(buffer))>0)             {                 output.write(buffer,0,bytesread);             }              fileinputstream.close();                     system.out.println("send finished!");              input.read(buffer);             system.out.println("receive finished!");         }           catch(exception e)           {               e.printstacktrace();           }                  {             try             {                 in.close();                 output.close();                 socket.close();             }             catch(exception e)             {              }         }     } } 

any ideas solve problem? in advance!

it's because have blocking sockets. when socket created (by connecting or accepting) in blocking mode. means if there no data receive not return, blocking caller.

so after received last byte in loop in server, recv call block indefinitely.

on windows winsockets, use ioctlsocket function make socket blocking or non-blocking. linked reference have example shows hot make socket blocking or non-blocking.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -