I'm creating a server in C on Ubuntu, but I've a problem with a printf function which doesn't work. This is the code. I'd like that the terminal prints "dd" as soon as the program starts, but it doesn't do anything. Suggestions?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <netdb.h> void main(){ printf("dd"); int ds_sock; struct sockaddr_in my_addr; ds_sock=socket(AF_INET,SOCK_STREAM,0); memset(&my_addr,0,sizeof(my_addr)); my_addr.sin_family=AF_INET; my_addr.sin_port=htons(25000); my_addr.sin_addr.s_addr=INADDR_ANY; bind(ds_sock,(struct sockaddr *)&my_addr,sizeof(my_addr)); listen(ds_sock,3); printf("dd"); int ds_sock_acc; struct sockaddr_in addr; size_t sin_size = sizeof(struct sockaddr_in); ds_sock_acc = accept(ds_sock,(struct sockaddr *)&addr,&sin_size); while(ds_sock_acc==-1) { printf("connessione non riuscita"); ds_sock_acc = accept(ds_sock,(struct sockaddr *)&addr,&sin_size); } printf("connessione riuscita"); close(ds_sock); close(ds_sock_acc); } this (client) works as expected:
void main(){ printf("dd"); int ds_sock; ds_sock = socket(AF_INET, SOCK_STREAM,0); int ret; struct sockaddr_in Eaddr; Eaddr.sin_family = AF_INET; Eaddr.sin_port = htons(25000); Eaddr.sin_addr.s_addr=inet_addr("127.0.0.1"); ret = connect(ds_sock,(struct sockaddr *)&Eaddr,sizeof(Eaddr)); while(ret==-1){ printf("Errore nel connect"); ret = connect(ds_sock,(struct sockaddr *)&Eaddr,sizeof(Eaddr)); } printf("connect OK"); close(ds_sock); }
'\n'afterdd, orfprintf(stderr, "dd");instead.sleep(30)statement after the lastcloseto see for yourself).void main()is not one of them.