1

I have implemented the client server using socket programming in C on Unix OS. I have used the non blocking socket at client end. I want to implement the two way communication. But its working only with one way i.e. Client can read and write the data on server, but server can not read or write data on client.

Client

nread = recv(sock, ptr, nleft, MSG_DONTWAIT))
send(sock, ptr, nleft, 0))

Server

recv(sock, ptr, nleft, MSG_DONTWAIT))
SockWrite(sock, Message, dataLength)

Server is always facing problem in reading. Can any one explain me why and how to get rid of this?

3
  • 3
    Posting complete source code will increase your chances of receiving answers. Commented Oct 12, 2009 at 11:13
  • 1
    Can you clarify - do you mean that the client can "recv data from the server" and "send data to the server"? If that is the case, then it seems like your server is able to both send/recv data from the client. Commented Oct 12, 2009 at 19:42
  • yes I want a two way communication berween client and server. Commented Oct 13, 2009 at 5:26

3 Answers 3

1

Await for socket ready for reading or writing using select() call.

Sign up to request clarification or add additional context in comments.

Comments

0

code samples

static void SetNonBlock(const int nSock, bool bNonBlock)
{
    int nFlags = fcntl(nSock, F_GETFL, 0);
    if (bNonBlock) {
        nFlags |= O_NONBLOCK;
    } else {
        nFlags &= ~O_NONBLOCK;
    }

    fcntl(nSock, F_SETFL, nFlags);
}

     ...
     SetNonBlock(sock, true);
     ...
    int code = recv(sock, buf, len_expected, 0);
    if(code > 0) {
            here got all or partial data
    } else if(code < 0) {
        if((errno != EAGAIN) && (errno != EINPROGRESS) ) {
                         here handle errors
        }
              otherwise may try again       
    } else if(0 == code) {
        FIN received, close the socket
    }

Comments

0

What is the return code to recv? Have you set the recv socket to non-blocking? In that case you are probably seeing EAGAIN, and you need to select() etc, or go back to blocking. I would not recommend ever ignoring return values to system calls.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.