1

I am a student in a network programming class and I am writing a UDP client that connects to three servers to do a file transfer. So far, Ive gotten the client to communicate with the first 2 servers but this 3rd copy of the server is not working. Its not even printing out the printf debug statements I put in. What is going on? Is there something wrong with Unix? The first two servers are exactly the same as this one with changes in port/ip address and they work perfectly fine.

#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <strings.h>

int main(int argc, char**argv)
{
   printf("i will destroy you");
   int o = 1;
   int sockfd,n;
   struct sockaddr_in servaddr,cliaddr;
   socklen_t len;
   char mesg[1000];

   printf("im confused");
   sockfd=socket(AF_INET,SOCK_DGRAM,0);

   bzero(&servaddr,sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
   servaddr.sin_port=htons(3454);
   bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
   printf("yo wtf");

   if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &o, sizeof(o)) < 0){
      perror("setsockopt failed");
      exit(0);
   }
   printf("here1");
   while(1)
   {
      len = sizeof(cliaddr);
      printf("here");
      n = recvfrom(sockfd,mesg,1000,0,(struct sockaddr *)&cliaddr,&len);
      sendto(sockfd,mesg,n,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
      printf("-------------------------------------------------------\n");
      mesg[n] = 0;
      printf("Received the following:\n");
      printf("%s",mesg);
      printf("-------------------------------------------------------\n");
   }
}
James Le
  • 49
  • 6
  • 1
    For a start, put `\n` at the end of your `printf` strings. You may not be seeing them because they're buffered. – paxdiablo Feb 20 '13 at 03:45
  • If you want output messages to appear, make sure they end with a newline. Your first two `printf()` statements do not end with a newline; their output won't appear until after you output a newline, or use `fflush(stdout)` (or print so much data on a single line that it no longer fits in the buffer in the `FILE *` for `stdout`). – Jonathan Leffler Feb 20 '13 at 03:45
  • 1
    `printf(3)` output normally is buffered, it is only written out when the buffer (largeish) fills. You can force output with `fflush(stdout)`. – vonbrand Feb 20 '13 at 03:46

2 Answers2

1

You probably need to change e.g. printf("i will destroy you") to printf("i will destroy you\n"). Or see setbuf(). Or use fflush(stdout).

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
  • it does print now. why is the newline character needed? sometimes in my other programs i leave it out but it still shows up somewhere before the prompt in the terminal. – James Le Feb 20 '13 at 03:49
  • `scanf`, etc, which uses `stdin`, is tied to `stdout`, so that prompts don't get garbled. – Joseph Quinsey Feb 20 '13 at 03:52
  • One minor point: the line `mesg[n] = 0;` is not quite right. `n` could possibly be `1000`, and so the nul is put past the end of the array. (Or, of course, `n` could be -1, if you wished to check for errors.) – Joseph Quinsey Feb 20 '13 at 03:54
  • See http://stackoverflow.com/q/2123528/318716, Does reading from stdin flush stdout? But the answers there seem to disagree with my comment. – Joseph Quinsey Feb 20 '13 at 03:59
  • 1
    does a `\n` always guarantee a flush? – phoeagon Feb 20 '13 at 05:31
  • @phoeagon: Yes (and no). Yes, for `stdout`, ***if*** `stdout` "refers to a terminal", as per `man setlinebuf`. Otherwise, for shell piping or redirection, you need to call `setlinebuf(stdout)` explicitly (at least on my system). Ditto for other streams, except `stderr` which is unbuffered by default. – Joseph Quinsey Feb 20 '13 at 14:38
0

You are not flushing stdout.

Alternatively, use stderr as in

fprintf(stderr,"My message\n");
Doug Currie
  • 40,708
  • 1
  • 95
  • 119