Reputation: 93
Hi I am trying to send a string from a client to a server. It does not work so I am trying to test the functions below by sending data and receiving data on the same machine.
The problem that I encounted was that _serverStream.DataAvailable
in ReceiveData()
always returns false. How do I check that both functions are working correctly? Is the _serverStream
unable to send/receive data?
public void SendData(string dataToSend)
{
if (string.IsNullOrEmpty(dataToSend))
return;
_serverStream = _serverClient.GetStream();
byte[] outStream = Encoding.UTF8.GetBytes(dataToSend);
_serverStream.Write(outStream, 0, outStream.Length);
//_serverStream.Flush();
}
public string ReceiveData()
{
StringBuilder message = new StringBuilder();
_serverStream = _serverClient.GetStream();
_serverStream.ReadTimeout = 100;
while (true)
{
if (_serverStream.DataAvailable)
{
int read = _serverStream.ReadByte();
if (read > 0)
{
message.Append((char)read);
}
else
{
break;
}
}
else if (message.ToString().Length > 0)
{
break;
}
}
return message.ToString();
}
I call the SendData()
and ReceiveData()
as below:
tcpclient.SendData(username);
string test = tcpclient.ReceiveData();
MessageBox.Show(test);
Upvotes: 4
Views: 8250
Reputation:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class Program
{
public static void Main()
{
// Create a new TCP listener
TcpListener listener = new TcpListener(IPAddress.Any, 12345);
// Start listening
listener.Start();
// Create a new thread to handle the client
Thread thread = new Thread(() =>
{
// Accept a client
TcpClient client = listener.AcceptTcpClient();
// Get the network stream
NetworkStream stream = client.GetStream();
// Receive a message
byte[] data = new byte[256];
int bytesRead = stream.Read(data, 0, data.Length);
string message = Encoding.ASCII.GetString(data, 0, bytesRead);
// Print the message
Console.WriteLine("Received message: " + message);
// Send a response
string response = "Hello, client!";
byte[] responseData = Encoding.ASCII.GetBytes(response);
stream.Write(responseData, 0, responseData.Length);
// Close the stream and client
stream.Close();
client.Close();
});
// Start the thread
thread.Start();
// Create a new TCP client
TcpClient client = new TcpClient();
// Connect to the server
client.Connect("localhost", 12345);
// Get the network stream
NetworkStream stream = client.GetStream();
// Send a message
string message = "Hello, server!";
byte[] data = Encoding.ASCII.GetBytes(message);
stream.Write(data, 0, data.Length);
// Receive a response
byte[] responseData = new byte[256];
int bytesRead = stream.Read(responseData, 0, responseData.Length);
string response = Encoding.ASCII.GetString(responseData, 0, bytesRead);
// Print the response
Console.WriteLine("Received response: " + response);
// Close the stream and client
stream.Close();
client.Close();
// Stop listening
listener.Stop();
}
}
Upvotes: 0
Reputation: 39095
Although it is not shown in your code, I'm guessing _serverClient
is a TcpClient.
In both methods, you get a network stream from the same _serverClient
, so you're accessing the exact same network stream. And it seems you expect that when you write data to the network stream in SendData
, you should be able to read that same data within ReceiveData
, which is not correct.
When you write data to a network stream, it is actually sent over the network -- it is not available to be read from the same network stream. You can only read data that you receive from the remote endpoint. In other words, unless you have a remote endpoint sending you back data, you won't have any DataAvailable
to read.
In ReceiveData
, you should be getting the network stream from the server end, e.g. from a TcpClient
which was returned by calling TcpListener.AcceptTcpClient
, not from the same TcpClient
that you used to send data.
Upvotes: 2