Results 1 to 5 of 5

Thread: crash on write to closed socket

  1. #1
    Join Date
    Nov 2009
    Beans
    5

    Unhappy crash on write to closed socket

    OK so I was writing a program that used the socks4 protocol, and I ran into something rather annoying. If I create a TCP socket, once the remote host closes it if I try to call write() on the socket it straight up terminates my process rather than giving an error. Paraphrased portion of code:

    printf("Going to send new line...\n");
    write(fd, "\n", 1);
    printf("Sent new line.\n");

    It would print the first message, but the program would terminate before the second line was printed. Is there something I'm missing, or is write() not acting like it's supposed to?

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: crash on write to closed socket

    Your code snippet looks fine, thus you are going to need to supply more code from your application. write() should return a -1 on error. The cause of the error can be analyzed through the value of errno.

    Btw, what do you mean by sock4 protocol? Do you mean IPv4?

    Also, when sending data through sockets, write() is not the most favored function to use. Try using send() or sendto().

  3. #3
    Join Date
    Nov 2009
    Beans
    5

    Re: crash on write to closed socket

    I meant Socks4, the protocol a lot of open proxies use (I was doing a connection tunnelling experiment).

    I tried switching everything to send(), but the same problem occurred.

    I know the socket was up and running right because I was able to send data through it just fine, but once the connection's terminated on the other end any attempt to call write() or send() on the file descriptor of the socket just kills the program.

    I also tried printing errno. It was 0, all the way up until the program got stopped.

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: crash on write to closed socket

    Quote Originally Posted by BipolarJunction View Post
    I meant Socks4, the protocol a lot of open proxies use (I was doing a connection tunnelling experiment).

    I tried switching everything to send(), but the same problem occurred.

    I know the socket was up and running right because I was able to send data through it just fine, but once the connection's terminated on the other end any attempt to call write() or send() on the file descriptor of the socket just kills the program.

    I also tried printing errno. It was 0, all the way up until the program got stopped.
    What you are describing is not normal behavior for write() or for send().

    Personally, I suspect that you are corrupting the stack (or possibly the heap) data using an operation somewhere else in your code.

    P.S. Post your code... show me how you create the socket, setup the connection (if using TCP), and are sending/receiving data.

  5. #5
    Join Date
    Nov 2009
    Beans
    5

    Re: crash on write to closed socket

    Oh now there's a thought! I'll check for buffer overflows and the like.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •