M
Matt
I wrote the tcp socket client-server program that the server will
echo the message received from the client.
In client program:
char sendBuf[100];
while(1)
{
cout << "Enter message:";
cin.getline(sendBuf,100);
rVal = send(theSocket, sendBuf, strlen(sendBuf), 0);
}
In server program:
char recvBuf[100];
while (1)
{
rVal = recv(s, recvBuf, strlen(recvBuf), 0);
cout << "Echo: " << recvBuf << endl; //appends garable charcters
cout << recvBuf << endl;
//try to clear the buffer first for every echo, but it turned out
//couldn't get any data from client
//strcpy(recvBuf,""); <---------------------
}
The problem is recvBuf variable is appended with funny characters for
every
echo. And I think I need to clean up the buffer for every echo;
otherwise
it will append with previous data. The output of recvBuf is like this:
Echo: hello world ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦`?
Echo: erere world¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦?
Echo: weee world¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦?
^C
If I initialize recvBuf as follows, then server program is not able to
get data from client
strcpy(recvBuf,"");
any workarounds to the problem? please advise!! thanks!
echo the message received from the client.
In client program:
char sendBuf[100];
while(1)
{
cout << "Enter message:";
cin.getline(sendBuf,100);
rVal = send(theSocket, sendBuf, strlen(sendBuf), 0);
}
In server program:
char recvBuf[100];
while (1)
{
rVal = recv(s, recvBuf, strlen(recvBuf), 0);
cout << "Echo: " << recvBuf << endl; //appends garable charcters
cout << recvBuf << endl;
//try to clear the buffer first for every echo, but it turned out
//couldn't get any data from client
//strcpy(recvBuf,""); <---------------------
}
The problem is recvBuf variable is appended with funny characters for
every
echo. And I think I need to clean up the buffer for every echo;
otherwise
it will append with previous data. The output of recvBuf is like this:
Echo: hello world ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦`?
Echo: erere world¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦?
Echo: weee world¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦?
^C
If I initialize recvBuf as follows, then server program is not able to
get data from client
strcpy(recvBuf,"");
any workarounds to the problem? please advise!! thanks!