I am having problems getting a TCP server to connect. I tried to pull apart the sample and thought I had everything correct. For some reason the server never accepts the incoming connection.
Code:
public async void StartServer()
{
StreamSocketListener listener = new StreamSocketListener();
listener.ConnectionReceived += OnConnection;
listener.Control.KeepAlive = true;
try
{
await listener.BindServiceNameAsync(RX_Connection_Port.Text);
}
catch
{
}
}
private async void OnConnection(
StreamSocketListener sender,
StreamSocketListenerConnectionReceivedEventArgs args)
{
DataReader reader = new DataReader(args.Socket.InputStream);
try
{
while (true)
{
// Read first 4 bytes (length of the subsequent string).
uint sizeFieldCount = await reader.LoadAsync(sizeof(uint));
if (sizeFieldCount != sizeof(uint))
{
// The underlying socket was closed before we were able to read the whole data.
return;
}
// Read the string.
uint stringLength = reader.ReadUInt32();
uint actualStringLength = await reader.LoadAsync(stringLength);
if (stringLength != actualStringLength)
{
// The underlying socket was closed before we were able to read the whole data.
return;
}
string recieved = reader.ReadString(actualStringLength);
SynthPlayback(recieved);
}
}
catch (Exception exception)
{
// If this is an unknown status it means that the error is fatal and retry will likely fail.
if (Windows.Networking.Sockets.SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
}
}
Any ideas to what I missed. The ipaddress and port are correct in the connection?
Update:
Added a text block to see the error
public async void StartServer()
{
StreamSocketListener listener = new StreamSocketListener();
listener.ConnectionReceived += OnConnection;
listener.Control.KeepAlive = true;
try
{
await listener.BindServiceNameAsync(RX_Connection_Port.Text);
}
catch (Exception exception)
{
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
ErrorReport.Text = "Start listening failed with error: " + exception.Message;
}
}
Result: Start listening failed with error: The specified class was not found.
not sure what is happening. Any ideas?
Update: Narrowing this down. Got the first problem fixed and now have another.When the client connects to the listener it immediately starts to process before data has come in. I need to wait for data to come in not start on the connection. I am basing this off of this block of code:
while (true)
{
uint sizeFieldCount = await reader.LoadAsync(sizeof(uint));
if (sizeFieldCount != sizeof(uint))
{
// The underlying socket was closed before we were able to read the whole data.
return;
}
// Read the string.
uint stringLength = reader.ReadUInt32();
uint actualStringLength = await reader.LoadAsync(stringLength);//This is where the code breaks.
if (stringLength != actualStringLength)
{
// The underlying socket was closed before we were able to read the whole data.
return;
}
ErrorReport.Text = String.Format("Received data: \"{0}\"", reader.ReadString(actualStringLength));
}
maybe I need to do this differently. I just need a server to sit there and listen to incoming data. Then read the data to a string for use later. I have looked through so many examples my head is spinning. I am in overload. Any one have a snippit of working server class?
catch
block of theStartServer
method to see if its blowing up there? – tobyd Oct 23 '17 at 13:17OnConnection
and then just do a simple request. It should hit the break point. If it doesnt it means there could be a firewall problem. Also the HTTP Result from the client can also contain more information, expsiecailly if its 4xx - 404 not found, 405 security exception, and others. codeproject.com/articles/1079341/webcontrols See if that helps sort something out? and actual IoT examples here github.com/Microsoft/Windows-universal-samples/tree/master/… – Piotr Kula Oct 23 '17 at 13:59RX_Connection_Port.Text
is your likely culprit, if you swap that for an integer port (as you noticed) that should relieve thespecified class not found
exception. I can replicate the error on a Win10 UWP after finding out UWP sockets cannot be easily accessed locally, but are fine remotely... – tobyd Oct 23 '17 at 19:08