UDP UWP server + Mono2x client - c#

I have a really weird issue with communication between server and client using UDP protocol. Client is written in Mono2x (I use Unity 3D as my client) and creates UdpClient class instance:
_udpClient = new UdpClient(9050);
_serverEP = new IPEndPoint(IPAddress.Parse(_serverIp), _serverPort);
My server is UWP application I want to run on Raspberry Pi that is using DatagramSocket:
_udpServer = new DatagramSocket();
_udpServer.MessageReceived += ClientCheck;
await _udpServer.BindServiceNameAsync(port.ToString());
I send data from client to server but with no luck. I checked with TCPView that data is send from my client application but never reaches the server. And now is the weird part. When I receive message from server first (I hardcode port to client), my client is able to send data with a success.
I am using same IPEndPoint to send data from client without any changes after receiving packet from server, it just starts working. Honestly, I have no idea what I could do wrong, so I will be thankful for any advice.

And now is the weird part. When I receive message from server first (I hardcode port to client), my client is able to send data with a success
This is a known issue which is filed based on this related question: https://stackoverflow.com/a/39767527/5254458
It includes the issue's description and temporary workaround.
The corresponding is team is investigating it and I can not guarantee that when the fix will be delivered.

Related

How to send message from server back to client in C# WebSocket?

I am a junior programmer and it is my first time building a WebSocket application in C#.
I have been following the tutorial from this page:
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_server
By sending messages from the client to the server, everything works well. However, I want to have a bidirectional connection, so that the server could send messages back to the client in order to see them in the browser.
I have tried the following method, by adding the following lines to the server code:
byte[] tosend = Encoding.ASCII.GetBytes("Message received by server");
stream.Write(tosend, 0, tosend.Length);
I thought the client would receive this message but unfortunately, it return an undefined error and closes the connection.
Could you explain why this method of sedning messages doesn't work and maybe give me a few suggestions?
Thanks!

Connection specific AppendShutdownHandler

I'm using NetworkComms.Net and I'm currently trying to create a sort of forwarder which listens on a port (calling it GATEWAY), which receive a certain packet (from CLIENT) which will tell him where to redirect next pacekts coming from the same client.
Example:
CLIENT tell GATEWAY that he needs to get to SERVER:serverport
GATEWAY creates a connection to SERVER:serverport
CLIENT sends packets to GATEWAY which sends them to SERVER
SERVER sends back a response, which goes through the GATEWAY to the CLIENT.
I had it working with Net.Sockets and I’m now changing to NetworkComms.
The problem I’m facing is that when a connection from Gateway (client) to Server (listening server) is closed, the Gateway trigger the global callbacks for connection/disconnection:
NetworkComms.AppendGlobalConnectionCloseHandler(ClientDisconnected);
NetworkComms.AppendGlobalConnectionEstablishHandler(ClientConnected);
Those callbacks are supposed to be only called when a client connect or disconnect on the Gateway
This is the code i’m currently using to start the listener.
NetworkComms.DefaultSendReceiveOptions = new SendReceiveOptions<ProtobufSerializer, , LZMACompressor>();
NetworkComms.DefaultSendReceiveOptions.IncludePacketConstructionTime = NetworkComms.DefaultSendReceiveOptions.ReceiveHandlePriority = QueueItemPriority.AboveNormal;
NetworkComms.AppendGlobalConnectionCloseHandler(SessionClosed);
NetworkComms.AppendGlobalConnectionEstablishHandler(NewSessionConnected);
NetworkComms.AppendGlobalIncomingPacketHandler<string>("ECHO", SocketCommands.HandleIncomingECHOPacket);
Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 10000));
I suppose this is like a “global” way of starting It?
How do I start one then add those Connected/Disconnected handlers to just the listener?
Thanks

Listen for certain IP address and port with tcplistener

I'm creating a screen sharing application. To allow for nat traversal I'm having to do this through a server, I'm just wondering how I can check that the client trying to connect to the server is the one I want to connect and not someone else, whilst not actually blocking other people from being able to connect to that port.
I'm thinking of using BeginAcceptTcpClient() instead of AcceptTcpClient() is there a way to check who is trying to connect before it accepts the connection this way so it can be picked up by the TcpListener later?
The only other thing I can of is to send the ID of the person I want to send the image to in the connection and let the server handle who to send it to but would that be too slow or resource intensive?
Maybe you should take a look on how join.me makes it:
On first machine start the Observable.
This connects to the central server and receives a unique id (ticket) that will be shown to the user.
On the second machine start the Observer.
This connects also to the central server and awaits an entering of the desired id to connect to.
Now the user on the observable machine has to communicate the id to the observer guy through a different channel (e.g. phone).
On the observer the ticket number is entered and the server can send both machines the ip address and port of each other where their did the hole punching. If a direct connect is possible, the central server has finished its work, otherwise both sides will send their data to the server and from their to the other client.
So to ensure that the correct clients are connected simply create a unique id which both sides have to share through a different communication channel.
I think the following should be enough to see if the correct client has connected to your server:
Create a TCPListener and start accepting connections either synchronously or async. I'm using the synchronous example from MSDN: http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.accepttcpclient(v=vs.110).aspx
When a client connects, create a stream to read data sent by the client like this:
NetworkStream stream = client.GetStream();
i = stream.Read(bytes, 0, bytes.Length);
Deserialize the message from bytes to string and check if it, for example, matches a unique ID that you are expecting:
`
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
//There are, of course, better ways of doing this.
if(data.Equals("HandshakeId")
{
//start receiving/sending data in a loop
}
else
{
// Shutdown and end connection
client.Close();
}
`
A more interesting question is how to capture the actual screen you'll be sharing. I personally have done this in VS C++, and the performance was okay. There are several ways to do it, i.e. using GDI+, using DirectX or OpenGL, the list goes on..
I'm curious as to what are you planing to use :)

How to listen to a TCP port which is already being listened by another app

I've a plugin which always listening to the port 6002, and i have an ASP.net application which sending messages to the same port and receiving the reply from the plugin on the same port,
Sending is working fine, and the plugin sends a reply on the same port but i don't know how to catch this reply, when i try to listen to the same port using Tcplistener the start method throws this exception : Only one usage of each socket address (protocol/network address/port) is normally permitted,
is there any way to catch the received message
Thanks
It sounds like you are wrongly assuming that the Socket which you get from TcpListener.AcceptSocket can only be used in one direction.
Sockets can actually be bidirectional. You can use Send to send something, and Receive to listen for get the replies. Open one socket, and then use it for both sending and receiving.
In short, no.
Once a port is opened an exception will be thrown if further attempts are made to utilise that same port from a different source - as you are experiencing right now. There isn't a way to get around this.
I've solved this problem using this way ,, I know it's old method but it's working !! :
'/*Variables Initialization*/
dim objSocket, strServicePort, strIpAddr, strResult, strMsgTo, strMsgResponse
strServicePort = "6002"
strIpAddr = "127.0.0.1"
'/* Create a TCP/IP socket. */
objSocket = Server.CreateObject("Intrafoundation.TCPClient.3")
objSocket.ClearLog()
'/* Establish socket connection. */
objSocket.Open (strIpAddr,strServicePort)
objSocket.Timeout=60.0
strMsgTo ="---- Message here ----"
'/* Send request message to plugin */
objSocket.SendRN(strMsgTo)
'/* receive XML Request Message from plugin */
strMsgResponse = objSocket.Recv()
strMsgResponse = Replace(strMsgResponse, vbLf, "")
objSocket.Close()
If you want to inspect traffic you can use winpcap.
edit: I don't think you are asking the right question. In this case the plugin is the server (listening on port 6002) and your ASP.net app is the client listening on some arbitrary port. You only need to bind to a different port in your ASP.net app if it also needs to run as a server with the plugin acting s the client. In this case you should pick a different port even though there are, in fact, ways to make it work when they are both bound to the same port.
In your case though you should just read back responses from the connection you established from the client.

C# - WireShark detects incomming packets but application does not receive them

I've got a strange problem. I have a client sending packets to my server, but my servers UDP socket never receives them. Same thing happens the other way around, if I send and he tries to receive.
Check this image, captured from wireshark:
http://img263.imageshack.us/img263/2636/bokus.png
I hav bound my UDP socket to EndPoint 192.168.2.3, which is the internal IP of the server, and port 9998.
The client sends data to my IP, which should then be forwarded to the local server machine..
As you can see wireshark clearly detects incomming packets for 192.168.2.3 with destination port 9998 but nothing gets delivered!
(...why does it say distinct32 btw in destination port?)
Something else to watch for is make sure any firewall you might running has a rule setup to allow communications on your port 9998.
If I had to guess (would need to see your recieving C# code to know), It looks like you might be trying to receive UDP packets but using TCP protocol on the client side. (Or i might just be misunderstanding some of the text of your screenshot.)
Theres no need to 'listen' for a connection when using UDP. UDP packets don't have a connect/disconnect protocol. Nor do they guarantee that packets are received in the same order they're sent.
try using something along these lines in your C# client and see if you get data.
var udpClient = new System.Net.Sockets.UdpClient(9998);
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);

Categories