-
Notifications
You must be signed in to change notification settings - Fork 55
/
SocketServer.cs
149 lines (114 loc) · 3.69 KB
/
SocketServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using UnityEngine;
using System;
using System.Text;
using System.Net.Sockets;
using System.Net;
public class SocketServer : MonoBehaviour
{
public static string data;
System.Threading.Thread SocketThread;
volatile bool keepReading = false;
// Use this for initialization
void Start()
{
Application.runInBackground = true;
}
public static void Execute(string command)// Execute cmd command
{
var processInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/S /C " + command)
{
CreateNoWindow = true,
UseShellExecute = true,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
};
System.Diagnostics.Process.Start(processInfo);
}
public void StartServer()
{
SocketThread = new System.Threading.Thread(NetworkCode);
SocketThread.IsBackground = true;
SocketThread.Start();
Execute("cd PythonScript & python visual_measurement.py");// Execute python client script
}
private string GetIPAddress()
{
string localIP = "127.0.0.1";
return localIP;
}
Socket listener;
Socket handler;
void NetworkCode()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Host running the application.
Debug.Log("Ip " + GetIPAddress().ToString());
IPAddress[] ipArray = Dns.GetHostAddresses(GetIPAddress());
IPEndPoint localEndPoint = new IPEndPoint(ipArray[0], 1755);//端口为1755
// Create a TCP/IP socket.
listener = new Socket(ipArray[0].AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
// Start listening for connections.
while (true)
{
keepReading = true;
// Program is suspended while waiting for an incoming connection.
Debug.Log("Waiting for Connection");
handler = listener.Accept();
Debug.Log("Client Connected");
data = null;
// An incoming connection needs to be processed.
while (keepReading)
{
bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
Debug.Log("Received from Server");
if (bytesRec <= 0)
{
keepReading = false;
handler.Disconnect(true);
break;
}
data = Encoding.ASCII.GetString(bytes, 0, bytesRec);
string[] tempdata = data.Split(':');
if (data.IndexOf("<EOF>") > -1)
{
break;
}
System.Threading.Thread.Sleep(1);
}
System.Threading.Thread.Sleep(1);
}
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
void StopServer()
{
keepReading = false;
//stop thread
if (SocketThread != null)
{
//listener.Shutdown(SocketShutdown.Both);
//listener.Close();
SocketThread.Abort();
}
if (handler != null && handler.Connected)
{
handler.Disconnect(false);
Debug.Log("Disconnected!");
}
}
public void OnDisable()
{
StopServer();
}
}