2

I write a simple application that provide connection between two computers in C. I have a problem with simultaneously reading and writing in socket. I am able to check if user press any key by getch(), but I don't know how to check if it something in socket buffer. When I use read() function it wait until be something in socket. I wanted to check socket buffer and then use read(), but I can't find any function/flag to check this. Maybe is different solution for this problem, perhaps use another thread to read?

perror
  • 6,737
  • 16
  • 59
  • 77
  • 4
    The `select()` function with a timeout is a way to get notification of when data arrives on a socket. – mathematician1975 May 01 '15 at 09:29
  • you need to try establishing Async socket. please check this http://stackoverflow.com/questions/2331869/what-are-async-sockets – GingerJack May 01 '15 at 09:32
  • @Mautokar: You should at least provide an example code to start with. – perror May 01 '15 at 09:38
  • You can use asynchronus I/O *or* threads *or* non-blocking mode *or* `ioctl()` with the `FIONREAD` option, depending entirely on your requirements, your application architecture, ... Too broad. – user207421 May 01 '15 at 09:41
  • @myaut Because it's equvalent to `recv()` with a zero fourth parameter. Why are you asking? – user207421 May 01 '15 at 09:41
  • I had the same problem. I sent every 100ms a word to indicate if there is something to read. For example write("NOTHING") or write("MSG") and then write the message. –  May 01 '15 at 09:57
  • 2
    @DAO What a shocking waste of bandwidth. – user207421 May 01 '15 at 09:58

1 Answers1

1

You can use sys/ioctl.h file's method ioctl:

#include <sys/ioctl.h>

...

int count;
ioctl(fd, FIONREAD, &count);

Reference credit : This Answer

Community
  • 1
  • 1
Kushal
  • 7,439
  • 7
  • 58
  • 76