C# socket StreamReader.ReadToEnd wait forever - c#

In an Universal Windows Platform (UWP) app, I'm trying to send an object to a Windows form program using socket:
public void CreateListener()
{
TcpListener listener = new TcpListener(IPAddress.Any, port);
listener.Start();
while (true)
{
if (listener.Pending())
{
var childSocketThread = new Thread(() =>
{
string data = null;
var Client = listener.AcceptTcpClient();
using (NetworkStream ns = Client.GetStream())
using (StreamReader sr = new StreamReader(ns))
{
data = sr.ReadToEnd();
var Items = JsonConvert.DeserializeObject<Models.Orders>(data);
Process(Items);
ns.Flush();
ns.Close();
}
});
childSocketThread.IsBackground = true;
childSocketThread.Start();
}
else
{
Thread.Sleep(50); //<--- timeout
}
}
}
But receiving object stuck in ReadToEnd and never exit.
What should I do?

Seems you have problem when you want to send data, after flushing your StreamWriter object you have to use StreamReader as a response.
Stream streamIn = socket.InputStream.AsStreamForRead();
StreamReader reader = new StreamReader(streamIn);
string response = await reader.ReadLineAsync();
Hope this would help you.

Related

binary formatter deserialize from memory stream freezing

I am trying to do a client-server application that communicate through sockets and their messages are serialez and deserialised using BinaryFormatter. My code is freezing and does absolutely nothing when reaching deserialize, and I don't understand why since I have no exception. I also can not step into with debugger, everything is freezing. This is my code:
public class Serializer
{
public static MemoryStream ToStream(object obj)
{
var stream = new MemoryStream();
var formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
return stream;
}
public static object FromStream(MemoryStream stream)
{
Console.WriteLine("Starting from stream");
var formatter = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin);
object rez = formatter.Deserialize(stream); //NEVER GOES OVER THIS
Console.WriteLine("Starting deserialization" + rez);
return formatter.Deserialize(stream);
}
}
public class Connection
{
private Socket socket;
public Connection(Socket socket)
{
this.socket = socket;
Console.WriteLine($"Connected to client: {socket.RemoteEndPoint}");
Task.Factory.StartNew(() => Execute(socket));
}
private void Execute(Socket socket)
{
while (true)
{
var buffer = new byte[2048];
var bytesCount = socket.Receive(buffer);
if(bytesCount != 0)
{
var msgReceived = (Message)Serializer.FromStream(new MemoryStream(buffer, 0, buffer.Length));
Console.WriteLine($"Received msg: {msgReceived.Content}");
}
/* var msg = new Message { Content = "Hello World2!" };
Console.WriteLine($"Sending msg with content: {msg.Content}");
MemoryStream stream = Serializer.ToStream(msg);
var bytesSent = socket.Send(stream.GetBuffer());*/
Console.WriteLine("Trying again");
Thread.Sleep(500);
}
}
client code:
var host = Dns.GetHostEntry("localhost");
var ipAddress = host.AddressList.First();
var serverEndpoint = new IPEndPoint(ipAddress, 9000);
Socket serverSocket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Connect(serverEndpoint);
Console.WriteLine($"Successfully connected to server on: {serverSocket.RemoteEndPoint}");
while (true)
{
var msg = new Message { Content = "Hello World!" };
Console.WriteLine($"Sending msg with content: {msg.Content}");
MemoryStream stream = Serializer.ToStream(msg);
var bytesSent = serverSocket.Send(stream.GetBuffer());
Console.WriteLine("Waiting to receive");
var buffer = new byte[2048];
int bytesReceived = serverSocket.Receive(buffer);
if (bytesReceived != 0)
{
var receivedMessage = (Message)Serializer.FromStream(new MemoryStream(buffer));
Console.WriteLine($"Received message: {receivedMessage.Content}");
}
Console.WriteLine("Received done");
}
server code :
var host = Dns.GetHostEntry("localhost");
var ipAddress = host.AddressList.First();
var localEndPoint = new IPEndPoint(ipAddress, 9000);
var serverSocket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(localEndPoint);
serverSocket.Listen(1);
while (true)
{
Console.WriteLine("Waiting for client");
Socket clientSocket = serverSocket.Accept();
var connection = new Connection(clientSocket);
}
I also checked the byteCount and they are arriving to the server, also the buffer is not empty, I don't understand why deserialize does nothing..
You call Deserialize twice, I think problem here:
object rez = formatter.Deserialize(stream); //NEVER GOES OVER THIS
Console.WriteLine("Starting deserialization" + rez);
return formatter.Deserialize(stream);
I think you can use methods like that:
private byte[] SerializeMessage(Message msg)
{
var formatter = new BinaryFormatter();
byte[] buf;
using (MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, msg);
buf = new byte[stream.Length];
return stream.ToArray();
}
}
private Message DeserializeMessage(byte[] buff)
{
var formatter = new BinaryFormatter();
ConnectingMessage msg;
using (Stream stream = new MemoryStream(buff))
{
msg = formatter.Deserialize(stream) as Message;
}
return msg;
}
Also methods Send/Receive are synchronous, they block thread execution.
The description of the asynchronous option is here https://learn.microsoft.com/en-us/dotnet/framework/network-programming/using-an-asynchronous-client-socket
Only one piece of code can run at the same time. While that infinute loop while (true) runs, no other code will ever be able to run.
You will need to add some Multitasking to this project. The minimum amount, that is required for every word processor. However you also seem to be programming in console. And from my experience, console and Multitasking do not mix that well. You need to keep the programm alive, without blocking input.
My advice for learning Multitasking, is a BackgroundWorker in Windows Forms. The BGW is dated, usese Threads wich would be unessary here and should not be used in productive code. But it is perhaps the best "Training Wheels" for multitasking and -threading I know off.

UWP app cannot receive, only send, using sockets

I'm currently developing an UWP app which should have capability to be as a TCP server (using ports) so client can connect to it via other device and send requests and server responds with data.
I followed the Socket example on :Microsoft site, and got sample code working (in which server and client are both in same app)
I changed IP addresses and ports so i could use apps on 2 different machines with direct connection, I also made separate simple client application, using sample code from Here
Now problem is as follows: UWP app can successfully communicate with its own client method provided by Microsoft's sample, but is unable to communicate with console client program I made and was running on other. UWP can indeed connect with client and also send data, but it cannot receive data, the function streamReader.ReadLineAsync(); will wait infinitely long and that's all.
How do i make UWP app get the message client is sending and what i might be doing wrong ?
public sealed partial class MainPage : Page
{
static string PORT_NO = "1300";
const string SERVER_IP = "192.168.0.10";
public MainPage()
{
this.InitializeComponent();
outputText.Text = "Helloo";
StartConnection(SERVER_IP, PORT_NO);
//StartClient();
}
public async void StartConnection(string net_aadress, string port_nr)
{
try
{
var streamSocketListener = new StreamSocketListener();
// The ConnectionReceived event is raised when connections are received.
streamSocketListener.ConnectionReceived += this.StreamSocketListener_ConnectionReceived;
// Start listening for incoming TCP connections on the specified port. You can specify any port that's not currently in use.
await streamSocketListener.BindServiceNameAsync(port_nr);
outputText.Text = "server is listening...";
}
catch (Exception ex)
{
Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult);
outputText.Text = (webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message);
}
}
private async void StreamSocketListener_ConnectionReceived(Windows.Networking.Sockets.StreamSocketListener sender, Windows.Networking.Sockets.StreamSocketListenerConnectionReceivedEventArgs args)
{
string request = "password";
string second;
/*
using (var streamReader = new StreamReader(args.Socket.InputStream.AsStreamForRead()))
{
request = await streamReader.ReadLineAsync();
}
*/
//await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.serverListBox.Items.Add(string.Format("server received the request: \"{0}\"", request)));
// Echo the request back as the response.
using (Stream outputStream = args.Socket.OutputStream.AsStreamForWrite())
{
using (var streamWriter = new StreamWriter(outputStream))
{
await streamWriter.WriteLineAsync(request);
await streamWriter.FlushAsync();
}
}
using (var streamReader = new StreamReader(args.Socket.InputStream.AsStreamForRead()))
{
second = await streamReader.ReadLineAsync();
}
using (Stream outputStream = args.Socket.OutputStream.AsStreamForWrite())
{
using (var streamWriter = new StreamWriter(outputStream))
{
await streamWriter.WriteLineAsync(second);
await streamWriter.FlushAsync();
}
}
//await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.serverListBox.Items.Add(string.Format("server sent back the response: \"{0}\"", request)));
sender.Dispose();
//await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => this.serverListBox.Items.Add("server closed its socket"));
}
private async void StartClient()
{
try
{
// Create the StreamSocket and establish a connection to the echo server.
using (var streamSocket = new Windows.Networking.Sockets.StreamSocket())
{
// The server hostname that we will be establishing a connection to. In this example, the server and client are in the same process.
var hostName = new Windows.Networking.HostName("localhost");
//this.clientListBox.Items.Add("client is trying to connect...");
await streamSocket.ConnectAsync(hostName, PORT_NO);
//this.clientListBox.Items.Add("client connected");
// Send a request to the echo server.
string request = "Hello, World!";
using (Stream outputStream = streamSocket.OutputStream.AsStreamForWrite())
{
using (var streamWriter = new StreamWriter(outputStream))
{
await streamWriter.WriteLineAsync(request);
await streamWriter.FlushAsync();
}
}
//this.clientListBox.Items.Add(string.Format("client sent the request: \"{0}\"", request));
// Read data from the echo server.
string response;
using (Stream inputStream = streamSocket.InputStream.AsStreamForRead())
{
using (StreamReader streamReader = new StreamReader(inputStream))
{
response = await streamReader.ReadLineAsync();
}
}
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
outputText.Text = "Client got back " + response;
}
);
}
//this.clientListBox.Items.Add("client closed its socket");
}
catch (Exception ex)
{
Windows.Networking.Sockets.SocketErrorStatus webErrorStatus = Windows.Networking.Sockets.SocketError.GetStatus(ex.GetBaseException().HResult);
//this.clientListBox.Items.Add(webErrorStatus.ToString() != "Unknown" ? webErrorStatus.ToString() : ex.Message);
}
}
}
Here is source code for Client application:
{
class Program
{
const int PORT_NUMBER = 1300;
const string SERVER_IP = "192.168.0.10";
static void Main(string[] args)
{
string textToSend = DateTime.Now.ToString();
string password = "Madis on loll";
string receiveddata;
try
{
Console.WriteLine("Client progrm started");
TcpClient client = new TcpClient(SERVER_IP, PORT_NUMBER);
NetworkStream nwStream = client.GetStream();
//System.Threading.Thread.Sleep(500);
//see, how long is packet
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
byte[] password2 = ASCIIEncoding.ASCII.GetBytes(password);
Console.WriteLine("Sending : " + password);
nwStream.Write(password2, 0, password2.Length); //sending packet
byte[] receiveddata2 = new byte[client.ReceiveBufferSize];
int receiveddatalength = nwStream.Read(receiveddata2, 0, client.ReceiveBufferSize);
Console.WriteLine("Received : " + Encoding.ASCII.GetString(receiveddata2, 0, bytesRead));
}
catch (Exception ex)
{
Console.WriteLine("Connection error");
}
}
}
}
Found answer myself: main problem is with ReadLineAsync() in Server program: it waits and collects all the stream until it gets end of line character. In this case end of line was never sent and therefore server kept waiting infinitely.
Simplest fix was on Client side by simply adding end of line at the end of packet, like this:
before:
byte[] password2 = ASCIIEncoding.ASCII.GetBytes(password);
Console.WriteLine("Sending : " + password);
nwStream.Write(password2, 0, password2.Length); //sending packet
after:
byte[] newLine = Encoding.ASCII.GetBytes(Environment.NewLine);
byte[] password2 = ASCIIEncoding.ASCII.GetBytes(password);
Console.WriteLine("Sending : " + password);
nwStream.Write(password2, 0, password2.Length); //sending packet
nwStream.Write(newLine,0,newLine.Length);
Also 1 thing worth mentioning: current StreamSocketListener_ConnectionReceived is able to send only once, then it sets outputStream.CanWrite to false.
This can be solved by removing using() from writing and reading functions, like this:
before:
PS! Manually flushing is also replaced with autoflush.
using (Stream outputStream = args.Socket.OutputStream.AsStreamForWrite())
{
using (var streamWriter = new StreamWriter(outputStream))
{
await streamWriter.WriteLineAsync(request);
await streamWriter.FlushAsync();
}
}
after:
Stream outputStream = args.Socket.OutputStream.AsStreamForWrite();
var streamWriter = new StreamWriter(outputStream);
streamWriter.AutoFlush = true;
await streamWriter.WriteLineAsync(request);
Hope it helps someone someday.

TcpListener not accepting client

I have this web server I tried to make using TcpListener, but it won't accept about half of the requests I made. It simply hangs on AcceptTcpClient when the request is made, also it behaves the same on any port.
this.main = new Thread(() =>
{
while (this.running)
{
TcpClient c = this.t.AcceptTcpClient();
new Task(() =>
{
handleClient(c);
}).Start();
}
});
this.main.Start();
Maybe you can try something like this:
public void Start()
{
Console.WriteLine("Server started...");
TcpListener listener = new TcpListener(System.Net.IPAddress.Loopback, 1234);
listener.Start();
while (true)
{
TcpClient client = listener.AcceptTcpClient();
new Thread(new ThreadStart(() =>
{
HandleClient(client);
})).Start();
}
}
private void HandleClient(TcpClient client)
{
NetworkStream stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream, Encoding.ASCII) { AutoFlush = true };
StreamReader reader = new StreamReader(stream, Encoding.ASCII);
string inputLine = reader.ReadLine();
Console.WriteLine("The client with name " + " " + inputLine + " is conected");
}

TcpListener reading request returning -1

I'm using TcpListener like this:
TcpListener _listener;
public void Start()
{
_listener = new TcpListener(IPAddress.Any, 8000);
_listener.Start();
_listener.BeginAcceptSocket(this.HandleAcceptTcpClient, _listener);
}
private void HandleAcceptTcpClient(IAsyncResult result)
{
TcpClient client = _listener.EndAcceptTcpClient(result);
_listener.BeginAcceptTcpClient(HandleAcceptTcpClient, _listener);
Console.WriteLine("Accepting connection...");
NetworkStream stream = client.GetStream();
using (StreamWriter writer = new StreamWriter(stream, Encoding.ASCII))
using (StreamReader reader = new StreamReader(stream, Encoding.ASCII))
{
char c;
while ((c = (char)reader.Read()) != 0)
{
Console.Write(c);
}
Console.WriteLine();
}
}
When I make a request to localhost:8000 in Chrome, I the following response in the console (the ?'s are endless):
And when I change the code inside HandleAcceptTcpClient to this (changed it to print ints instead of chars):
private void HandleAcceptTcpClient(IAsyncResult result)
{
TcpClient client = _listener.EndAcceptTcpClient(result);
_listener.BeginAcceptTcpClient(HandleAcceptTcpClient, _listener);
Console.WriteLine("Accepting connection...");
NetworkStream stream = client.GetStream();
using (StreamWriter writer = new StreamWriter(stream, Encoding.ASCII))
using (StreamReader reader = new StreamReader(stream, Encoding.ASCII))
{
// the changes are on the next 5 lines
int i;
while ((i = reader.Read()) != 0)
{
Console.Write(i);
}
Console.WriteLine();
}
}
I get the following:
So basically, the browser seems to be making another request and sending only -1; also, doing reader.ReadToEnd() just hangs, meaning it's endless.
Any ideas?
It is not Chrome sending another request. It seems to be Stream.ReadByte method that returns -1 since it reaches end of the stream.
MSDN:
Return Value: The unsigned byte cast to an Int32, or
-1 if at the end of the stream.
So the output makes sense if StreamReader uses the Stream.ReadByte method of the input stream to read data from it.

TCP error on reply

I created a simple network game that uses TCP sockets in order to get the other player's data. I have two different classes, a server and a client. Everything was perfectly fine when I was only sending one message at a time like so:
Client:
public void ClientTester()
{
thread = new Thread(SendPosition);
thread.Start();
}
private void SendPosition()
{
while (true)
{
using (TcpClient client = new TcpClient())
{
client.Connect("127.0.0.1", 82);
using (NetworkStream n = client.GetStream())
{
BinaryWriter w = new BinaryWriter(n);
w.Write("Position:" + GameFiles.Player.Player.position.ToString());
w.Flush();
string msg = new BinaryReader(n).ReadString();
parseString(msg, "Position:", 9);
}
}
Thread.Sleep(60);
}
}
Server:
public void ServerTester()
{
thread = new Thread(TheadedMethod);
thread.Start();
}
private void TheadedMethod()
{
while (true)
{
TcpListener listener = new TcpListener(IPAddress.Any, 82);
listener.Start();
using (TcpClient c = listener.AcceptTcpClient())
using (NetworkStream n = c.GetStream())
{
parseString(msg, "Position:", 9);
BinaryWriter w = new BinaryWriter(n);
w.Write("Position:" + GameFiles.Player.Player.position.ToString());
w.Flush();
}
listener.Stop();
}
}
Here is the new code:
Client:
public void ClientTester()
{
thread = new Thread(SendPosition);
thread.Start();
SendMousePosition();
}
private void SendPosition()
{
while (true)
{
using (TcpClient client = new TcpClient())
{
client.Connect("127.0.0.1", 82);
using (NetworkStream n = client.GetStream())
{
BinaryWriter w = new BinaryWriter(n);
w.Write("Position:" + GameFiles.Player.Player.position.ToString());
w.Flush();
string msg = new BinaryReader(n).ReadString();
parseString(msg, "Position:", 9);
}
}
Thread.Sleep(60);
}
}
private void SendMousePosition()
{
using (TcpClient client = new TcpClient())
{
client.Connect("127.0.0.1", 82);
using (NetworkStream n = client.GetStream())
{
BinaryWriter w = new BinaryWriter(n);
w.Write("MousePosition:" + cursor.mousePosition());
w.Flush();
string msg = new BinaryReader(n).ReadString();
parseString(msg, "MousePosition:", 14);
}
}
}
Server:
public void ServerTester()
{
thread = new Thread(TheadedMethod);
thread.Start();
}
private void TheadedMethod()
{
while (true)
{
TcpListener listener = new TcpListener(IPAddress.Any, 82);
listener.Start();
using (TcpClient c = listener.AcceptTcpClient())
using (NetworkStream n = c.GetStream())
{
string msg = new BinaryReader(n).ReadString();
if (msg == "Position:")
{
parseString(msg, "Position:", 9);
BinaryWriter w = new BinaryWriter(n);
w.Write("Position:" + GameFiles.Player.Player.position.ToString());
w.Flush();
}
if (msg == "MousePosition:")
{
parseString(msg, "MousePosition:", 14);
BinaryWriter w = new BinaryWriter(n);
w.Write("MousePosition:" + cursor.mousePosition());
w.Flush();
}
}
listener.Stop();
}
}
When I try to send two messages in I receive an error:
Unable to read beyond the end of the stream.
on this line from the client's method SendPosition():
string msg = new BinaryReader(n).ReadString();
Why doesn't this work even though I have created a new instance of BinaryReader? Shouldn't the server automatically respond to each message sent to it?
You are doing two things wrong: The fist is that you create and re-create the connection all the time. Instead create the listener once, enter the loop and read messages. Setting up a new connection in TCP ads a lot of overhead, especially if you're just sending small messages. Same thing in the client, connect once, and then send when needed.
The second problem is that TCP is a streaming protocol, and that there is no message boundaries. That means that when you read from a TCP connection you can't know beforehand how much data you will read, you need to provide a way to separate messages yourself: You can add message boundaries, you can prepend each message with a header containing the message size, of you have have all messages being the same fixed size. Either way, you might have to read multiple times to get a complete message, or one read could give you more than one message.
Regarding the second problem, you can't of course attempt to read more than have been received, which is probably what the error message is telling you.

Categories

Resources