How to write a function to disconnect client from server in C? [duplicate] - c

This question already has answers here:
close vs shutdown socket?
(9 answers)
Closed 7 years ago.
I tried server client tcp code in c, I want to add some functionality to disconnect client from server. I search on google for it. I found function shutdown(), I am not getting idea how to do it ?

To disconnect a client from the server, just close the fd linked to this client.
To disconnect a client to a server from the client, just close the client socket.
This is a quick way to disconnect, but don't forget to send a last exit message when you are leaving from a server / disconnecting a client.
No need of shutdown here. (Except if you share fd between processes, but without more information we cannot be more precise)

Related

How to let a socket server accept only once [duplicate]

This question already has answers here:
How can I refuse a socket connection in C?
(5 answers)
Closed 4 years ago.
I have a really big headache.
If there is a case that I have many socket client and only one socket server, how can I make the server only accept the first connect() order he received, and fail all the other connect() from other client? I'm using C in linux btw
I'm really struggle with this question. In other words, how can I make a client recognizance that "Hey,this port number of this ip is busy, I need to try another one maybe."
My teacher told me that I will get a timeout error if i just let a client connect to a server, while another client has connect to the same ip same port recently. But, well, I just cannot find where is the error. Both of the connect() functions in two clients return 0.
Maybe I misunderstand my teacher's word,but since the program doesn't crash and no negative number returned from functions, where is it,how can I find it?
Thanks a lot.
Try using a value of 0 or 1 for backlog in your listen() call.
I haven't tried it, but the man page claims that extra connections will get a ECONNREFUSED error.
Immediately after calling accept to get your actual socket to the first client close the listener. Otherwise another client can queue up.

share a socket with a previously forked child [duplicate]

This question already has an answer here:
Sending file descriptor by Linux socket
(1 answer)
Closed 4 years ago.
I've got a server with a number of worker processes forked initially with the server setup. The sever accepts connections, and must pass the descriptor to a worker.
What's an easiest way that I could later on share the socket descriptor of a client's connection with a worker?
I would suggest you to run workers with specific port range and use socketpair to share the socket descriptor from another port. Also you can block those worker ports for external communication for the sake of good security pratices.

make server to receieve/send data from/to a client several times(morethan once) in C

I have made a client and server application. In that when ever i am sending data to the server,the server is receiving data,but the server goes into the block state(waits to recv data still when data sending has been completed from client) then I compulsory needs to shutdown the socket form sending data. Which I don't want.
So can any one please tell me how to tell the server that my data transfer is completed.
My server behaves as following:
CLIENT: hi //now compulsory to shutdown the socket otherwise server
goes into block state
SERVER: hi //after this nothing can be
transferred since socket is closed
Where as i want like following:
CLIENT: hi
SERVER: hi
CLIENT: how are you?
SERVER: I am fine, thanks!
SERVER: What about you?
CLIENT: I am fine as well, thanks.
Other than setting the socket to be non-blocking, if that makes it easier, you need to develop your own protocol. This protocol would define the communications between client/server. For example, you could include data size, data, or just some form of data separator if you only use strings.
Look into the ioctlsocket function (on Windows) or the fcntl function (in linux), and in particular, set the socket to be non-blocking.
fcntl(sock_fd, F_SETFL, O_NONBLOCK);
Alternatively, have a separate thread to write to the socket while the first is blocking on the read.
Some more info here: http://rhoden.id.au/doc/sockets2.html

Localhost connection closed not detected with select

I design a little web server in C winapi.
I use select to detect a 'connection closed' from the localhost (Firefox).
I launch a request with Firefox on my local server, and my software wait until I close the connection with Firefox (stop button).
If I close the connection from Firefox after about ten seconds, my software is able to detect the connection closed.
But, if I close the connection after one minute, my software doesn't detect anything...
ONLY, when I close Firefox, the connection closed is finally detected or ... finally closed by firefox ?!
Detection of the closing of a socket can be very dependent on how/why the socket was closed along with other characteristics of the connection. I've found that one way to detect a closed connection in a reasonable amount of time is to attempt to write to the connection (as in establishing a periodic poll in both directions). That should result in an immediate error.
How this helps in your particular case, I'm not sure.

Is it possible for me to accept a connection and have it die withouit my knowing, then accept antoher connection on the same socket number?

Is it possible for me to accept a connection and have it die withouit my knowing, then accept another connection on the same socket number?
I've got a thread to do protocol parsing and response creation. I've got another thread to handle all my network IO and one more thread to handle new incomcing connection requests. That makes three threads total. Using select in the IO thread, I get a failure and have to search for the dead socket. I am afraid there is the case that accept might want to accept a new connection on a socket number that was previous dead.
I'd assume this can't happen until I "shutdown() || close();" the socket that may be dead on the server side. If it could happen, is the only solution to setup mutexes to halt everything while I sort out what sockets have gone bonkers?
Thanks,
Chenz
A socket descriptor wont get reused until you close it.
Assuming we're talking TCP, then if the remote side closes its send side of the connection then you'll get a recv() returning 0 bytes to tell you of this. Since TCP support half closed connections you could still be able to send data to the remote side of the connection (if your application level protocol is made that way) or you might take the fact that the remote side has closed its send side as an indication that you should do the same.
You use shutdown() to close either your send side or your recv side or both sides of the connection. You use close() to close the socket and release the handle/descriptor for reuse.
So, in answer to your question. No, you wont be able to accept another connection with the same socket descriptor until you call close() on the descriptor that you already have.
You MAY accept a connection on a new socket descriptor; but that's probably not a problem for you.

Resources