Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace WinFormsApp1.ext;
- using System.Net.Sockets;
- using System.Net;
- internal class WinAPI
- {
- // We import some Methods from WinAPI. First the MoveWindow
- [System.Runtime.InteropServices.DllImport("user32.dll")]
- public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
- }
- public class PyHelper
- {
- TcpListener server = null;
- public void SocketServer()
- {
- Int32 Port = 11000;
- IPAddress addr = IPAddress.Parse("0.0.0.0");
- server = new TcpListener(addr, Port);
- server.Start();
- Byte[] bytes = new Byte[256];
- String data = null;
- while (true)
- {
- TcpClient client = server.AcceptTcpClient();
- NetworkStream stream = client.GetStream();
- int i;
- while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
- {
- data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
- MessageBox.Show(data);
- if(data.Split(" ")[0] == "set_url")
- {
- string url = data.Split(" ")[1];
- Program.SetUrl(url);
- }
- data = data.ToUpper();
- byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
- stream.Write(msg, 0, msg.Length);
- }
- client.Close();
- }
- }
- public String AskPy(string cmd)
- {
- // Here we build a TCP connection with the Python backend
- TcpClient client = new TcpClient();
- client.Connect(IPAddress.Loopback, 11001);
- NetworkStream stream = client.GetStream();
- byte[] msg = System.Text.Encoding.ASCII.GetBytes(cmd);
- stream.Write(msg, 0, msg.Length);
- byte[] bytes = new byte[256];
- String data = null;
- int i = stream.Read(bytes, 0, bytes.Length);
- data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
- client.Close();
- return data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement