-
February 18th, 2008, 03:01 PM
#1
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?
-
February 18th, 2008, 03:24 PM
#2
Re: Sending data over Winsocks
if the server got the msg then its sends back to the client "msg sent"?
-
February 18th, 2008, 03:34 PM
#3
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.
-
February 18th, 2008, 04:00 PM
#4
Re: Sending data over Winsocks
post ur code with quote then i could understand it better.
-
February 18th, 2008, 09:27 PM
#5
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.
-
February 19th, 2008, 02:00 AM
#6
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
-
February 19th, 2008, 06:31 AM
#7
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.
-
February 19th, 2008, 10:12 AM
#8
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
-
February 19th, 2008, 06:43 PM
#9
Re: Sending data over Winsocks
I can kind of see what you are saying, but can I please get an example?
-
February 19th, 2008, 08:42 PM
#10
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
-
February 19th, 2008, 10:59 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|