public static void OnConnectionAcept(SocketAsyncEventArgs arg) { SOCKET.Client client = new SOCKET.Client(); IPAddress address = arg.ReceiveMessageFromPacketInfo.Address; clients.Add(address.ToString(), client); client.socket = arg.AcceptSocket; client.StartReceiveAsync(OnReceiveAsync); }
public bool OnReceiveAsyncStream(SocketAsyncEventArgs arg) { if (arg.BytesTransferred > 4 && clientStream.pmIn.NewRead() == arg.BytesTransferred) { switch (clientStream.pmIn.ReadCommand()) { case Command.REMOTE_DESKTOP_CONNECT_STREAM_SUCCESS: { MainWindow.loader.ChangeText("Creating input stream..."); System.Net.IPAddress address = clientStream.ClientAddress; clientInput = new SOCKET.Client(); clientInput.socket = new System.Net.Sockets.Socket(new System.Net.IPEndPoint(address, Settings.s.remoteDesktopPort).AddressFamily, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); try { clientInput.socket.Connect(address, Settings.s.remoteDesktopPort); clientInput.StartReceiveAsync(OnReceiveAsyncInput); clientInput.socket.SendTimeout = 5000; clientInput.socket.ReceiveTimeout = 5000; clientInput.pmOut.Write(Command.REMOTE_DESKTOP_CONNECT_INPUT); clientInput.pmOut.End(); if (clientInput.SendSafe()) { //cw.WriteLine(Remote.Language.Find("ClientConnecting", this, "Connecting to {0}"), Address.Text); return(true); } else { //cw.WriteLine(clientInput.lastSendException.Message); clientInput.Close(); clientInput = null; return(false); } //cw.WriteLine(Remote.Language.Find("ClientConnected", this, "Succesfully conencted to {0}"), Address.Text); } catch (System.Net.Sockets.SocketException ee) { //cw.WriteLine(ee.Message); clientInput.Close(); clientInput = null; } } break; default: break; } } return(false); }
// Todo: Blocking call if failed to connect for about 10-15 sec private void Connect_Click(object sender, RoutedEventArgs e) { /*if (client != null) * { * cw.WriteLine(Remote.Language.Find("ClientNotNull", this, "A connection is already made!")); * return; * }*/ System.Net.IPAddress address; if (System.Net.IPAddress.TryParse(Address.Text, out address)) { SOCKET.Client client = new SOCKET.Client(); client.socket = new System.Net.Sockets.Socket(new System.Net.IPEndPoint(address, Settings.s.remoteDesktopPort).AddressFamily, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); try { client.socket.Connect(address, Settings.s.remoteDesktopPort); client.StartReceiveAsync(OnReceiveAsync); client.socket.SendTimeout = 5000; client.socket.ReceiveTimeout = 5000; client.pmOut.Write(Command.CONNECT); client.pmOut.Write(Command.USER_NAME); client.pmOut.Write(Settings.s.name); client.pmOut.End(); if (client.SendSafe()) { cw.WriteLine(Remote.Language.Find("ClientConnecting", this, "Connecting to {0}"), Address.Text); return; } else { cw.WriteLine(client.lastSendException.Message); client.Close(); client = null; } //cw.WriteLine(Remote.Language.Find("ClientConnected", this, "Succesfully conencted to {0}"), Address.Text); } catch (System.Net.Sockets.SocketException ee) { cw.WriteLine(ee.Message); client.Close(); client = null; } } }
public static void ProcessInput(ref string[] words) { switch (words[0].ToLower()) { case "connect": { if (!ArrayPositionCheck(1, words)) { Console.WriteLine("Error: No IP"); return; } SOCKET.Client client = CONNECT.Connect(ref words); if (client != null) { if (clients.ContainsKey(client.ClientAddress.ToString())) { Console.WriteLine("A connection to the following address is already made! -> {0}", client.ClientAddress.ToString()); client.socket.Disconnect(false); } else { client.StartReceiveAsync(OnReceiveAsync); clients.Add(client.ClientAddress.ToString(), client); Console.WriteLine("Succesfully connected to {0}!", client.ClientAddress.ToString()); } } } break; case "select": if (!ArrayPositionCheck(1, words)) { Console.WriteLine("Can't select nothing!"); return; } else { switch (words[1].ToLower()) { case "clients": { MyEnum temp; enumerators.TryGetValue("clients", out temp); if (temp != null) { Console.WriteLine("An enumerator named \"clients\" already exists!"); } else { enumerators.Add("clients", new MyEnum() { type = "clients", pos = 0, objects = clients.Keys.ToArray() }); Console.WriteLine("Enumerator created!"); } } break; default: break; } } break; case "next()": case "next": { if (!ArrayPositionCheck(2, words)) { Console.WriteLine("Invalid enumerator command!"); return; } else if (words[1] == "->") { MyEnum temp; enumerators.TryGetValue(words[2], out temp); if (temp != null) { temp.Next(); } else { Console.WriteLine("Invalid enumerator!"); } } else { Console.WriteLine("Invalid enumerator command!"); return; } } break; case "ping": if (!ArrayPositionCheck(1, words)) { Console.WriteLine("No IP or local channel, pinging default -> 192.168.1.*"); PING.ALL("1"); PING.ADD_NEW(ref computers); return; } else { int temp; if (int.TryParse(words[1], out temp)) { PING.ALL(words[1]); PING.ADD_NEW(ref computers); return; } else { Console.WriteLine("Error: Invalid channel"); } } break; case "get": if (!ArrayPositionCheck(1, words)) { Console.WriteLine("Error: No command!"); return; } switch (words[1].ToLower()) { case "ip": { foreach (IPAddress ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { Console.WriteLine(ip.ToString()); } } } break; default: break; } break; case "push": { List <PUSH.Message> m = new List <PUSH.Message>(); PushMain: Console.Write("> "); string pushInput = Console.ReadLine(); string[] pushWords = pushInput.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); switch (pushWords[0].ToLower()) { case "send": { /*if (!ArrayPositionCheck(1, pushWords)) * { * Console.WriteLine("Error: No command!"); * continue; * }*/ m.Add(new PUSH.Message(Command.SEND, pushWords)); goto PushMain; } case "update": m.Add(new PUSH.Message(Command.UPDATE, pushWords)); goto PushMain; case "done": break; default: goto PushMain; //break; } } break; case "remote": if (ArrayPositionCheck(2, words)) { /*if (words[1].ToLower() == "desktop" && words[2].ToLower() == "->") * { * if (words[3].ToLower() == "open") * { * System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); * psi.FileName = guiPath; * psi.Arguments = "-"; * } * MyEnum temp; * enumerators.TryGetValue("clients", out temp); * if (temp == null) //ip cím * { * * } * else * { * * } * //RemoteGUI.MainWindow mw = new RemoteGUI.MainWindow(); * //mw.jajj(); * }*/ if (words[1].ToLower() == "desktop") { if (words[2].ToLower() == "clients") { foreach (IPAddress address in computers.Values) { SOCKET.Client client = new SOCKET.Client(); client.socket = new System.Net.Sockets.Socket(new System.Net.IPEndPoint(address, 6546).AddressFamily, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); try { client.socket.Connect(address, 6546); } catch (System.Net.Sockets.SocketException ee) { //Console.WriteLine(e); Console.WriteLine("{0} -> Offline.", address.ToString()); } client.pmOut.New(); client.pmOut.Set(Command.COMPUTER_NAME_REQUEST); client.pmOut.End(); client.socket.SendTimeout = 1000; int sent; try { sent = client.socket.Send(client.bufferOut, 0, client.pmOut.Size, SocketFlags.None); } catch { Console.WriteLine("{0} -> SendTimeout.", address.ToString()); } int received = 0; client.socket.ReceiveTimeout = 1000; try { received = client.socket.Receive(client.bufferIn); } catch { Console.WriteLine("{0} -> ReceiveTimeout.", address.ToString()); } if (received > 0) { client.pmIn.New(); if (client.pmIn.Size == received) { Command c = client.pmIn.Read(); if (c == Command.COMPUTER_NAME) { Console.WriteLine("{0} -> Online -> {1}", address.ToString(), client.pmIn.br.ReadString()); } } else { Console.WriteLine("{0} -> Packet error.", address.ToString()); } } } } } } Commands.Set(ref words); break; case "network": if (ArrayPositionCheck(2, words)) { if (words[1] == "stream") { if (words[2] == "host") { Remote.NetworkStream ns = new Remote.NetworkStream("127.0.0.1", 6547, Remote.NetworkStream.Type.Server); Console.WriteLine("Test server running"); } else if (words[2] == "client") { Remote.NetworkStream ns = new Remote.NetworkStream("127.0.0.1", 6547, Remote.NetworkStream.Type.Client); Console.WriteLine("Test client running"); byte[] arr = new byte[1016 * 1500]; ns.SendSimple(arr); } } } break; case "exit": foreach (var client in clients) { client.Value.socket.Shutdown(SocketShutdown.Both); client.Value.socket.Close(); } Environment.Exit(0); break; default: break; } }