Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


TCP messages not being sent


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Saoblol   Members   -  Reputation: 143

Like
0Likes
Like

Posted 22 August 2012 - 01:41 AM

Hi

I am currently working on a project that involves a server and multiple clients that can connect to it and request data. Im sending messages (strings) from the client to the server and vice versa (never between clients), but sometimes they dont get sent but are stuck until I try to send it again, and then both messages are sent at the same time. At first this seemed easy to solve: the packets are automatically stored until there is enough data to send out in order to save bandwith (aka Nagle algorithm), so setting tcpclient.NoDelay = true should do the trick, but it didnt. I've tried to solve this for days now, but can't get any progress on it.

Im using a BinaryWriter for sending data, and also tried the tcpclient.Client.Send(byte[]) method with the same results.

public Client()

{

this.tcpClient = new TcpClient();

this.tcpClient.NoDelay = true;

this.outgoingMessages = new List<string>();

}





/// <summary>

/// Write pending messages to the server.

/// </summary>

private void Write() //This is running in a thread

{

while (true)

{

lock (this.outgoingMessages)

{

if (this.binaryWriter != null && this.outgoingMessages.Count > 0)

{

this.binaryWriter.Write(this.outgoingMessages[0]);

this.binaryWriter.Flush();

this.outgoingMessages.RemoveAt(0);

}

}

Thread.Sleep(33);

}

}


Hopefully someone knows an answer to this smile.png

Sponsor:

#2 rip-off   Moderators   -  Reputation: 8532

Like
0Likes
Like

Posted 22 August 2012 - 02:25 AM

How does the server handle reading data? Is it non blocking? It is possible that the server is blocking while waiting for data from a particular client, and all the other clients then block as the server's receiving buffers for their connection fills.

#3 Saoblol   Members   -  Reputation: 143

Like
0Likes
Like

Posted 22 August 2012 - 02:53 AM

This is how I receive incoming data (also running in a thread)

	/// <summary>
	/// Read the incoming messages from the server.
	/// </summary>
	private void Read()
	{
	  while (true)
	  {
		this.CheckConnection();
		if (this.isConnected && this.tcpClient.Available > 0)
		{
		  string message = this.binaryReader.ReadString();
		
		  //Processing the message
		  //...
		}
		Thread.Sleep(33);
	  }
	}

Edited by Saoblol, 22 August 2012 - 02:54 AM.


#4 hplus0603   Moderators   -  Reputation: 5547

Like
0Likes
Like

Posted 23 August 2012 - 12:10 PM

You are not guaranteed that an entire string is available, just because some bytes are available.

Typically, on TCP, you will prefix each message (string, in this case) with a length value, and you'll receive data into a buffer. When there is enough data in the buffer to cover both the length value, and all the data that that length value says should be there, you decode that message and remove from the buffer.

enum Bool { True, False, FileNotFound };




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS