This question already has answers here:
How to check amount of data available for a socket in C and Linux
(6 answers)
Closed 7 years ago.
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?
You can use sys/ioctl.h file's method ioctl:
#include <sys/ioctl.h>
...
int count;
ioctl(fd, FIONREAD, &count);
Reference credit : This Answer
Related
This question already has an answer here:
Are TCP SOCKET handles inheritable?
(1 answer)
Closed 1 year ago.
As far as I understand, if you create a socket on POSIX-related systems, then spawn a child which inherits that socket fd, apparently you should call close on that fd in each process when that process is done with it, as the fd is internally reference counted, and I suppose that reference count is incremented on fork, and close then decrements it again.
Is this the same with Windows, should you call closesocket in each process that has a copy of that socket, or do you only call it in one process, when all processes are done with it?
Edit: the question marked as a duplicate of this one is relevant (Are TCP SOCKET handles inheritable?), as it suggests that inheriting SOCKETs is error-prone, but it also suggests that it's still possible. In which case, if one persists regardless of the possible errors, then it would be good if this question had an answer; should one closesocket in all processes, or just the parent?
Second edit: I believe this question is answered in the documentation for WSADuplicateSocketA:
A process can call closesocket on a duplicated socket and the descriptor will become deallocated. The underlying socket, however, will remain open until closesocket is called by the last remaining descriptor.
You shouldn't see sockets on Windows as file descriptors, so they don't get inherited the same way. See this link for the full answer to your question: Are TCP SOCKET handles inheritable?
This question already has answers here:
How to view thread id of a process which has opened a socket connection?
(2 answers)
Closed 3 years ago.
I have a process with 100 threads.
I know that only one thread is using a specific fd.
For example, this fd is a socket descriptor, and only one thread is using this socket with send() and receive().
How can I find out, with C, on Linux, the ID of this thread?
Is there a smarter way than attaching to each thread with ptrace and waiting until one of them will be detected?
File descriptors are part of the process. And since a file descriptor is just a nonnegative integer, and can be used by all threads of the same processes without explicit rebinding, asking "which thread holds an fd" is not a question applicable to the Linux process/threading model.
If you really want an answer then it would be: All the threads do!
This question already has answers here:
Select function in socket programming
(5 answers)
Closed 3 years ago.
I'm setting up a server with some clients using socket programming in c.
I found that in order to have more than one client, I should use threads or select or poll.
I know how should I use these function but when I searched I found that there is a way that used from thread and select together.
I have two questions:
1) what is the reason and benefits(using select and thread together)?
2) Is thread used for clients and select for reading the socket?
You can handle more than one socket/client in one thread by using select/epoll and non-blocking I/O. You can handle even more sockets/clients if you have multiple threads doing that.
Old but still relevant read The C10K problem.
This question already has answers here:
What is the Difference Between read() and recv() , and Between send() and write()?
(9 answers)
Closed 7 years ago.
Is there anything bad with using read() and write() on a socket fd, instead of send() and recv()? I thought about using that in my program because read() and write() are much simpler than send() and recv().
No, there's nothing wrong with it, man 7 socket tells you explicitly that you can use the standard calls on them.
Though the send and recv functions aren't really hard to use, you can just pass 0 as the flags argument to get the same behavior as plain read and write.
This question already has answers here:
C non-blocking keyboard input
(11 answers)
Closed 9 years ago.
Let's suppose I'm in a while (1) loop calculating something, would it be possible to quit the whole application by pressing a certain key?
It's a C console application without threads.
I'm pretty sure it's not possible, but I'm a newbie so hey. I can only imagine beeing forced to press a key by some function like _getch() or similar. But then you have to wait until the user presses the key and the calculation cannot run meanwhile.
On Windows systems, there's kbhit() function that is nonblocking and returns true when any key is pressed. So, you could change while(1) to while(!kbhit()), or you could if(kbhit()) c = getch() to read the char without waiting. But this is very crude solution, really..
You could do that by using C "signals".
This is not really difficult to use. Take a look at this wikipedia page. ;)
http://en.wikipedia.org/wiki/C_signal_handling