LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Server/Client-commucation via TCP-Sockets (https://www.linuxquestions.org/questions/programming-9/server-client-commucation-via-tcp-sockets-59416/)

cYbORg 05-12-2003 02:09 PM

Server/Client-commucation via TCP-Sockets
 
Hey, there!

Well, I wrote two classes encapsulating socket funcitons of winsock1 (what should run under unix, too). I'm bored of that MS socket stuff. However, the server-application runs as exspected. The client-app's Connect()-function seems to fail, but the running server-app receives the connection. The client's Connect()-functions is implemented as follows:
Code:

bool CClient::Connect(std::string strServerIP, unsigned int nPort){
        if(m_bIsConnected)
                Close();

        memset(&m_SockAddr, 0, sizeof m_SockAddr);
        m_SockAddr.sin_family = AF_INET;
        m_SockAddr.sin_addr.s_addr = inet_addr(strServerIP.c_str());
        m_SockAddr.sin_port = htons(nPort);

        OnConnecting();
        int nResult = connect(m_nSocket, (sockaddr *)&m_SockAddr, sizeof m_SockAddr);
        if(m_bIsConnected = (nResult != SOCKET_ERROR)){
                OnConnect();
                Resume();
        }

        return m_bIsConnected;
}

The function alsways returns false by connecting to the server at 127.0.0.1:23. I have no clue of how nResult becoming -1 (SOCKET_ERROR) but enabling the connection...
I hope one could help me out.
BEst regards, Gary

Tinkster 05-12-2003 04:02 PM

First of all, what's the physical relationship
between your client & server? Do they run
on the same machine? Can you telnet the box?


Cheers,
Tink

cYbORg 05-12-2003 04:09 PM

I forst wrote the server application. It does run on the same machine and everything works fine if i telnet to my "server". My "client" is compareable to telnet. It should just connect to the server, output every received data and send all input data directly. This is totally protocol-independend.
Again: telneting works fine, but the problem I have on the client side is: connect() fails, but the server receives a connection. But how/why?

cYbORg 05-13-2003 06:12 PM

Detect server's disconnection
 
Well, I figured out that connect() always fails if the unconnected socket is in non-blocking mode. So I set in to non-blocking mode after connection. Now the server-client-connections works all fine except of I have no clue how to recognize the server's disconnection. If the server closes its socket, the non-blocking client still returns -1 on read() meaning no data available. It never returns 0.
On the other hand: if the client closes the connection, the read()-method on the server side returns 0 indicating the client's closure. But that procedure does not work on the client side in case of a server's socket closure :(
Maybe there is some bug in my code I do not know about, cause this is my first socket encapsulation. But referring to all used documentation I think it's ok like that... However: if anybody would like to see my code: email me cause my webspace-provider does not support http-downloads :(. Remember: I used winsock1. If you're gonna test it under unix, please include the corresponding headers yourself. Thx.
I'd appreciate any kind of constructive critic/help.

Gary

PTBmilo 05-18-2003 06:39 AM

I've been following this nice tutorial on the subject:

http://www.ecst.csuchico.edu/~beej/g....html#blocking

You might want to check out that select() function.

cYbORg 05-18-2003 06:01 PM

Well, I dont's think that's the answer to my question. My problem is the client announcing a connecton failure but the server recognizes a new connection. That does not have anything to do with checks on data availability.
There are many tutorials on the web but none of 'em tells anything about the descripted behaviour :(

So far, Gary


All times are GMT -5. The time now is 02:40 AM.