CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2007
    Posts
    20

    Angry Sending data over Winsocks

    Sorry, new to the forum, not really sure if this is the right place to post.

    Anyways, I am just working on a server/client program which can send messages between each other. I'm able to send the messages, which are sent as a *char data type, but on the other end, I'm not sure how to validate the passed in data. For example, the server sends the message "x" to the client on connect using send(). On the client side, they will receive it using recv() into a variable buf (I did this: recv(csock, buf, sizeof(buf), 0), which works.) The only thing is I am not able to validate the buf variable, such as if(buf == "x" ) cout << something;... I am not sure how to handle the data coming through the sockets. Anyone that can give me some help on this?

  2. #2
    Join Date
    Apr 2007
    Posts
    41

    Re: Sending data over Winsocks

    if the server got the msg then its sends back to the client "msg sent"?

  3. #3
    Join Date
    Mar 2007
    Posts
    20

    Re: Sending data over Winsocks

    well, yeah it sends a message stating something like "Welcome to the server." But I want to be able to send the server a message from client or vice versa, such as "Welcome to the Server", and use it in an if statement, such as if(Buffer == "Welcome to the Server") cout << something;. That's what I can't figure out how to do, it just doesn't work.

  4. #4
    Join Date
    Apr 2007
    Posts
    41

    Re: Sending data over Winsocks

    post ur code with quote then i could understand it better.

  5. #5
    Join Date
    Mar 2007
    Posts
    20

    Re: Sending data over Winsocks

    Here's some code samples (I took some stuff out just to save me the hassle of having to put everything and explain things. The socket connection between client and server works, as do recv() and send() without errors.

    Client:

    Code:
    char buf[256];
    
    SOCKET cSock = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
    
    int receive = recv(cSock, buf, sizeof(buf), 0);
                        
    if(receive == 0) {
        cout << "Could not receive message from server...\n";
    } else {
        if(buf == "something") { // THIS is the problem.  The buf variable cannot be compared in an if statement.
                cout << "something was called";
        }
        cout << "Server response: '" << buf << "' [" << receive << " Bytes]\n";
    }
    Server:

    Code:
    SOCKET ClientSocket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
    
    send(ClientSocket, "something", sizeof(buf), 0);
    again, i took some stuff out just because it's a hassle, but this is the main concept. i cannot figure out how to compare whatever is being received through recv() using an if statement.

  6. #6
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Sending data over Winsocks

    Are you possibly looking for the strcmp() function? See http://msdn2.microsoft.com/en-us/lib...31(VS.80).aspx

    Mike

  7. #7
    Join Date
    Mar 2007
    Posts
    20

    Re: Sending data over Winsocks

    thanks for the help, but i'm not sure if strcmp is what I'm looking for.

    Even though the data sent through send() is a char *, it isn't compared in an if statement as one. I just need to know how to use the received variables (using recv()) in an if statement properly.

  8. #8
    Join Date
    Aug 1999
    Location
    Darmstadt, FRG
    Posts
    87

    Re: Sending data over Winsocks

    Hi, the problem is that you mix up pointers (buf and "something" are pointers to char) with objects of class string. Only ìf you would use the latter the comparison with == makes sense. Otherwise you compare the addresses of two unrelated buffer-areas which never will be identical.

    Cheers, Thomas

  9. #9
    Join Date
    Mar 2007
    Posts
    20

    Re: Sending data over Winsocks

    I can kind of see what you are saying, but can I please get an example?

  10. #10
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Sending data over Winsocks

    greve and I are saying mostly the same thing. Try:
    Code:
    if ( 0 == strcmp( buf, "something" ) )
    {
      cout << "something was received";
    }
    Mike

  11. #11
    Join Date
    Mar 2007
    Posts
    20

    Re: Sending data over Winsocks

    Thank you all for your help! It turned out strcmp() really was exactly what I was looking for

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured