I have encountered a problem when I was trying to exchange messages between a server and client in C.
Server
char username[50],parola[50];
bzero(username,50);
strcpy(mTrimis,"Enter message");
//this is working fine
if(write(client,mTrimis,sizeof(mTrimis))<=0)
{
perror ("[server]Error1\n");
continue;
}
//this is working fine
if(read(client,username,sizeof(username))<=0)
{
perror("[server]Error1r\n");
continue;
}
bzero(parola,50);
bzero(mTrimis,500);
strcpy(mTrimis,"Enter password");
//this is working fine
if(write(client,mTrimis,sizeof(mTrimis))<=0)
{
perror ("[server]Error2\n");
continue;
}
//this "read" is not working
if(read(client,parola,sizeof(parola))<=0)
{
perror("[server]Error2\n");
continue;
}
bzero(mTrimis,500);
printf("%s-%s\n",username,parola);
Client
bzero(mTrimis,500);
bzero(mPrimit,500);
if(read(sd,mPrimit,sizeof(mPrimit))<0)
{
perror ("[client]Error1 client)\n");
return errno;
}
printf ("MESSAGE SERVER:\n%s\n",mPrimit);
if(read(0,mTrimis,sizeof(mTrimis))<0)
{
perror ("[client]error client-keyboard\n");
return errno;
}
if(write(sd,mTrimis,sizeof(mTrimis))<0)
{
perror ("[client]Error1 client->server\n");
return errno;
}
bzero(mTrimis,500);
bzero(mPrimit,500);
if(read(sd,mPrimit,sizeof(mPrimit))<0)
{
perror ("[client]Error2 cleint\n");
return errno;
}
printf ("MESSAGE SERVER:\n%s\n",mPrimit);
if(read(0,mTrimis,sizeof(mTrimis))<0)
{
perror ("[client] error2 client keyboard\n");
return errno;
}
if(write(sd,mTrimis,sizeof(mTrimis))<0)
{
perror ("[client]Error2 client->server\n");
return errno;
}
So the server is asking the client for an username and receives it. Then, it asks for a password and here (at the second "read") it doesn't work properly. It just passes over this "read", it will print something like this "user123-" and will disconnect the client because it reached at the end of the code.
In the client side, it gets the message asking for username from server, then the client sends its message to the server. Then it gets the second message from the server, the one asking for a password, and is unable to send the 2nd message back to the server, I get the "error2 client->server" error (because it was disconnected).
Check the size of the buffers on the client side used to send data to that of the size of the buffers on the server side to get data. Clue is in there.
Client:
if(write(sd,mTrimis,sizeof(mTrimis))<0)
Used to write username in the first write
. You are writing at least 500 bytes going by bzero(mTrimis,500);
So, 500 bytes go out on socket.
Server:
How much is being read in getting username?
if(read(client,username,sizeof(username))<=0)
Just 50 going by char username[50]
Hence server does not read the the entire bytes the client sent.
The next time you call read
on server you are reading the remaining of the 500 bytes - this time too just 50 - which are all zeroes. Hence the behaviour you see.
Solution:
Once you read from standard input, instead of using sizeof
the buffer in the write
, use the actual number of bytes input by the user.
n = read(0,mTrimis,sizeof(mTrimis));
if(write(sockfd,mTrimis,n)<0)