Click to See Complete Forum and Search --> : Sending data over Winsocks


tophat
February 18th, 2008, 02:01 PM
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?

SliderMan
February 18th, 2008, 02:24 PM
if the server got the msg then its sends back to the client "msg sent"?

tophat
February 18th, 2008, 02:34 PM
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.

SliderMan
February 18th, 2008, 03:00 PM
post ur code with quote then i could understand it better.

tophat
February 18th, 2008, 08:27 PM
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:


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:



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.

MikeAThon
February 19th, 2008, 01:00 AM
Are you possibly looking for the strcmp() function? See http://msdn2.microsoft.com/en-us/library/e0z9k731(VS.80).aspx

Mike

tophat
February 19th, 2008, 05:31 AM
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.

greve
February 19th, 2008, 09:12 AM
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

tophat
February 19th, 2008, 05:43 PM
I can kind of see what you are saying, but can I please get an example?

MikeAThon
February 19th, 2008, 07:42 PM
greve and I are saying mostly the same thing. Try:
if ( 0 == strcmp( buf, "something" ) )
{
cout << "something was received";
}

Mike

tophat
February 19th, 2008, 09:59 PM
Thank you all for your help! It turned out strcmp() really was exactly what I was looking for :)