Network programming - socket stuck in close_wait state
I am using libevent to comminucate between two computers on the network. The following code is a minimal example to reproduce my problem. The same code is running on both computers.
What I want to happen is that either computer can send data to the other, then close the connection. But what actually happens is that both end up with the connections hanging in the CLOSE_WAIT state and eventually the max number of open files is exceeded and everything stops. Labelling the computers A and B, in my head this is what should be happening: A and B both call Listen() A calls Send() which calls bufferevent_socket_connect() B calls Accept() A calls HandleSendEvent() which calls bufferevent_write() B calls Read() then bufferevent_free() A calls Close() which calls bufferevent_free() then event_base_loopbreak() Since bufferevent_free() is meant to close the socket I expected the socket to be closed on both sides, but my logic is obviously wrong. I can see using tcpdump that no finish packet is ever sent. It's my first time using libevent and any help would be much appreciated, thanks. Code below (I'm posting it all since I don't even have a clue where the problem is) and screenshot here. Code:
#include <event2/listener.h> Code:
g++ -g -Wall -Werror netest.cpp -L/usr/local/lib -levent -pthread -o netest |
I'm not sure that you can have both a client connection and a server connection on the same ip/port pair. I would use two different ports for server and client. How would a packet know whether it should go to your Listen thread or be a response to your Send thread?
|
The port number I'm using is used to listen on and connect to, not connect from. When you make an outgoing connection the OS will assign a free port to make the connection on. As you can see in the screenshot, all the ip/port pairs are different.
|
You might want to quote some sample-output of command nestat -an | grep 55000
|
1 Attachment(s)
I've attached a screenshot of the output.
|
Guess you shouldn't open a new connection for every message.
|
1 Attachment(s)
It happens even with one single connection.
|
All times are GMT -5. The time now is 04:47 AM. |