Click to See Complete Forum and Search --> : Loop problem


tieungao35
March 24th, 2006, 07:16 PM
Hi all,
I downloaded 2 programs that help me to learn winsock programming. Right now, it works fine if I send only 1 message from client to server. However, i can't send multiple message from client to server by using a loop in client. Please help me to solve this. Thanks.

ridcullyCZ
March 25th, 2006, 05:51 AM
Maybe I'm wrong, I'm a newbie to sockets. But AFAIK you're not able to distinguish particular messages in TCP communication. The server just accepts a stream of bytes without letting you know that a new message comes. I thing possible sollutions are:
1. changing protocol to UDP
2. closing and opening the TCP connection for each message
3. adding your own flag of new message on client side and searching for that flag on server side.
Try to find more in some socket tutorial or in socket API reference.

logan
March 25th, 2006, 10:47 AM
YOu are returning after you recieve a message and thus you cann't reieve more messages. See the code below from your server, comment the line I have marked and it works as you expect.

char * readline(SOCKET *client)
{
vector<char> theVector;
char buffer;
int rVal;

while(true)
{
rVal = recv(*(client), &buffer, 1, 0);
if(rVal == SOCKET_ERROR)
{
int errorVal = WSAGetLastError();

if(errorVal == WSAENOTCONN)
{
socketError("Socket not connected!");
}
socketError("Failed recv()");
WSACleanup();
}

if(buffer == '\n')
{
char *data = new char[theVector.size() + 1];
memset(data, 0, theVector.size()+1);

for(int i=0; i<theVector.size(); i+=1)
{
data[i] = theVector[i];
}

cout << data << endl;
// return data;<--comment out this
}
else
{
theVector.push_back(buffer);
}
}
}

tieungao35
March 25th, 2006, 12:28 PM
It's working now. However, I don't undestand why. Can you please explain to me?
Just another question, how can i do modify those codes in order send a message from the server to client in that program? hihi.

Thanks for your help.