Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send Problem (Socket)

Status
Not open for further replies.

kathyayini

Programmer
Aug 16, 2002
52
IN
I have written a program which will accept only 20 connections to the server port. If connection goes beyond 20 then below mentioned code will be executed which is in while loop

memset(cSendBuf,' ',23);
memset(&sd, '\0', 23);

printf("\nRejecting Excess Client %s ...\n",inet_ntoa(sockAddr.sin_addr));
strcpy(sd.cstrmsg,"Rejecting Excess Client");
strcpy(cSendBuf,"");
printf("sd.cstrmsg is ;%s\n",sd.cstrmsg);
memcpy(cSendBuf,sd.cstrmsg,23);
int x=send(sock_id,cSendBuf,23,MSG_NOSIGNAL);
printf("x is %d\n",x);
close(sock_id);
continue;

Problem is socket closes before data is sent to client, so client fails to receive data. Is there is any way to send data first and then close the socket.

Waiting for the reply.
 
I have written a program which will accept only 20 connections to the server port. If connection goes beyond 20 then below mentioned code will be executed which is in while loop

memset(cSendBuf,' ',23);
memset(&sd, '\0', 23);

printf("\nRejecting Excess Client %s ...\n",inet_ntoa(sockAddr.sin_addr));
strcpy(sd.cstrmsg,"Rejecting Excess Client");
strcpy(cSendBuf,"");
printf("sd.cstrmsg is ;%s\n",sd.cstrmsg);
memcpy(cSendBuf,sd.cstrmsg,23);
int x=send(sock_id,cSendBuf,23,MSG_NOSIGNAL);
printf("x is %d\n",x);
close(sock_id);
continue;

Problem is socket closes before data is sent to client, so client fails to receive data (even though close is after send). Is there is any way to wait till client receives data and then close the socket or other solution like that. If i put sleep(2); before close then it works fine. Using sleep is not a right way to wait.

Waiting for the reply.
 
You must be using UDP. Since UDP sockets are connectionless and inherently unreliable, this is one of the things that can happen.

Switch to TCP unless you have a good reason for using UDP.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top