Sign in to follow this  
Thunder Sky

Winsock Server to Client sending problem

Recommended Posts

Thunder Sky    144
Hi Im what you could call a experienced newbie to programing in C/C++. Im still in that "Oh, whats that? I gotta try it!"-phase. My last affection was OpenGL (which I have taken a pause with right now because I'm not good enough yet, I did get a nice terrain viewer working though) Anyway, I always been somewhat facinated by networks and now Ive dived into Winsock. I've read some tutorials and such and managed to make a server-client program (the server could only take 1 client though :P). It was pretty much useless so I trashed that and more or less started over. Ive gotten so far that my server should be able to accept several clients and then comunicate whith them. Heres my centeral code which loops untill a server shutdown messages is given by the admin:

    // ACCEPT CONNECTION AND TEST IT
    if(numclients<maxclients) {
      if((sClient[numclients]=accept(sServer, &addrClient[numclients], &addr_size))==INVALID_SOCKET) {
        err=WSAGetLastError();
        switch(err) {
          case WSAEWOULDBLOCK:
          break;
          //error checking
          default:
          printf("Unknown error when accepting connection from client %d", numclients);
          break;
          };
        } else {
        buffer[0]==TEST_SEND;
        while(send(sClient[numclients], buffer, 1, 0)==SOCKET_ERROR) {
          err=WSAGetLastError();
          switch(err) {
            case WSAEWOULDBLOCK:
            break;
            //error checking
            default:
            printf("send to client%d: unknown error (%d)", i, err);
            break;
            };
          Sleep(500);
          };
        numclients++;
        printf("Now %d clients\n",numclients);
        };
      };
    
    // RECIEVE MESSAGES FROM CLIENTS
    for(i=0;(i<numclients) && (sClient[i]!=INVALID_SOCKET);i++) {
      if(recv(sClient[numclients],buffer,2,0)==SOCKET_ERROR) {
        err=WSAGetLastError();
        switch(err) {
          case WSAEWOULDBLOCK:
          break;
          //error checking
          default:
          printf("recv from client%d: unknown error (%d)", i, err);
          break;
          };
        } else {
        printf("Recieved package from client %d",i);
        };
      };

Ok so here's the problem: the recv() keeps failing and WSAGetLastError() returns WSAENOTSOCK. According to microsofts developer site I havnt set up the socket properly. The problem is, I didnt know you had to! I have not called socket() on the clients or bound them to any port. The microsoft site had a good example of how to get a client to send to a server but not the other way round. Whats strange here is that the send() isnt reporting any errors (even though I cant be 100% sure it works because I havnt put together a client to recvieve that data yet) When you send data from a client it uses a socket which has been bound to a specific IP and port. As you can see I havnt done anything like that in the code above, ut I thought accept set up the socket for you. I guess my question is: sould I call something like bind(sClient[numclients],&addrClient[numclients],sizeof(addrClient[numclients])) before sending the TEST_SEND? I should probably also point out that sServer is a non-blocking socket. Any comments or ways to fix this would be apreciated. /ThunderSky

Share this post


Link to post
Share on other sites
hplus0603    11348
I suggest you try Beej's networking tutorial, or some of the other links found in the Forum FAQ.

In brief, to use TCP, you do this:


// On the client
s = socket(...) // socket connecting to server
connect(s, ...)
select(...)/send(s, ...)/recv(s, ...)

// On the server
a = socket(...) // the listening socket
bind(a, ...)
listen(a, ...)
s = accept(a) // one socket per client
select(...)/send(s, ...)/recv(s, ...)

Share this post


Link to post
Share on other sites
Thunder Sky    144
Thanks for the reply.
Ok, so I dont need to do anything more on the client socket file descriptor after accept(). But I still dont get why the code won't work. I meen, if accept() fixed the socket so that you could call "select(...)/send(s, ...)/recv(s, ...)" on it then recv() shouldn't complain right? Am Im missing something ovious in my code?

(I know I should use select(), and I will as soon as I completely understand the more basic send/recieve functions. Im also going to throw together a client soon (hopefully) to recieve the data that the server send()s so I can check if the problem lies in the recv() call or in the socket setup)

But as I said Im still a bit lost, because I think ive done everything accordingly to the tuts and it still doesnt work, so, again, if theres something wrong with the above code please point it out to me, it would help.

Thanks again.

/ThunderSky

Share this post


Link to post
Share on other sites
Thunder Sky    144
Hey fellows.

Im happy :D

I found the ovious error in my code. In the recieving bit of the code I tried to recv() from sClient[numclients] when I sould have tried to recv() from sClient[i]. Stupid mistake and I apologize for taking your time.

Thanks a lot hplus0603 for the comment.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this