Advertisement
Guest User

Untitled

a guest
Aug 8th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. namespace WinFormsApp1.ext;
  2. using System.Net.Sockets;
  3. using System.Net;
  4.  
  5. internal class WinAPI
  6. {
  7.     // We import some Methods from WinAPI. First the MoveWindow
  8.     [System.Runtime.InteropServices.DllImport("user32.dll")]
  9.     public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  10. }
  11.  
  12. public class PyHelper
  13. {
  14.     TcpListener server = null;
  15.     public void SocketServer()
  16.     {
  17.         Int32 Port = 11000;
  18.         IPAddress addr = IPAddress.Parse("0.0.0.0");
  19.  
  20.         server = new TcpListener(addr, Port);
  21.  
  22.         server.Start();
  23.  
  24.         Byte[] bytes = new Byte[256];
  25.         String data = null;
  26.  
  27.         while (true)
  28.         {
  29.             TcpClient client = server.AcceptTcpClient();
  30.             NetworkStream stream = client.GetStream();
  31.             int i;
  32.             while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
  33.             {
  34.                 data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
  35.                 MessageBox.Show(data);
  36.                 if(data.Split(" ")[0] == "set_url")
  37.                 {
  38.                     string url = data.Split(" ")[1];
  39.                     Program.SetUrl(url);
  40.                 }
  41.                 data = data.ToUpper();
  42.                 byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
  43.                 stream.Write(msg, 0, msg.Length);
  44.             }
  45.             client.Close();
  46.         }
  47.     }
  48.  
  49.     public String AskPy(string cmd)
  50.     {
  51.         // Here we build a TCP connection with the Python backend
  52.         TcpClient client = new TcpClient();
  53.         client.Connect(IPAddress.Loopback, 11001);
  54.         NetworkStream stream = client.GetStream();
  55.         byte[] msg = System.Text.Encoding.ASCII.GetBytes(cmd);
  56.         stream.Write(msg, 0, msg.Length);
  57.         byte[] bytes = new byte[256];
  58.         String data = null;
  59.         int i = stream.Read(bytes, 0, bytes.Length);
  60.         data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
  61.         client.Close();
  62.         return data;
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement