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"); } }
\nat the end of yourprintfstrings. You may not be seeing them because they're buffered.printf()statements do not end with a newline; their output won't appear until after you output a newline, or usefflush(stdout)(or print so much data on a single line that it no longer fits in the buffer in theFILE *forstdout).printf(3)output normally is buffered, it is only written out when the buffer (largeish) fills. You can force output withfflush(stdout).