質問編集履歴

2

IPアドレスが個人のものなので危ないのでコードから消させていただきます。

2018/04/19 02:51

投稿

peter_kes
peter_kes

スコア14

test CHANGED
File without changes
test CHANGED
@@ -192,10 +192,6 @@
192
192
 
193
193
  }
194
194
 
195
-
196
-
197
- //string ipOrHost = "14.132.37.176";
198
-
199
195
  string ipOrHost = "192.168.0.4";
200
196
 
201
197
  //string ipOrHost = "127.0.0.1";

1

Client側のコードも記載

2018/04/19 02:51

投稿

peter_kes
peter_kes

スコア14

test CHANGED
File without changes
test CHANGED
@@ -24,13 +24,9 @@
24
24
 
25
25
 
26
26
 
27
-
28
-
29
-
30
-
31
27
  ### 該当のソースコード
32
28
 
33
-
29
+ (フリーズするダメなコード Server側)
34
30
 
35
31
  int port = 2222;
36
32
 
@@ -60,6 +56,176 @@
60
56
 
61
57
  ....
62
58
 
59
+
60
+
61
+ --------------------------------------------------------------------------------
62
+
63
+ サーバー側修正後、ポートとIPなどがおかしいくてエラーSocketException: 対象のコンピューターによって拒否されたため、接続できませんでした。がでる Client側のコード
64
+
65
+ bool isForceStop = false;
66
+
67
+ NetworkStream stream = null;
68
+
69
+ bool isStopReading = false;
70
+
71
+ byte[] readbuf;
72
+
73
+
74
+
75
+ private IEnumerator Start(){
76
+
77
+ Debug.Log("START START");
78
+
79
+ readbuf = new byte[1024];
80
+
81
+
82
+
83
+ while (!isForceStop) {
84
+
85
+ if(!isStopReading) {
86
+
87
+ StartCoroutine(ReadMessage ());
88
+
89
+ }
90
+
91
+ yield return new WaitForSeconds(1f);//あんまりしょっちゅうやらないために
92
+
93
+ }
94
+
95
+ }
96
+
97
+
98
+
99
+
100
+
101
+ void Update()
102
+
103
+ {
104
+
105
+ if (Input.GetKeyDown (KeyCode.A) == true) {
106
+
107
+ isForceStop = true;
108
+
109
+ }
110
+
111
+
112
+
113
+ }
114
+
115
+
116
+
117
+ public IEnumerator SendCurrentMessage(string message){
118
+
119
+
120
+
121
+ Debug.Log ("START SendMessage:" + message);
122
+
123
+
124
+
125
+ if (stream == null) {
126
+
127
+ stream = GetNetworkStream();
128
+
129
+ }
130
+
131
+ string playerName = "[A]: ";
132
+
133
+ //サーバーにデータを送信する
134
+
135
+ Encoding enc = Encoding.UTF8;
136
+
137
+ byte[] sendBytes = enc.GetBytes(playerName + message + "\n");
138
+
139
+ //データを送信する
140
+
141
+ stream.Write(sendBytes, 0, sendBytes.Length);
142
+
143
+ yield break;
144
+
145
+ }
146
+
147
+
148
+
149
+ //常駐
150
+
151
+ private IEnumerator ReadMessage(){
152
+
153
+ stream = GetNetworkStream ();
154
+
155
+ // 非同期で待ち受けする
156
+
157
+ stream.BeginRead (readbuf, 0, readbuf.Length, new AsyncCallback (ReadCallback), null);
158
+
159
+ isStopReading = true;
160
+
161
+ yield return null;
162
+
163
+ }
164
+
165
+
166
+
167
+ public void ReadCallback(IAsyncResult ar ){
168
+
169
+ Encoding enc = Encoding.UTF8;
170
+
171
+ stream = GetNetworkStream ();
172
+
173
+ int bytes = stream.EndRead(ar);
174
+
175
+ string message = enc.GetString (readbuf, 0, bytes);
176
+
177
+ message = message.Replace("\r", "").Replace("\n", "");
178
+
179
+ isStopReading = false;
180
+
181
+ Chat.Insntace.GetMessage (message);
182
+
183
+ }
184
+
185
+
186
+
187
+ private NetworkStream GetNetworkStream(){
188
+
189
+ if (stream != null && stream.CanRead) {
190
+
191
+ return stream;
192
+
193
+ }
194
+
195
+
196
+
197
+ //string ipOrHost = "14.132.37.176";
198
+
199
+ string ipOrHost = "192.168.0.4";
200
+
201
+ //string ipOrHost = "127.0.0.1";
202
+
203
+ int port = 2222;
204
+
205
+
206
+
207
+ //TcpClientを作成し、サーバーと接続する
208
+
209
+ TcpClient tcp = new TcpClient(ipOrHost, port);
210
+
211
+ //TcpClient tcp = new TcpClient(System.Net.IPAddress.Any.ToString(), port);
212
+
213
+
214
+
215
+ Debug.Log("success conn server");
216
+
217
+
218
+
219
+ //NetworkStreamを取得する
220
+
221
+ return tcp.GetStream();
222
+
223
+ }
224
+
225
+
226
+
227
+
228
+
63
229
  ### 試したこと
64
230
 
65
231
  Try chatch でエラーハンドルを入れても何も解決しません。