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); }
public ClientData this[SOCKET.Client sc] { get { return(scDic[sc]); } /*set * { * scDic[sc] = value; * }*/ }
// 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; } }
private bool OnReceiveAsync(System.Net.Sockets.SocketAsyncEventArgs arg) { // Get the client object from arg SOCKET.Client client = (SOCKET.Client)arg.UserToken; // Notes: First 4 bytes are the size of the message. // Notes: It's impossible to send only 4 bytes, as that would mean we only send the message size, which doesn't make sense , so this must be always bigger than 4 bytes if (arg.BytesTransferred > 4 && client.pmIn.NewRead() == arg.BytesTransferred) { lastCommand = client.pmIn.ReadCommand(); switch (lastCommand) { // Connection attempt case Command.CONNECT: #region CONNECT { cw.WriteLine(Remote.Language.Find("IncomingConnection", this, "Incoming connection attempt from: {0}"), client.ClientAddress); Command c = client.pmIn.ReadCommand(); if (c == Command.USER_NAME) { Dispatcher.Invoke(new Action(() => { foreach (var cd in clients) { if (cd.client == client) { cd.name = client.pmIn.ReadString(); cd.lName.Content = cd.name; connectedComputersItems.Add(cd.sp); ConnectedComputers.ItemsSource = connectedComputersItems; break; } } }), DispatcherPriority.Render); client.pmOut.New(); client.pmOut.Write(Command.CONNECT_SUCCESS); client.pmOut.Write(Command.USER_NAME); client.pmOut.Write(Settings.s.name); client.pmOut.End(); if (client.SendSafe()) { cw.WriteLine(Remote.Language.Find("ClientConnectSuccess", this, "Client from {0} successfully connected!"), client.ClientAddress); return(true); } else { cw.WriteLine(client.lastSendException.Message); client.Close(); client = null; } } } break; #endregion // Success! case Command.CONNECT_SUCCESS: #region CONNECT_SUCCES { cw.WriteLine(Remote.Language.Find("ClientConnected", this, "Succesfully connected to {0}"), client.ClientAddress); Command c = client.pmIn.ReadCommand(); if (c == Command.USER_NAME) { Dispatcher.Invoke(new Action(() => { ClientData cd = new ClientData(); cd.client = client; Label l = new Label(); cd.name = client.pmIn.ReadString(); cd.lName = l; l.Content = cd.name; StackPanel sp = new StackPanel(); sp.Children.Add(l); //connectedComputersBinding.Add(sp, cd); clients[sp] = cd; connectedComputersItems.Add(sp); ConnectedComputers.ItemsSource = connectedComputersItems; }), DispatcherPriority.Render); } return(true); } #endregion case Command.REMOTE_DESKTOP_REQUEST: #region REMOTE_DESKTOP_REQUEST { ClientData cd = clients[(SOCKET.Client)arg.UserToken]; cd.remoteDesktopRequest = true; Dispatcher.Invoke(new Action(() => { ConnectedComputers_SelectionChanged(null, null); }), DispatcherPriority.Render); cw.WriteLine(Remote.Language.Find("RemoteDesktopRequestIncoming", this, "A Remote Desktop request was received from {0} / {1}"), cd.name, cd.client.ClientAddress); return(true); } #endregion case Command.REMOTE_DESKTOP_CAPTURE_INFO_REQUEST: #region REMOTE_DESKTOP_CAPTURE_INFO_REQUEST { Dispatcher.Invoke(new Action(() => { this.WindowState = System.Windows.WindowState.Minimized; loader.Show(); }), DispatcherPriority.Render); loader.ChangeText("Sending streaming settings..."); ClientData cd = clients[(SOCKET.Client)arg.UserToken]; cd.client.pmOut.New(); cd.client.pmOut.Write(Command.REMOTE_DESKTOP_CAPTURE_INFO); ProtoBuf.Serializer.SerializeWithLengthPrefix(cd.client.pmOut.Stream, Settings.s.remoteDesktopSettings, ProtoBuf.PrefixStyle.Base128); cd.client.pmOut.End(); if (cd.client.SendSafe()) { return(true); } else { } } break; #endregion case Command.REMOTE_DESKTOP_CAPTURE_INFO: #region REMOTE_DESKTOP_CAPTURE_INFO { loader.ChangeText("Creating host..."); ClientData cd = clients[(SOCKET.Client)arg.UserToken]; Settings.RemoteDesktopSettings s = ProtoBuf.Serializer.DeserializeWithLengthPrefix <Settings.RemoteDesktopSettings>(cd.client.pmIn.Stream, ProtoBuf.PrefixStyle.Base128); host = new RemoteDesktopHost(s); cd.client.pmOut.New(); cd.client.pmOut.Write(Command.REMOTE_DESKTOP_HOST_READY); cd.client.pmOut.Write(SystemParameters.PrimaryScreenWidth); cd.client.pmOut.Write(SystemParameters.PrimaryScreenHeight); cd.client.pmOut.End(); if (cd.client.SendSafe()) { return(true); } else { } } break; #endregion case Command.REMOTE_DESKTOP_HOST_READY: #region REMOTE_DESKTOP_HOST_READY { loader.ChangeText("Creating streaming connection..."); bool whatToReturn = false; RemoteDesktop rd; Dispatcher.Invoke(new Action(() => { rd = new RemoteDesktop(); rd.x = (int)client.pmIn.ReadDouble(); rd.y = (int)client.pmIn.ReadDouble(); System.Net.IPAddress address = client.ClientAddress; rd.clientStream = new SOCKET.Client(); rd.clientStream.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 { rd.clientStream.socket.Connect(address, Settings.s.remoteDesktopPort); rd.clientStream.StartReceiveAsync(rd.OnReceiveAsyncStream); rd.clientStream.socket.SendTimeout = 5000; rd.clientStream.socket.ReceiveTimeout = 5000; rd.clientStream.pmOut.Write(Command.REMOTE_DESKTOP_CONNECT_STREAM); rd.clientStream.pmOut.End(); if (rd.clientStream.SendSafe()) { //cw.WriteLine(Remote.Language.Find("ClientConnecting", this, "Connecting to {0}"), Address.Text); whatToReturn = true; } else { cw.WriteLine(rd.clientStream.lastSendException.Message); rd.clientStream.Close(); rd.clientStream = null; whatToReturn = false; } //cw.WriteLine(Remote.Language.Find("ClientConnected", this, "Succesfully conencted to {0}"), Address.Text); } catch (System.Net.Sockets.SocketException ee) { cw.WriteLine(ee.Message); rd.clientStream.Close(); rd.clientStream = null; } }), DispatcherPriority.Render); if (whatToReturn) { return(true); } else { return(false); } } #endregion case Command.REMOTE_DESKTOP_CONNECT_STREAM: #region REMOTE_DESKTOP_CONNECT_STREAM { loader.ChangeText("Streaming connection created..."); ClientData cd = clients[(SOCKET.Client)arg.UserToken]; Dispatcher.Invoke(new Action(() => { cd.sp.Children.Remove(cd.lName); clients.Remove(cd); host.clientStream = cd.client; host.clientStream.onReceiveAsync = host.OnReceiveAsync; }), DispatcherPriority.Render); host.clientStream.pmOut.New(); host.clientStream.pmOut.Write(Command.REMOTE_DESKTOP_CONNECT_STREAM_SUCCESS); host.clientStream.pmOut.End(); if (host.clientStream.SendSafe()) { return(true); } else { } // send success } break; #endregion case Command.REMOTE_DESKTOP_CONNECT_INPUT: #region REMOTE_DESKTOP_CONNECT_INPUT { loader.ChangeText("Input stream created..."); ClientData cd = clients[(SOCKET.Client)arg.UserToken]; Dispatcher.Invoke(new Action(() => { cd.sp.Children.Remove(cd.lName); clients.Remove(cd); host.clientInput = cd.client; host.clientInput.onReceiveAsync = host.OnReceiveAsyncInput; }), DispatcherPriority.Render); host.clientInput.pmOut.New(); host.clientInput.pmOut.Write(Command.REMOTE_DESKTOP_CONNECT_INPUT_SUCCESS); host.clientInput.pmOut.End(); if (host.clientInput.SendSafe()) { return(true); } else { } // send success } break; #endregion default: break; } } //Client disconnected, clean up. else if (arg.BytesTransferred == 0) { switch (lastCommand) { // Error? EMPTY_COMMAND = 0 case Command.EMPTY_COMMAND: break; // Restore MainWindow upon client fail case Command.REMOTE_DESKTOP_CAPTURE_INFO_REQUEST: { Dispatcher.Invoke(new Action(() => { loader.Close(); loader = new RemoteGuiLoader.MainWindow(); WindowState = System.Windows.WindowState.Normal; }), DispatcherPriority.Render); cw.WriteLine(Remote.Language.Find("RemoteDekstopCaptureInfoRequestClientDisconnected", this, "The client disconnected during Remote Desktop initialization")); } break; default: break; } foreach (var item in connectedComputersItems) { } //cw.WriteLine("End"); } return(false); }