2

I've been trying to receive data on a server socket from a client with the following code,

server.c

int startServer(uint16_t myPort)
{
  int listenFd = 0, connfd = 0;
  struct sockaddr_in serv_addr;

  listenFd = socket(AF_INET, SOCK_STREAM, 6);
  if (listenFd == -1) {
    perror("socket");
    return EXIT_FAILURE;
  }

  memset(&serv_addr, '0', sizeof(serv_addr));

  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(myPort);

  if (-1 == bind(listenFd, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) {
    perror("bind");
    return EXIT_FAILURE;
  }

  if (-1 == listen(listenFd, 10)) {
    perror("listen");
    return EXIT_FAILURE;
  }

  printf("Server listening on %d\n", myPort);

  int n = 0, k;
  char recvBuff[1024];
  struct sockaddr_in conn_addr;
  unsigned int len = sizeof(conn_addr);

  while(1)
  {
     connfd = accept(listenFd, (struct sockaddr*)NULL, NULL);

     n = recv(connfd, &recvBuff, 1023, 0);
     printf("Recd: %d bytes\n",n);
     for (k=0; k<16; ++k) { printf("%d: 0x%02X, ", k, recvBuff[k]); }

     sleep(1);
   }
}

and in client.c

int main(int argc, char *argv[])
{
    int sockfd = 0, n = 0;
    struct sockaddr_in serv_addr; 

    if(argc != 2)
    {
        printf("\n Usage: %s <ip of server> \n",argv[0]);
        return 1;
    } 

    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Error : Could not create socket \n");
        return 1;
    } 

    memset(&serv_addr, '0', sizeof(serv_addr)); 

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(5000); 

    if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
    {
        printf("\n inet_pton error occured\n");
        return 1;
    } 

    if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
       printf("\n Error : Connect Failed \n");
       return 1;
    } 

    uint32_t dataVal = 0xa0b0c0d0;
    int res = write(sockfd, (void*)&dataVal, sizeof(dataVal));
    printf("%d bytes sent\n", res);
    close(sockfd);
    return 0;
}

However with this I see the server saying it received the bytes but the line after that printing the bytes (in a for loop) doesn't ever seem to be executed. Does anyone know what I am doing wrong here?

8
  • 2
    does adding printf("\n"); after the for loop (or adding "\n" to the printf inside the for loop) help? Commented Oct 17, 2012 at 6:52
  • 1
    Have you tried to use a debugger? Commented Oct 17, 2012 at 6:53
  • 2
    The memset call is wrong: memset(&serv_addr, '0', sizeof(serv_addr)); set the memory to the character '0', i.e. the value 48. Commented Oct 17, 2012 at 7:07
  • 5
    Remember that output to stdout is buffered. Printing a newline flushes the buffers which does the actual printing. Commented Oct 17, 2012 at 7:08
  • 1
    and after you are finished with connfd on the server side, you should close() it... Commented Oct 17, 2012 at 7:20

1 Answer 1

3

Add printf("\n"); after the for loop, or "\n" to the printf inside the for loop.

printf writes to stdout, which is buffered. This means that output will not display until the buffer is filled, flushed, or a newline is sent. As such, it is easiest to ensure output by sending a newline.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.