Client - server communication - problem with buffers

same buffer for receiving/sending data problem

Page 1 of 1

4 Replies - 4688 Views - Last Post: 05 December 2009 - 03:08 AM Rate Topic: -----

#1 Teres  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 02-December 09

Client - server communication - problem with buffers

Post icon  Posted 02 December 2009 - 04:02 AM

Hi

I recently started learning client/server communication and I found nice tutorial HERE. I've modified it a bit, so that client sends message to server, server changes the message to all capital letters and should send it back to client.
I don't undestand why only the 3rd way of sending data back to client works ok. Seems to me that I have to use another buffer to send data to client, not the one I'm receiving data to.
Can anyone try to explain this ? (It's been bothering me since month or so :angry: ;) )
Client code
Console.WriteLine("Type 'exit' to exit");
   while (true)
   {
		Console.Write("SEND: ");
		buffer = encoder.GetBytes(Console.ReadLine());
		if (encoder.GetString(buffer) == "exit")
		{ break; }
		 Console.WriteLine("SENDING: " + encoder.GetString(buffer));
		 clientStream.Write(buffer, 0, buffer.Length);
		 clientStream.Read(buffer, 0, buffer.Length);
		 Console.WriteLine("REPLY: " + encoder.GetString(buffer));
	}


Server code
while (true)
	{
	bytesRead = 0;
	try
		{
			bytesRead = clientStream.Read(message, 0, 4096);
		 }
		 catch
		 {  break;  }
		  if (bytesRead == 0)
		  { break;  }
		  Console.WriteLine("RECEIVED: " + encoder.GetString(message, 0, bytesRead));
		  //REPLY
		  // 1			 
		  /*   sMessage = encoder.GetString(message, 0, bytesRead);
				sMessage = sMessage.ToUpper();
				message = encoder.GetBytes(sMessage);
				Console.WriteLine("REPLY: " + encoder.GetString(message, 0, bytesRead));
				clientStream.Write(message, 0, bytesRead);*/
			   
		  //2
		  /*   message = encoder.GetBytes(encoder.GetString(message,0,bytesRead).ToUpper());
				Console.WriteLine("REPLY: " + encoder.GetString(message, 0, bytesRead));
				clientStream.Write(message, 0, bytesRead);*/
			   
		  //3
				byte[] buffer = encoder.GetBytes(encoder.GetString(message, 0, bytesRead).ToUpper());
				clientStream.Write(buffer, 0, buffer.Length);
				Console.WriteLine("REPLY: " + encoder.GetString(buffer, 0, buffer.Length));
			   
}

This post has been edited by Teres: 02 December 2009 - 04:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Client - server communication - problem with buffers

#2 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Client - server communication - problem with buffers

Posted 03 December 2009 - 09:43 PM

What error are you getting? My only guess would be because your server's "message" byte[] variable is not the same size as your client's "buffer" byte[] variable. Notice how your server reads up to 4096 bytes into the message variable? If your buffer variable on the client size is not the same size, then this may present a problem since your client is expecting a byte array of the same size to come back. That's the most educated guess I can think of.
Was This Post Helpful? 0
  • +
  • -

#3 Teres  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 02-December 09

Re: Client - server communication - problem with buffers

Posted 03 December 2009 - 11:35 PM

View Postmasteryee, on 3 Dec, 2009 - 08:43 PM, said:

What error are you getting? My only guess would be because your server's "message" byte[] variable is not the same size as your client's "buffer" byte[] variable. Notice how your server reads up to 4096 bytes into the message variable? If your buffer variable on the client size is not the same size, then this may present a problem since your client is expecting a byte array of the same size to come back. That's the most educated guess I can think of.

Thanks for the reply, actually there are no compiler errors. Thus the thing you mentioned (server's buffer size) should not be the issue.
If I use 1st or 2nd way of sending data back to client, client disconnects from server after receiving one reply (like it was closing it's socket , if I try to send another message to server I get an exception).
If I use the 3rd method, they can talk to each other all night long :D

Maybe this line is source of the problem (in server's code):
clientStream.Write(message, 0, bytesRead);

but I'll be able to check this when I get back from work (maybe it should be)
clientStream.Write(message, 0, message.length);

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: Client - server communication - problem with buffers

Posted 04 December 2009 - 09:10 AM

The use of encoding can change the size of the received data, so the number of bytes read may not be the same as the number of bytes after encoding.

Moral of the story: don't use the number of bytes read as the length of the data you're writing back.
Was This Post Helpful? 0
  • +
  • -

#5 Teres  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 22
  • Joined: 02-December 09

Re: Client - server communication - problem with buffers

Posted 05 December 2009 - 03:08 AM

I tried using
clientStream.Write(message, 0, message.length);
to cend reply to cllient but it changes nothing. I like to know why something is working (or not) but here I've got no idea why it's not :huh:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1