Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading; //
- using UnityEngine; // Using Unity 2017.1.0f3
- //
- //Called when the application started
- void Start()
- {
- //Create another thread for the networking (I don't want to block the main thread in C# too)
- thisServer = new TcpListener(IPAddress.Any, PORT);
- serverThreadStart = new ThreadStart(ServerThread);
- serverThread = new Thread(serverThreadStart);
- //Start the TcpServer
- serverThread.Start();
- }
- //The server thread
- void ServerThread() {
- //The reading buffer and the data
- Byte[] bytes = new Byte[1024];
- String data = null;
- //Wait for the client here after he is disconnected too
- while (true) {
- //Try to accept the incoming client
- connectedClient = thisServer.AcceptTcpClient();
- //Get a stream object for reading and writing
- NetworkStream stream = connectedClient.GetStream();
- //The data size
- int i;
- //Try to receive data from client while its connected to the server(this)
- while (connectedClient.Connected) {
- //Dont try with data encoding id its null
- if ((i = stream.Read(bytes, 0, bytes.Length)) != 0) {
- //Get the track data and decode it / encode to ASCII
- data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
- Debug.Log(data /*The message from processing*/);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement