Thread: socket send

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    socket send

    I got a little problem with a server / client, using TCP.

    If I start my server, then start my client, all is good.
    My client and server finds out if they "Loose connection", by checking returnvalue of recv().

    But some of the functionality in my client use send() only, before any recv().
    Meaning if I start my server, start my client, close the server, then my client will do send() to a closed connection.... And then I get segmentationFault.

    How to avoid this?

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    What OS, and coudl you [post the relevant code?

  3. #3
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    OS is Redhat Linux.

    The code failing:
    Code:
    void fail(void) {
      if(!(client->isConnected)) {
        fprintf(stderr,"Error, you are not connected to server\n");
        return;
      }
    
      safeSend(client->serverSock, "test", strlen("test")+1, client);
      safeSend(client->serverSock, "test", strlen("test")+1, client);
    }
      
    int safeSend(int incomingSocket, char *data, int  size, struct ClientInfo *client) {
      if(client->isConnected == 0) {
        fprintf(stderr,"RETURN -1\n");
        return -1;
      }
    
      int pos = 0;
      ssize_t status = 0;
      while(pos != size) {
        fprintf(stderr, "SEND IN FUNC\n");
        status = send(incomingSocket, &(data[pos]), size-pos, 0);
        fprintf(stderr, "END SEND IN FUNC\n");
        pos+=status;
        if(status == -1) {
          
          perror("send");
          return status;
        }
      }
      fprintf(stderr,"Return 0!\n");
      return 0; // All is well
    }
    What happens if I try to run fail() with an allready closed connection, is this:

    Code:
    SAFESEND:
    SEND IN FUNC
    END SEND IN FUNC
    Return 0!
    SEND IN FUNC
    And there it just stops. And by stop, I mean that the program terminates.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    you need to handle the return value before moving on to the rest of the code. I'm not sure on linux, but on windows you check the return value of send() and if it is SOCKET_ERROR (-1) you call WSAGetLastError() to get the actual cause of the error. In any case, once an error has occurred the socket is usually no longer valid.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    So you set isConnected to false only when recv(2) returns 0? You should also set this when send(2) fails with EPIPE. Read man page.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Run this on your socket:
    Code:
    int set = 1;
    setsockopt(incomingSocket, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(set));
    This will change the socket so it doesn't send the SIGPIPE signal when you attempt to write to a broken socket.

    Another option is to install a signal handler for SIGPIPE. This way you wouldn't need to call setsockopt() on every socket.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    On the opengroup-page for the recv() function, they specify that if it reads 0, it means the connection is lost.

    But about send(), all it says is this:

    Successful completion of a call to send() does not guarantee delivery of the message. A return value of -1 indicates only locally-detected errors.
    Upon successful completion, send() shall return the number of bytes sent. Otherwise, -1 shall be returned and errno set to indicate the error.

    Does this mean only recv() can neatly detect if the client has closed the connection?
    I've seen people who check if send() return 0, in the same way as recv(), but it doesent work for me, and I can't find anything about that on the opengroup-pages :/

  8. #8
    Registered User
    Join Date
    Feb 2009
    Location
    India, Gujarat
    Posts
    22
    can u put both client and server code?

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    POSIX also writes:

    Quote Originally Posted by Open Group
    [EPIPE]
    The socket is shut down for writing, or the socket is connection-mode and is no longer connected. In the latter case, and if the socket is of type SOCK_STREAM, the SIGPIPE signal is generated to the calling thread.
    At least BSD supports MSG_NOSIGNAL in flags which tells send not to raise SIGPIPE.

    If send(2) returns 0, it sent zero bytes if it conforms to standards. Check your system's man documentation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Send
    By anne_kleyheeg in forum C Programming
    Replies: 3
    Last Post: 08-13-2009, 02:20 PM
  2. Socket Send Help
    By cloudy in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-13-2007, 04:17 PM
  3. Socket or send() problem?
    By Achy in forum C Programming
    Replies: 5
    Last Post: 06-09-2006, 01:09 PM
  4. send zip file via socket
    By WaterNut in forum C Programming
    Replies: 11
    Last Post: 05-24-2005, 11:49 AM
  5. CString conversion for socket send()
    By WaterNut in forum C++ Programming
    Replies: 13
    Last Post: 04-27-2005, 04:55 PM