├── MyListener.cs ├── PythonClient.py └── README.md /MyListener.cs: -------------------------------------------------------------------------------- 1 | using System.Net; 2 | using System.Net.Sockets; 3 | using System.Text; 4 | using UnityEngine; 5 | using System.Threading; 6 | 7 | public class MyListener : MonoBehaviour 8 | { 9 | Thread thread; 10 | public int connectionPort = 25001; 11 | TcpListener server; 12 | TcpClient client; 13 | bool running; 14 | 15 | 16 | void Start() 17 | { 18 | // Receive on a separate thread so Unity doesn't freeze waiting for data 19 | ThreadStart ts = new ThreadStart(GetData); 20 | thread = new Thread(ts); 21 | thread.Start(); 22 | } 23 | 24 | void GetData() 25 | { 26 | // Create the server 27 | server = new TcpListener(IPAddress.Any, connectionPort); 28 | server.Start(); 29 | 30 | // Create a client to get the data stream 31 | client = server.AcceptTcpClient(); 32 | 33 | // Start listening 34 | running = true; 35 | while (running) 36 | { 37 | Connection(); 38 | } 39 | server.Stop(); 40 | } 41 | 42 | void Connection() 43 | { 44 | // Read data from the network stream 45 | NetworkStream nwStream = client.GetStream(); 46 | byte[] buffer = new byte[client.ReceiveBufferSize]; 47 | int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize); 48 | 49 | // Decode the bytes into a string 50 | string dataReceived = Encoding.UTF8.GetString(buffer, 0, bytesRead); 51 | 52 | // Make sure we're not getting an empty string 53 | //dataReceived.Trim(); 54 | if (dataReceived != null && dataReceived != "") 55 | { 56 | // Convert the received string of data to the format we are using 57 | position = ParseData(dataReceived); 58 | nwStream.Write(buffer, 0, bytesRead); 59 | } 60 | } 61 | 62 | // Use-case specific function, need to re-write this to interpret whatever data is being sent 63 | public static Vector3 ParseData(string dataString) 64 | { 65 | Debug.Log(dataString); 66 | // Remove the parentheses 67 | if (dataString.StartsWith("(") && dataString.EndsWith(")")) 68 | { 69 | dataString = dataString.Substring(1, dataString.Length - 2); 70 | } 71 | 72 | // Split the elements into an array 73 | string[] stringArray = dataString.Split(','); 74 | 75 | // Store as a Vector3 76 | Vector3 result = new Vector3( 77 | float.Parse(stringArray[0]), 78 | float.Parse(stringArray[1]), 79 | float.Parse(stringArray[2])); 80 | 81 | return result; 82 | } 83 | 84 | // Position is the data being received in this example 85 | Vector3 position = Vector3.zero; 86 | 87 | void Update() 88 | { 89 | // Set this object's position in the scene according to the position received 90 | transform.position = position; 91 | } 92 | } -------------------------------------------------------------------------------- /PythonClient.py: -------------------------------------------------------------------------------- 1 | import socket 2 | 3 | host, port = "127.0.0.1", 25001 4 | data = "1,2,3" 5 | 6 | # SOCK_STREAM means TCP socket 7 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 8 | 9 | try: 10 | # Connect to the server and send the data 11 | sock.connect((host, port)) 12 | sock.sendall(data.encode("utf-8")) 13 | response = sock.recv(1024).decode("utf-8") 14 | print (response) 15 | 16 | finally: 17 | sock.close() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-Unity-Socket 2 | Includes a c# script for creating a server in Unity and a python script for creating a client to send data to Unity 3 | 4 | A brief tutorial for this is available at: https://youtu.be/Dm0CiAiZk14 5 | --------------------------------------------------------------------------------