using System.Net; using System.Net.Sockets;
public partial class ServerForm : Form { private Socket _serverSocket, _clientSocket; private byte[] _buffer;
private void StartServer() { try { _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _serverSocket.Bind(new IPEndPoint(IPAddress.Any, 3333)); _serverSocket.Listen(0); _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null); } catch (Exception ex) { MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public ServerForm() { InitializeComponent(); StartServer(); }
[ DllImport("user32.dll")] private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo); private const int KEYEVENTF_EXTENDEDKEY = 1; private const int KEYEVENTF_KEYUP = 2; public static void KeyDown(Keys vKey) { keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0); } public static void KeyUp(Keys vKey) { keybd_event((byte)vKey, 0, KEYEVENTF_KEYUP, 0); }
private void ReceiveCallback(IAsyncResult AR) { try { int received = _clientSocket.EndReceive(AR); Array.Resize(ref _buffer, received); string text = Encoding.ASCII.GetString(_buffer); // AppendToTextBox(text); // ------------------ Array.Resize(ref _buffer, _clientSocket.ReceiveBufferSize); _clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null); } catch (Exception ex) { MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void AppendToTextBox(string text) { MethodInvoker invoker = new MethodInvoker(delegate { string text_before = text; string exitW = text; // if (text == "a") { KeyUp(Keys.D); KeyDown(Keys.A); textBox.Text += text + " "; } // if (text == "a1" ) { KeyUp(Keys.A); textBox.Text += text + " "; } }); this.Invoke(invoker); }
if (text == "a") { KeyUp(Keys.D); KeyDown(Keys.A); textBox.Text += text + " "; }
using Windows.Networking.Sockets; using Windows.Networking; using Windows.Storage.Streams;
StreamSocket clientSocket = new StreamSocket();
private async void btnConnect_Click(object sender, RoutedEventArgs e) { HostName host = new HostName(textBoxIP.Text); string port = textBoxPort.Text; if (connected) { StatusText.Text = " "; return; } try { StatusText.Text = " ..."; await clientSocket.ConnectAsync(host, port); connected = true; StatusText.Text = " " + Environment.NewLine; } catch (Exception exception) { if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown) { throw; } StatusText.Text = " : "; closing = true; clientSocket.Dispose(); clientSocket = null; } }
private async void sendkey(string key) { if (!connected) { StatusText.Text = " "; return; } try { StatusText.Text = " ..."; DataWriter writer = new DataWriter(clientSocket.OutputStream); writer.WriteString(key); await writer.StoreAsync(); StatusText.Text = " " + Environment.NewLine; writer.DetachStream(); writer.Dispose(); } catch (Exception exception) { if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown) { throw; } StatusText.Text = " "; closing = true; clientSocket.Dispose(); clientSocket = null; connected = false; } }
// // private void gotFocusUp(object sender, RoutedEventArgs e) { sendkey("w"); } private void lostFocusUp(object sender, RoutedEventArgs e) { sendkey("w1"); btnConnect.Focus(FocusState.Programmatic); } private void lostFocusUp(object sender, PointerRoutedEventArgs e) { sendkey("w1"); btnConnect.Focus(FocusState.Programmatic); }
Source: https://habr.com/ru/post/274271/
All Articles