private static void SendCallBack(IAsyncResult ar) { Socket handler = (Socket)ar.AsyncState; int bytesSent = handler.EndSend(ar); Console.WriteLine("Sent {0} bytes to client.", bytesSent); handler.Shutdown(SocketShutdown.Both); handler.Close(); StartBoth._TCPlistening = false; StartBoth.ListeningMessage(); }
/// <summary> /// Goes here after receiving a signal /// </summary> public static void ReadCallBack(IAsyncResult res) { UdpClient client = (UdpClient)res.AsyncState; IPEndPoint RemoteIPEndPoint = new IPEndPoint(IPAddress.Any, listenPort); Console.WriteLine("----------------------"); Console.WriteLine("UDP Connection made. Receiving data..."); byte[] received = client.EndReceive(res, ref RemoteIPEndPoint); listenPort = ((IPEndPoint)client.Client.LocalEndPoint).Port; Console.WriteLine("Data received from: {0} at Port: {1}", RemoteIPEndPoint.Address.ToString(), listenPort.ToString()); Console.WriteLine("Sending back changed position for debugging/testing purposes..."); using (MemoryStream ms = new MemoryStream()) { ms.Write(received, 0, received.Length); ms.Position = 0; if (listenPort == 8888) { replyPort = 9999; MessageParser<RobotStructure> parser = new MessageParser<RobotStructure>(() => new RobotStructure()); receivedStructure = parser.ParseFrom(ms); receivedStructure.RootJointID = 1; ms.Position = 0; SendBackStructure(receivedStructure); } else if (listenPort == 7777) { replyPort = 6666; MessageParser<PositionList> parser = new MessageParser<PositionList>(() => new PositionList()); receivedList = parser.ParseFrom(ms); receivedList.PList[0].Rotation = 30; ms.Position = 0; SendBackPositions(receivedList); } else if (listenPort == 1234) { replyPort = 4321; MessageParser<MeshList> parser = new MessageParser<MeshList>(() => new MeshList()); receivedMeshes = parser.ParseFrom(ms); Console.WriteLine("RECEIVED MESHES... LENGTH = {0}", receivedMeshes.Meshes.Count); StartBoth.ListeningMessage(); StartBoth._MeshListening = false; } } }
public static void SendBackPositions(PositionList received) { using (MemoryStream ms = new MemoryStream()) { received.WriteTo(ms); Socket tempSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint replyEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), replyPort); ms.Position = 0; tempSocket.SendTo(received.ToByteArray(), replyEndPoint); tempSocket.Close(); } Console.WriteLine("Data sent!"); StartBoth.ListeningMessage(); StartBoth._Positionlistening = false; }