F
Francois Malgreve
Hi,
I am a bit new in socket programming and I have a question about what's the
best way to implemnt a TCPClient that can receive long messages?
The example I found in MSDN works for me but it downloads only the first
chunck of data sent by the server I connect to. How can i make the code wait
until the end of the message is received from the server?
Changing the buffer size won't change anything as i guess the stream.Read()
method returns after the first bunch of data is received from the server. I
don't really know how many packets is that neither why it returns before
everything is read but i guess that i need to add in some infrastructure
that will do some waiting and / or looping and which will detect when all
the data is read and that i can exit.
I have done some research on the internet and the example i saw often did
not care for this kind of longer message or were too complex or not working
if not communicating with their own server implementation. I have no access
on the server I connect to then I cannot change anything on that side.
Here is the code i use from MSDN doc (TCPClient class):
static void Connect(String server, String message)
{
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 13000;
TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("\n Press Enter to continue...");
Console.Read();
}
Anyone can help me or point me to an adequate resource?Thanks a lot,Francois
Malgreve
I am a bit new in socket programming and I have a question about what's the
best way to implemnt a TCPClient that can receive long messages?
The example I found in MSDN works for me but it downloads only the first
chunck of data sent by the server I connect to. How can i make the code wait
until the end of the message is received from the server?
Changing the buffer size won't change anything as i guess the stream.Read()
method returns after the first bunch of data is received from the server. I
don't really know how many packets is that neither why it returns before
everything is read but i guess that i need to add in some infrastructure
that will do some waiting and / or looping and which will detect when all
the data is read and that i can exit.
I have done some research on the internet and the example i saw often did
not care for this kind of longer message or were too complex or not working
if not communicating with their own server implementation. I have no access
on the server I connect to then I cannot change anything on that side.
Here is the code i use from MSDN doc (TCPClient class):
static void Connect(String server, String message)
{
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 13000;
TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
Console.WriteLine("\n Press Enter to continue...");
Console.Read();
}
Anyone can help me or point me to an adequate resource?Thanks a lot,Francois
Malgreve