static void Main(string[] args) { byte[] bytes = new byte[1024]; // Connect to a remote device. try { // Establish the remote endpoint for the socket. // This example uses port 11000 on the local computer. string serverIP = args[0]; //"192.168.56.1"; string client2IP = args[1];//192.168.56.1"; IPAddress ipAddress = IPAddress.Parse(serverIP); IPEndPoint remoteEP = new IPEndPoint(ipAddress,1010); Console.WriteLine("Client1: I will create a socket object to talk to the server"); // Create a TCP/IP socket. Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); // Connect the socket to the remote endpoint. Catch any errors. try { sender.Connect(remoteEP); Console.WriteLine("Client1: Got connected to {0}",sender.RemoteEndPoint.ToString()); byte[] msg = Encoding.ASCII.GetBytes("This is Client1 saying hello!\n "); // Send the data through the socket.[ Console.WriteLine("Client1: Sending message to server"); int bytesSent = sender.Send(msg); Console.WriteLine("Client1: Message sent successfully! "); System.Net.IPHostEntry ipHostEntry = System.Net.Dns.GetHostEntry(client2IP); Process clientTwoProcess = Process.GetProcessesByName("Client2VM", ipHostEntry.HostName)[0]; SocketInformation sinfo = sender.DuplicateAndClose(clientTwoProcess.Id); IPAddress client2IPAddress = IPAddress.Parse(client2IP); remoteEP = new IPEndPoint(client2IPAddress, 2010); // Create a TCP/IP socket. Socket sender1 = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); sender1.Connect(remoteEP); Console.WriteLine("Client1: Let me send the socket object to Client2"); bytesSent = sender1.Send(sinfo.ProtocolInformation); sender1.Close(); Console.WriteLine("Client1: GoodBye!"); Console.ReadLine(); } catch (ArgumentNullException ane) { Console.WriteLine("ArgumentNullException : {0}",ane.ToString()); } catch (SocketException se) { Console.WriteLine("SocketException : {0}",se.ToString()); } catch (Exception e) { Console.WriteLine("Unexpected exception : {0}", e.ToString()); } } catch (Exception e) { Console.WriteLine( e.ToString()); } }
public override void Handle(Socket accepted) { var si = accepted.DuplicateAndClose(Process.GetCurrentProcess().Id); var ad = DomainHelper.Use(HandlerName); log.i($"Handling socket in domain: {AppDomain.CurrentDomain.FriendlyName}"); DomainHelper.Execute(ad, (args) => { var info = (SocketInformation)args[0]; ProcessSocket(info); }, si); }
SocketInformation Utils.Wrappers.Interfaces.ISocket.DuplicateAndClose(int targetProcessId) { return(InternalSocket.DuplicateAndClose(targetProcessId)); }
public static CipheredCrosSocket ConvertSocketToCipheredCrosSocket(Socket socket) { return new CipheredCrosSocket(socket.DuplicateAndClose(Process.GetCurrentProcess().Id)); }
public void acceptClientConnection(Socket oldsock) { int TimeOut = 0; string sEndPoint = ""; //CLIENT_COUNT++; Socket sock = new Socket(oldsock.DuplicateAndClose(Process.GetCurrentProcess().Id)); sEndPoint = sock.RemoteEndPoint.ToString(); sock.ReceiveTimeout = 25000; NetworkStream st = new NetworkStream(sock); StreamReader rd = null; bool Connected = true; bool valid = true; int base_cnt = 0; int keep_alive = 0; string current=""; int y = 0; string test = ""; while (sock.Connected) { if (st.DataAvailable) { rd = new StreamReader(st); test = rd.ReadLine(); if (test.IndexOf("MESSAGE") > -1) { string[] arr = test.Split('|'); Allarm(DateTime.Now.ToString(), arr[0], arr[2]); current = arr[0]; if (!Connessioni.ContainsKey(arr[0])) Connessioni.Add(arr[0], st); } if (test.IndexOf("GPS") > -1) { string[] arr = test.Split('|'); validGPSTablet(arr[2] + "," + arr[3] + "," + arr[4] + "," + arr[5], arr[0], DateTime.Now.ToString(), true); current = arr[0]; if (!Connessioni.ContainsKey(arr[0])) Connessioni.Add(arr[0], st); } if (test.IndexOf("ALLARME") > -1) { string[] arr = test.Split('|'); Allarm(DateTime.Now.ToString(), arr[0], arr[2]); current = arr[0]; if (!Connessioni.ContainsKey(arr[0])) Connessioni.Add(arr[0], st); } if (test.IndexOf("READY") > -1) { keep_alive--; string[] arr = test.Split('|'); current = arr[0]; if (!Connessioni.ContainsKey(arr[0])) Connessioni.Add(arr[0], st); } } else { Thread.Sleep(100); base_cnt++; if (base_cnt >= 100) { base_cnt = 0; StreamWriter wr = new StreamWriter(st); wr.Write("KEEP ALIVE"); wr.Flush(); keep_alive++; if (keep_alive >= 5) { Output("L'unità: " + current + " non risponde."); } } } } // Alla Disconnessione Connessioni.Remove(current); Output(current + " si è disconnesso."); }
public Socket(ns.Socket sock) : this(sock.DuplicateAndClose(Process.GetCurrentProcess().Id)) { }