开发者

Problems after server uses accept() to accept connections

开发者 https://www.devze.com 2023-04-08 02:25 出处:网络
This is my first time ever using sockets, and I am having trouble accepting connections on the server side. My server is only designed to accept one connection at a time. Once a connection is received

This is my first time ever using sockets, and I am having trouble accepting connections on the server side. My server is only designed to accept one connection at a time. Once a connection is received, the current date and time are written to the socket, and then the client prints out the date and time it received from the server. My server has the following code:

cout << "Server: Waiting for connections." << endl;
client_length = sizeof(client_address);
connection_fd = accept(listen_fd, (struct sockaddr*)&client_address, (socklen_t*)&client_length);
cout << "Server: Client connected" << endl;

When I run my server, I get the following output:

./server&
Server: Waiting for connections.

Then when I run my client, I get 开发者_高级运维the following output:

./client 127.0.0.1
Client: Connecting to: 127.0.0.1
Client: Connected to server.
Sun Sep 25 13:20:07 2011

The client seems to print out the correct data, but the server never prints out that a client connected. Something is wrong here. Another symptom is when I try to write to the pipe (client writes, server reads), the client gets a broken pipe error. Is there something I am missing? If there is any code you would like to see, please ask.

Edit: Here is the server running under strace. Nothing seems to happen after accept is printed out. Weird?

write(2, "Server: Socket created.", 23Server: Socket created.) = 23
write(2, "\n", 1
)                       = 1
bind(3, {sa_family=AF_INET, sin_port=htons(4007), sin_addr=inet_addr("0.0.0.0")}, 16) = -1 EADDRINUSE (Address already in use)
write(2, "Server: Address and port bound t"..., 41Server: Address and port bound to socket.) = 41
write(2, "\n", 1
)                       = 1
listen(3, 100)                          = 0
write(2, "Server: Socket is now a listenin"..., 41Server: Socket is now a listening socket.) = 41
write(2, "\n", 1
)                       = 1
write(2, "Server: Waiting for connections.", 32Server: Waiting for connections.) = 32
write(2, "\n", 1
)                       = 1
accept(3,  

Thanks.


This shouldn't happen.

It's even hard to imagine a broken scenario, like you have two instances of the server running, and the other one is accepting the connections, but then the new instance shouldn't be able to use the same port number again, so you probably doesn't verify return values of system calls, but then accept should fail, and you should see a false client connected message.. so, what the heck are you doing?

Update: yepp... -1 EADDRINUSE (Address already in use)

Another instance is running, or something else that uses that port number. Check with netstat. And for god's sake check the return values of those calls.


Your bind is failing with EADDRINUSE, yet your server plods on. Add error handling to your code, and possibly change the port your are using.

Also, I suggest you make sure that you are not binding the same port in the client by accident

0

精彩评论

暂无评论...
验证码 换一张
取 消