program automatically closing after writing to socket

umdcoder

I have the following code:

  bytes_read = read(0, buffer_stdin, 65535);
  printf("bytes read from stdin: %d\n", bytes_read);
  bytes_written = write(sock, buffer_stdin, bytes_read);
  printf("bytes written to socket: %d\n", bytes_written); 

sock is the socket descriptor. In the code I have connected the socket to a server and the connection returned (0), indicating the connection was successful.

Now in the code above I type something into stdin, let's say "hello" and the number of bytes read would be 6 (because of null termination after letter 'o'). The program executes the first print statement.

But for some reason the program doesn't execute the second print statement. It just exits.

When I commented out line 3 where I write to the socket, the 2nd print statement executes.

My question is, why is the program exiting early without my instruction to do so?

Here is the preliminary code:

int main(int argc, char *argv[]) {
  if (argc != 3) {
    printf("Invalid arguments\n");
    exit(1);
  }

  char *serverIP = argv[1]; /*Server hostname*/
  char *portNumber = argv[2]; /*Port Number*/
  void *numericAddress;
  char buffer_stdin[65535];
  char buffer_stdout[65535];
  int bytes_read = 0;
  int bytes_written = 0;

  /*getting integral number of string representation of port number*/
  in_port_t servPort = atoi(argv[2]);

  /*------------------get binary number of hostname-----------------*/
  struct addrinfo addrCriteria;
  memset(&addrCriteria, 0, sizeof(addrCriteria));
  addrCriteria.ai_family = AF_INET;
  addrCriteria.ai_socktype = SOCK_STREAM;
  addrCriteria.ai_protocol = IPPROTO_TCP;

  struct addrinfo *addrList;

  int rtnVal = getaddrinfo(serverIP, portNumber, &addrCriteria, &addrList);
  if (rtnVal != 0) {
    printf("getaddrinfo() failed\n");
    exit(1);
  }
  if (rtnVal == 0) {
    printf("getaddrinfo() successful\n");
  }

  numericAddress = &((struct sockaddr_in *) (addrList->ai_addr))->sin_addr;
  ((struct sockaddr_in *)(addrList->ai_addr))->sin_port = htons(servPort);
  /*----------------------------------------------------------------*/

  /*Creating socket*/
  int sock = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
  if (sock < 0) {
    printf("error creating socket\n");
    exit(1);
  }

  /*Establish connection to the echo server*/
  int r = connect(sock, (struct sockaddr *) addrList, sizeof(addrList));
  if (r < 0) {
    printf("Connection failed\n");
    exit(1);
  }
  if (r == 0) {
    printf("connection succesful\n");
  }
jxh

SIGPIPE

The problem seems to be that the server closed the connection before your client had a chance to send data. Writing to a closed socket could result in a kind of error that is expressed as raising the SIGPIPE signal, unless that signal has been ignored. If the signal delivery of SIGPIPE has been suppressed (by setting its disposition to SIG_IGN), writing to a closed socket will cause an error result with errno set to EPIPE.

Since your program does not ignore SIGPIPE or otherwise hand it, the default handler is used, which would likely cause the program to terminate. This explains your immediate problem.

Privileged port 80

You indicated in comments that you used port 80 for your "echo server". Unless you are running the program as root, 80 is within the privileged port range, so your program would not be able to listen on that port. In addition, port 80 is the well known port for the HTTP service. So, even if you started the "echo server" with root privileges, it would still likely fail because some other server (the web server) would already be listening on that port.

Assuming you directed your client at the web server, it is unknown what actually happened when your client attempted communication. For example, the web server may have delivered a RST packet as soon as the client connected. The connection completed successfully, but writing would fail.

To get the behavior you expect, you should choose an unprivileged port for your "echo server". Something relatively high and random, so that it will not conflict with other programs, and will not conflict with other users if you are on a shared machine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

program automatically closing after writing to socket

From Dev

Exception after closing the program

From Dev

Closing the listening socket after a fork()

From Dev

iOS: Crash after writing socket

From Dev

Closing the webdriver instance automatically after the test failed

From Dev

Closing the webdriver instance automatically after the test failed

From Dev

Writing a java program to send emails automatically on a timer

From Dev

Python: Hold data in memory after closing program

From Dev

Keep Clipboard Info after closing Program

From Dev

A way for saving data after closing a C program

From Dev

pyinstaller program keeps running in background after closing

From Dev

My socket keeps closing after socket.connect times out

From Dev

How to Delete Program exe file after closing program

From Dev

SRWebsocket connection is closing automatically after keeping app idle for sometime in iOS

From Dev

smiley popup window is not closing automatically after sending message

From Dev

TCP connection - server only sends message after closing socket

From Dev

boost::asio + std::future - Access violation after closing socket

From Dev

Is it possible to keep a socket alive even after closing the app?

From Dev

Python: UDP echo server asyncore ; socket closing after send

From Dev

After closing the screen, android closes socket connection in few minutes

From Dev

Rebooting program after input() automatically on python

From Dev

How to automatically restart a program after it's closed

From Dev

How to run a python program in the background even after closing the terminal?

From Dev

How to run a python program in the background even after closing the terminal?

From Dev

How to stop program from closing after switch statements

From Dev

Java- starting a program again after stopping it, without closing it down

From Dev

Stop Ubuntu 20.04 from writing automatically in exponent after "^": a¹ --> a^1

From Dev

ServerSocket vs Socket -- closing the socket

From Dev

Place Picker automatically closing

Related Related

  1. 1

    program automatically closing after writing to socket

  2. 2

    Exception after closing the program

  3. 3

    Closing the listening socket after a fork()

  4. 4

    iOS: Crash after writing socket

  5. 5

    Closing the webdriver instance automatically after the test failed

  6. 6

    Closing the webdriver instance automatically after the test failed

  7. 7

    Writing a java program to send emails automatically on a timer

  8. 8

    Python: Hold data in memory after closing program

  9. 9

    Keep Clipboard Info after closing Program

  10. 10

    A way for saving data after closing a C program

  11. 11

    pyinstaller program keeps running in background after closing

  12. 12

    My socket keeps closing after socket.connect times out

  13. 13

    How to Delete Program exe file after closing program

  14. 14

    SRWebsocket connection is closing automatically after keeping app idle for sometime in iOS

  15. 15

    smiley popup window is not closing automatically after sending message

  16. 16

    TCP connection - server only sends message after closing socket

  17. 17

    boost::asio + std::future - Access violation after closing socket

  18. 18

    Is it possible to keep a socket alive even after closing the app?

  19. 19

    Python: UDP echo server asyncore ; socket closing after send

  20. 20

    After closing the screen, android closes socket connection in few minutes

  21. 21

    Rebooting program after input() automatically on python

  22. 22

    How to automatically restart a program after it's closed

  23. 23

    How to run a python program in the background even after closing the terminal?

  24. 24

    How to run a python program in the background even after closing the terminal?

  25. 25

    How to stop program from closing after switch statements

  26. 26

    Java- starting a program again after stopping it, without closing it down

  27. 27

    Stop Ubuntu 20.04 from writing automatically in exponent after "^": a¹ --> a^1

  28. 28

    ServerSocket vs Socket -- closing the socket

  29. 29

    Place Picker automatically closing

HotTag

Archive