Alex Rivera | Logout

Socket Programming C/C++ - recv function hangs?

Asked 2009-12-11T12:11:09.903
10

My recv function hangs while getting reponse from server.

Client side code in c/c++:

void sockStuff() {

 int sock, bytes_recieved,bytes_send;
 char send_data[1024], recv_data[4096];

 struct hostent *host;
 struct sockaddr_in server_addr;

 host = gethostbyname("127.0.0.1");

 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  perror("SocketError");
  exit(1);
 }
 server_addr.sin_family = AF_INET;
 server_addr.sin_port = htons(50500);
 server_addr.sin_addr = *((struct in_addr *) host->h_addr);
 bzero(&(server_addr.sin_zero), 8);


 if (connect(sock, (struct sockaddr *) &server_addr, sizeof(struct sockaddr))
   == -1) {
  perror("ConnectToError");
  exit(1);
 }


    bytes_send = send(sock, a, strlen(a), 0);
    bytes_send = shutdown(sock, 1);


 bytes_recieved = recv(sock, recv_data, 4096, 0); //Where the program hangs??
 recv_data[bytes_recieved] = '\0';
 printf("\nRecieved data = %s ", recv_data);
 cout << endl << endl;

 shutdown(sock,2);



}

My Client is C/C++ and server side is OpenEdge Progress. Please see code and suggest what went wrong with this.??

Edit
Report

1 Answer

1

Try using the the Non blocking mode. i.e. recv will return if no data is there to be read, so handle this case (in a while loop or with select. that is up to you)

For more on recv, assuming you are on a POSIX compliant system) read this http://www.opengroup.org/onlinepubs/009695399/functions/recv.html

answered 2009-12-11T21:39:50.967

Your Answer