1

I am writing a program for a LAN chat in C. For a chat application we need to receive the clints chat as soon as he sends it. But the problem is that i used the gets() function to input the chat from one side and while the gets is running, if a chat comes from the other end, it won't be received till I send a chat to the other end. Here is the code I used for sending and receiving the chat in the server side. I have removed all the conditions that i used because they all proved worthless. Please tell me an way to display the chat from the other end as soon as it is send, regardless of which statement is currently executed. Thanks in advance. Here is my present code of the sending and receiving part:

while(1)
{
     printf("\n");
     gets(send_data);
     send(connected, send_data,strlen(send_data), 0);
     bytes_recieved = recv(connected,recv_data,1024,0);
     recv_data[bytes_recieved] = '\0';
     printf("Client Says: %s " , recv_data);
     fflush(stdout);
}
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
Harikrishnan
  • 7,765
  • 13
  • 62
  • 113
  • 4
    http://en.wikipedia.org/wiki/Asynchronous_I/O – Cubic May 14 '12 at 10:36
  • 2
    Are you seriously using `gets`? o_O – jamesdlin May 14 '12 at 10:37
  • 2
    As, @jamesdlin is hinting at, `gets` is essentially deprecated (because it is extremely insecure and can easily cause buffer overrun bugs), one should use `fgets` or similar. More info: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1049157810&id=1043284351 – huon May 14 '12 at 10:40
  • There is no function in standard C called `gets`, it was removed in C11. Use `fgets` instead. – Lundin May 14 '12 at 11:20

1 Answers1

5

Instead of gets you will need to use non-blocking I/O or use threads to enable parallel computation and listening to moret han one client at the same time

Here's an introductory page to non-blocking I/O

Attila
  • 28,265
  • 3
  • 46
  • 55
  • I went through those links but didn't get a clear cut idea of the non-blocking I/O. Can you please give me the code of one such I/O operation and please tell me how i can implement it in the above code. Thank you. – Harikrishnan May 14 '12 at 11:03
  • It seems I slightly misunderstood your question (the link is about non-blocking network I/O). Anyway, reading the console (user input) in a non-blocking way is platform dependent (you can check this [SO thread](http://stackoverflow.com/questions/448944/c-non-blocking-keyboard-input) out for some ideas). – Attila May 14 '12 at 11:17
  • I am a beginner to this field, so I am not able to get what you are saying. Can you please make it a bit more simple. – Harikrishnan May 14 '12 at 11:31
  • What platform are you on (e.g. Windows, Linux)? – Attila May 14 '12 at 12:21
  • You cannot do this out-of-the-box without some more in-depth knowledge of signals. I would recommend instead to try using the curses or sfio libraries (see the link in my comments and follow the appropriate links to read more about how to use these libraries). Another possibility is to lear more about threading under linux [here is a cursory overview](http://www.comptechdoc.org/os/linux/programming/c/linux_pgcthreads.html) -- you can search for the individual structures/functions for more detail – Attila May 14 '12 at 14:39