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"); } } 
3
  • 1
    For a start, put \n at the end of your printf strings. You may not be seeing them because they're buffered. Commented Feb 20, 2013 at 3: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). Commented Feb 20, 2013 at 3: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). Commented Feb 20, 2013 at 3:46

2 Answers 2

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).

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

6 Comments

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.
scanf, etc, which uses stdin, is tied to stdout, so that prompts don't get garbled.
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.)
See stackoverflow.com/q/2123528/318716, Does reading from stdin flush stdout? But the answers there seem to disagree with my comment.
does a \n always guarantee a flush?
|
0

You are not flushing stdout.

Alternatively, use stderr as in

fprintf(stderr,"My message\n"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.