C++ socket

Hi,

I'm working on sockets in linux using telnet. Every things seems to work perfectly fine but when i kill the telnet, it will terminate my socket connection which i don't want.
Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  try {
            while (_this->running) {
                
                // MAIN CYCLE OF THE SOCKET
                
                // sends a messages from the queue through the socket
                while (_this->messages.size()>0) {

                    //cout << "Queue size: " << _this->messages.size() << endl;

                    string currentMessage = _this->messages.front();
                    currentMessage.append("\n");
                    cout << "write: " << _this->newSockFd << endl;

                    int n = write(_this->newSockFd, currentMessage.c_str(), currentMessage.length());
                    
                    cout << "n: " << n << endl;
                    if (n < 0) {
                        cout << "Throws an exception " << endl;
                        throw string("ERROR writing to socket");
                    }
                    _this->messages.pop();

                    //cout << "Message [" << currentMessage << "] written into the socket."  << endl;

                }

                // wait for a new message
                pthread_cond_wait(&_this->numberOfMessagesCondition, &_this->mutexSendMessage);

                // END OF THE MAIN CYCLE OF THE SOCKET

            }
        } catch (string exception) {
            cout << "Client Disconnected: " << exception << endl;
            _this->newSockFd = -1;
        }


i tried to test it. It seems the error is in the write function is used to write my data through the socket.When we kill the telnet and write is executed. It just terminate without returning any value.Which is not suppose to happen though.

Any help in this regard will be highly appreciated.

Thanks.
Chaudhry
I also tried to use the function sendto instead of write:

int n = sendto(_this->newSockFd, currentMessage.c_str(), currentMessage.length(), 0, (struct sockaddr *) &_this->cli_addr, sizeof(_this->cli_addr) );

but the result is still the same.

Any suggestion?
If a socket endpoint is gone, what else do you expect to happen?
The problem is exactly this.

How can I know if an endpoint is still valid or not?

I expected the write method raise a sort of exception or return an error, so I can handle this and do something in the server side.
But the problem is that when the program execute the instruction write, it just dies, without return any kind of error.

How can I know if the connection is still valid?
I expected the write method raise a sort of exception or return an error
It does. It returns -1 if it fails and sets errno to the reason why it failed.

But the problem is that when the program execute the instruction write, it just dies
Are you sure? Where does it die?
Yes, I'm sure.

This is the code I have inside the while cycle:
1
2
3
4
5
cout << "write... " << endl;
int n = write(_this->newSockFd, currentMessage.c_str(), currentMessage.length());
cout << "written." << endl;
cout << "errno: " << errno << endl;
cout << "n: " << n << endl;


and the output is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
write... 
written.
errno: 0
n: 13
write... 
written.
errno: 0
n: 13
write... 
written.
errno: 0
n: 13
write... 
Press [Enter] to close the terminal ...


So it means that the istruction write has some segment fault or something similiar.

Also the shell, after the program is terminated, says:
RUN FAILED (exit value 141, total time: 12s)

This is very strange.
Last edited on
closed account (z05DSL3A)
Is it that write() is generating a SIGPIPE due to a broken socket?

Edit:
You may be able to setsockopt() with SO_NOSIGPIPE, but I don't think this is portable and may not be available. Maybe set the SIGPIPE handler to SIG_IGN.
Last edited on
Topic archived. No new replies allowed.