0

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

}
testermaster
  • 1,031
  • 6
  • 21
  • 40
  • Add a newline character `'\n'` after `dd`, or `fprintf(stderr, "dd");` instead. – timrau Oct 07 '14 at 15:27
  • The second example produces output probably only because the program is short lived and output buffers are flushed when the program exits. Otherwise, it would have the same behaviour (try adding a `sleep(30)` statement after the last `close` to see for yourself). – isedev Oct 07 '14 at 15:37
  • 1
    You may want to build a program that does not exhibit undefined behaviour by selecting a valid entry point. `void main()` is not one of them. – nvoigt Oct 07 '14 at 15:38
  • You will find it much easier if you 1) pay attention to the return value from listen(), 2) make each of the printf("dd") put out unique values, so you know where they are originating. 3) there is no data being communicated, so no indication of the socket worked. 4) skip the while loops for the accept and connect, because if it fails once, it will always fail. – user3629249 Oct 08 '14 at 03:13

3 Answers3

5

The output from printf will be buffered.

To get the output immediately, you have two options:

  • Add a newline (\n) to the end of the string (printf("dd\n");). This implicitly flushes the output stream (i.e. writes out the buffered content).

  • Explicitly flush the standard output stream after the printf statement (fflush(stdout);).

As for the second part of the question, the second example produces output probably only because the program is short lived and output buffers are flushed when the program exits. Otherwise, it would have the same behaviour (try adding a sleep(30) statement after the last close to see for yourself).

isedev
  • 18,848
  • 3
  • 60
  • 59
  • Thanks for the reply. Could you explain the difference between the first piece of code and the second one I posted in the first message? Why in the second one there isn't any problem? – testermaster Oct 07 '14 at 15:35
  • See comment to the question. – isedev Oct 07 '14 at 15:38
3

You need to flush your output.

Easiest way:

printf("dd\n");

Better way:

printf("dd");
fflush(stdout);
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • Why would you say using `fflush` is better? Seems to me the newline way would be just fine if you want a newline in your output. – Fred Larson Oct 07 '14 at 15:35
  • @FredLarson: As far as I am aware, putting a `\n` in your output is not _guaranteed_ to flush the output, but typically does flush it. If you want _guaranteed_ behavior, use `fflush`. If you want something that will _likely_ work, use `\n`. – abelenky Oct 07 '14 at 15:46
  • I see. Thank you. I found more information here: http://stackoverflow.com/q/5229096/10077 – Fred Larson Oct 07 '14 at 15:50
2

You need to flush your output stream. Use fflush(stdout).

Murtaza Zaidi
  • 599
  • 3
  • 14