Advertisement

recv() gone haywire

Started by May 19, 2006 07:04 PM
2 comments, last by TLAK1001 18 years, 9 months ago
Hello, I am currently at work on a primative IMer to play with the concepts of networking. However, I am having problems using the send() and recv() functions. Right now my server reads:

int currentClient=0;
clients[currentClient]=accept(listenSock,&clientSocks[currentClient],0);
    
cout<<"Client has connected"<<endl;
    
char recvdWords[36];
recv(listenSock,recvdWords,sizeof(recvdWords),0);
    
cout<<"Client says "<<recvdWords<<endl;

and my client reads:

cout<<"Connecting to server..."<<endl;
    
    //Define server socket
    sockaddr_in server;
    server.sin_family=AF_INET;
    server.sin_port=htons(5001);
    server.sin_addr.s_addr=inet_addr("192.168.1.100");
    
    if(connect(serverSock,(struct sockaddr*)&server,sizeof(server))==SOCKET_ERROR)
    {
          cout<<"\nError: Failed to connect to server!"<<endl;
          cin.ignore(1,'\n');
          WSACleanup();
          return 0;
    }
    
    char words[]="Look! The program works!";
    send(serverSock,words,sizeof(words),0);

But when I run the client, the server spits out a string of strange characters. The string is the same every time, even if I change what I'm sending. Why won't send() and recv() work?
There is a forum specifically for networking stuff.

At any rate, when you are getting errors it helps to check error codes. That would probably give you an idea of what the problem is. Likely recv is returning an error and since you're not checking for it you print more-or-less random garbage.

The most obvious problem is that you're recving from your listen socket instead of the socket returned by accept...
-Mike
Advertisement
Agreed -- you're calling recv() on the listenSock, when you should be passing the clients[currentClient] socket.

Moved to Networking and Multiplayer.
enum Bool { True, False, FileNotFound };
that fixed it. Big thanks to both of you.

This topic is closed to new replies.

Advertisement