-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech_decreasing.cs
143 lines (110 loc) · 3.92 KB
/
speech_decreasing.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class decreasing : MonoBehaviour
{
private int level = 5;
public Text level_text;
private string roll = null;
private string pitch = null;
// Use this for initialization
void Start()
{
// Usually the server doesn't need to draw anything on the screen
Thread thr2 = new Thread(CreateServer);
thr2.Start();
}
void CreateServer()
{
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
//GetHostName not working on mac
// Debug.Log(ipHost.AddressList[3]);
IPAddress ipAddr = ipHost.AddressList[3];
IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 5005);
Socket listener = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
// Using Bind() method we associate a
// network address to the Server Socket
// All client that will connect to this
// Server Socket must know this network
// Address
listener.Bind(localEndPoint);
// Using Listen() method we create
// the Client list that will want
// to connect to Server
listener.Listen(10);
while (true)
{
Debug.Log("Waiting connection ... ");
// Suspend while waiting for
// incoming connection Using
// Accept() method the server
// will accept connection of client
Socket clientSocket = listener.Accept();
// Data buffer
byte[] bytes = new Byte[1024];
while (true)
{
int numByte = clientSocket.Receive(bytes);
string data = Encoding.ASCII.GetString(bytes,
0, numByte);
if (data[0] == 'I'){
string[] words = data.Split(',');
roll = words[1];
pitch = words[2];
if (data.IndexOf("#") > -1)
{
Debug.Log("failed");
break;
}
} else if (data[0] == 'S'){
Debug.Log(data)
cmd = data[1:]
Debug.Log(cmd)
msg = "";
if (cmd.Equals("hint")){
msg = "Here is a very useful hint";
} else if (cmd.Equals("play")) {
msg = "Resume the game";
} else if (cmd.Equals("pause")){
msg = "Game is now PAUSED;"
} else {
msg = "Client sent invalid command";
}
Debug.Log(msg);
}
}
//Debug.Log(data);
//byte[] message = Encoding.ASCII.GetBytes("Test Server");
// Send a message to Client
// using Send() method
// clientSocket.Send(message);
// Close client Socket using the
// Close() method. After closing,
// we can use the closed Socket
// for a new Client Connection
Debug.Log("Closing Connection");
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
Debug.Log("Connection Closed");
break;
}
}
catch (Exception e)
{
Debug.Log("exception");
Console.WriteLine(e.ToString());
}
}
void Update()
{
level_text.text = "Roll: " +roll+"\n"+"Pitch: "+pitch;
}
}