質問編集履歴

3

インデントを修正しました

2020/02/19 11:41

投稿

HiroyaIto
HiroyaIto

スコア5

test CHANGED
File without changes
test CHANGED
@@ -24,11 +24,11 @@
24
24
 
25
25
  ### 該当のソースコード```
26
26
 
27
- Imports System.IO                             
27
+ Imports System.IO
28
-
28
+
29
- Imports System.Net                       
29
+ Imports System.Net
30
-
30
+
31
- Imports System.Net.Sockets                  
31
+ Imports System.Net.Sockets
32
32
 
33
33
  Imports System.Threading
34
34
 
@@ -40,11 +40,11 @@
40
40
 
41
41
 
42
42
 
43
- Dim ServerStatus As Boolean = False                
43
+ Dim ServerStatus As Boolean = False
44
-
44
+
45
- Dim ServerTrying As Boolean = False                
45
+ Dim ServerTrying As Boolean = False
46
-
46
+
47
- Dim Server As TcpListener                 
47
+ Dim Server As TcpListener
48
48
 
49
49
  Dim Clients As New List(Of TcpClient)
50
50
 
@@ -52,7 +52,7 @@
52
52
 
53
53
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
54
54
 
55
- CheckForIllegalCrossThreadCalls = False                   
55
+ CheckForIllegalCrossThreadCalls = False
56
56
 
57
57
  End Sub
58
58
 
@@ -62,43 +62,43 @@
62
62
 
63
63
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
64
64
 
65
- StartServer()                                 
65
+ StartServer()
66
-
66
+
67
- End Sub
67
+ End Sub
68
-
69
-
70
-
71
-
72
-
73
-
74
-
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
- Function StartServer()                  
75
+ Function StartServer()
76
-
76
+
77
- If ServerStatus = False Then                
77
+ If ServerStatus = False Then
78
-
78
+
79
- ServerTrying = True                            
79
+ ServerTrying = True
80
-
80
+
81
- Try                
81
+ Try
82
-
82
+
83
- Server = New TcpListener(IPAddress.Any, 4305)           
83
+ Server = New TcpListener(IPAddress.Any, 4305)
84
-
84
+
85
- Server.Start()                   
85
+ Server.Start()
86
86
 
87
87
  ServerStatus = True
88
88
 
89
89
  Threading.ThreadPool.QueueUserWorkItem(AddressOf Handler_Client)
90
90
 
91
- Catch ex As Exception                
91
+ Catch ex As Exception
92
-
92
+
93
- ServerStatus = False                       
93
+ ServerStatus = False
94
-
94
+
95
- End Try                         
95
+ End Try
96
-
96
+
97
- ServerTrying = False                           
97
+ ServerTrying = False
98
-
98
+
99
- End If                                 
99
+ End If
100
-
100
+
101
- Return True                                    
101
+ Return True
102
102
 
103
103
  End Function
104
104
 
@@ -106,39 +106,39 @@
106
106
 
107
107
  Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
108
108
 
109
- StopServer()                                  
109
+ StopServer()
110
-
110
+
111
- End Sub
111
+ End Sub
112
-
113
-
114
-
112
+
113
+
114
+
115
- Function StopServer()                              
115
+ Function StopServer()
116
-
116
+
117
- If ServerStatus = True Then                            
117
+ If ServerStatus = True Then
118
-
118
+
119
- ServerTrying = True                          
119
+ ServerTrying = True
120
-
120
+
121
- Try                            
121
+ Try
122
-
122
+
123
- For Each Client As TcpClient In Clients                  
123
+ For Each Client As TcpClient In Clients
124
-
124
+
125
- Client.Close()                          
125
+ Client.Close()
126
-
126
+
127
- Next                                    
127
+ Next
128
-
128
+
129
- Server.Stop()                              
129
+ Server.Stop()
130
-
130
+
131
- ServerStatus = False                            
131
+ ServerStatus = False
132
-
132
+
133
- Catch ex As Exception                             
133
+ Catch ex As Exception
134
-
134
+
135
- StopServer()                              
135
+ StopServer()
136
-
136
+
137
- End Try                                 
137
+ End Try
138
-
138
+
139
- End If                                 
139
+ End If
140
-
140
+
141
- Return True                               
141
+ Return True
142
142
 
143
143
  End Function
144
144
 
@@ -146,13 +146,13 @@
146
146
 
147
147
 
148
148
 
149
- Function Handler_Client(ByVal state As Object)               
149
+ Function Handler_Client(ByVal state As Object)
150
150
 
151
151
  Dim TempClient As TcpClient
152
152
 
153
153
 
154
154
 
155
- Try                            
155
+ Try
156
156
 
157
157
  Using Client As TcpClient = Server.AcceptTcpClient
158
158
 
@@ -162,9 +162,9 @@
162
162
 
163
163
  Threading.ThreadPool.QueueUserWorkItem(AddressOf Handler_Client)
164
164
 
165
- End If                        
165
+ End If
166
-
166
+
167
- Clients.Add(Client)                        
167
+ Clients.Add(Client)
168
168
 
169
169
  TempClient = Client
170
170
 
@@ -178,19 +178,19 @@
178
178
 
179
179
  Dim RawData As String = RX.ReadLine
180
180
 
181
- Richtextbox1.text += Client.Client.RemoteEndPoint.ToString + ">>" + RawData + vbNewLine                          
181
+ Richtextbox1.text += Client.Client.RemoteEndPoint.ToString + ">>" + RawData + vbNewLine
182
-
182
+
183
- End While                            
183
+ End While
184
184
 
185
185
  End If
186
186
 
187
187
 
188
188
 
189
- If RX.BaseStream.CanRead = False Then              
189
+ If RX.BaseStream.CanRead = False Then
190
-
190
+
191
- Client.Close()                     
191
+ Client.Close()
192
-
192
+
193
- Clients.Remove(Client)                          
193
+ Clients.Remove(Client)
194
194
 
195
195
  End If
196
196
 
@@ -202,85 +202,85 @@
202
202
 
203
203
 
204
204
 
205
- Catch ex As Exception                      
205
+ Catch ex As Exception
206
-
206
+
207
- If TempClient.GetStream.CanRead = False Then                
207
+ If TempClient.GetStream.CanRead = False Then
208
-
208
+
209
- TempClient.Close()                 
209
+ TempClient.Close()
210
-
210
+
211
- Clients.Remove(TempClient)                   
211
+ Clients.Remove(TempClient)
212
-
212
+
213
- End If                                  
213
+ End If
214
214
 
215
215
  End Try
216
216
 
217
217
 
218
218
 
219
- Return True                               
219
+ Return True
220
220
 
221
221
  End Function
222
222
 
223
223
 
224
224
 
225
- Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown                      
225
+ Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
226
-
226
+
227
- If e.KeyCode = Keys.Enter Then           
227
+ If e.KeyCode = Keys.Enter Then
228
-
228
+
229
- e.SuppressKeyPress = True           
229
+ e.SuppressKeyPress = True
230
-
230
+
231
- If TextBox1.Text.Length > 0 Then                    
231
+ If TextBox1.Text.Length > 0 Then
232
-
232
+
233
- SendToClients(TextBox1.Text)                      
233
+ SendToClients(TextBox1.Text)
234
-
234
+
235
- TextBox1.Clear()                         
235
+ TextBox1.Clear()
236
-
236
+
237
- End If                                  
237
+ End If
238
-
238
+
239
- End If                                   
239
+ End If
240
-
240
+
241
- End Sub
241
+ End Sub
242
-
243
-
244
-
242
+
243
+
244
+
245
- Function SendToClients(ByVal Data As String)                      
245
+ Function SendToClients(ByVal Data As String)
246
-
246
+
247
- If ServerStatus = True Then                            
247
+ If ServerStatus = True Then
248
-
248
+
249
- If Clients.Count > 0 Then                     
249
+ If Clients.Count > 0 Then
250
-
250
+
251
- Try                         
251
+ Try
252
252
 
253
253
  For Each Client As TcpClient In Clients
254
254
 
255
255
  Dim TX1 As New StreamWriter(Client.GetStream)
256
256
 
257
- TX1.WriteLine(Data)                       
257
+ TX1.WriteLine(Data)
258
-
258
+
259
- TX1.Flush()                           
259
+ TX1.Flush()
260
-
260
+
261
- Next                          
261
+ Next
262
-
262
+
263
- Catch ex As Exception                     
263
+ Catch ex As Exception
264
-
264
+
265
- SendToClients(Data)                     
265
+ SendToClients(Data)
266
-
266
+
267
- End Try                              
267
+ End Try
268
-
268
+
269
- End If                                  
269
+ End If
270
-
270
+
271
- End If                               
271
+ End If
272
-
272
+
273
- Return True                               
273
+ Return True
274
274
 
275
275
  End Function
276
276
 
277
277
 
278
278
 
279
- Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 
279
+ Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
280
-
281
-
282
-
280
+
281
+
282
+
283
- End Sub                                   
283
+ End Sub
284
284
 
285
285
  End Class
286
286
 

2

2020/02/19 11:41

投稿

HiroyaIto
HiroyaIto

スコア5

test CHANGED
File without changes
test CHANGED
@@ -24,11 +24,11 @@
24
24
 
25
25
  ### 該当のソースコード```
26
26
 
27
- Imports System.IO
27
+ Imports System.IO                             
28
-
28
+
29
- Imports System.Net
29
+ Imports System.Net                       
30
-
30
+
31
- Imports System.Net.Sockets
31
+ Imports System.Net.Sockets                  
32
32
 
33
33
  Imports System.Threading
34
34
 
@@ -40,11 +40,11 @@
40
40
 
41
41
 
42
42
 
43
- Dim ServerStatus As Boolean = False
43
+ Dim ServerStatus As Boolean = False                
44
-
44
+
45
- Dim ServerTrying As Boolean = False
45
+ Dim ServerTrying As Boolean = False                
46
-
46
+
47
- Dim Server As TcpListener
47
+ Dim Server As TcpListener                 
48
48
 
49
49
  Dim Clients As New List(Of TcpClient)
50
50
 
@@ -52,7 +52,7 @@
52
52
 
53
53
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
54
54
 
55
- CheckForIllegalCrossThreadCalls = False
55
+ CheckForIllegalCrossThreadCalls = False                   
56
56
 
57
57
  End Sub
58
58
 
@@ -62,7 +62,7 @@
62
62
 
63
63
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
64
64
 
65
- StartServer()
65
+ StartServer()                                 
66
66
 
67
67
  End Sub
68
68
 
@@ -72,33 +72,33 @@
72
72
 
73
73
 
74
74
 
75
- Function StartServer()
75
+ Function StartServer()                  
76
-
76
+
77
- If ServerStatus = False Then
77
+ If ServerStatus = False Then                
78
-
78
+
79
- ServerTrying = True
79
+ ServerTrying = True                            
80
-
80
+
81
- Try
81
+ Try                
82
-
82
+
83
- Server = New TcpListener(IPAddress.Any, 4305)
83
+ Server = New TcpListener(IPAddress.Any, 4305)           
84
-
84
+
85
- Server.Start()
85
+ Server.Start()                   
86
86
 
87
87
  ServerStatus = True
88
88
 
89
89
  Threading.ThreadPool.QueueUserWorkItem(AddressOf Handler_Client)
90
90
 
91
- Catch ex As Exception
91
+ Catch ex As Exception                
92
-
92
+
93
- ServerStatus = False
93
+ ServerStatus = False                       
94
-
94
+
95
- End Try
95
+ End Try                         
96
-
96
+
97
- ServerTrying = False
97
+ ServerTrying = False                           
98
-
98
+
99
- End If
99
+ End If                                 
100
-
100
+
101
- Return True
101
+ Return True                                    
102
102
 
103
103
  End Function
104
104
 
@@ -106,39 +106,39 @@
106
106
 
107
107
  Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
108
108
 
109
- StopServer()
109
+ StopServer()                                  
110
110
 
111
111
  End Sub
112
112
 
113
113
 
114
114
 
115
- Function StopServer()
115
+ Function StopServer()                              
116
-
116
+
117
- If ServerStatus = True Then
117
+ If ServerStatus = True Then                            
118
-
118
+
119
- ServerTrying = True
119
+ ServerTrying = True                          
120
-
120
+
121
- Try
121
+ Try                            
122
-
122
+
123
- For Each Client As TcpClient In Clients
123
+ For Each Client As TcpClient In Clients                  
124
-
124
+
125
- Client.Close()
125
+ Client.Close()                          
126
-
126
+
127
- Next
127
+ Next                                    
128
-
128
+
129
- Server.Stop()
129
+ Server.Stop()                              
130
-
130
+
131
- ServerStatus = False
131
+ ServerStatus = False                            
132
-
132
+
133
- Catch ex As Exception
133
+ Catch ex As Exception                             
134
-
134
+
135
- StopServer()
135
+ StopServer()                              
136
-
136
+
137
- End Try
137
+ End Try                                 
138
-
138
+
139
- End If
139
+ End If                                 
140
-
140
+
141
- Return True
141
+ Return True                               
142
142
 
143
143
  End Function
144
144
 
@@ -146,13 +146,13 @@
146
146
 
147
147
 
148
148
 
149
- Function Handler_Client(ByVal state As Object)
149
+ Function Handler_Client(ByVal state As Object)               
150
150
 
151
151
  Dim TempClient As TcpClient
152
152
 
153
153
 
154
154
 
155
- Try
155
+ Try                            
156
156
 
157
157
  Using Client As TcpClient = Server.AcceptTcpClient
158
158
 
@@ -162,125 +162,125 @@
162
162
 
163
163
  Threading.ThreadPool.QueueUserWorkItem(AddressOf Handler_Client)
164
164
 
165
+ End If                        
166
+
167
+ Clients.Add(Client)                        
168
+
169
+ TempClient = Client
170
+
171
+ 'Dim TX As New StreamWriter(Client.GetStream)
172
+
173
+ Dim RX As New StreamReader(Client.GetStream)
174
+
175
+ If RX.BaseStream.CanRead = True Then
176
+
177
+ While RX.BaseStream.CanRead = True
178
+
179
+ Dim RawData As String = RX.ReadLine
180
+
181
+ Richtextbox1.text += Client.Client.RemoteEndPoint.ToString + ">>" + RawData + vbNewLine                          
182
+
183
+ End While                            
184
+
165
185
  End If
166
186
 
167
- Clients.Add(Client)
187
+
168
-
169
- TempClient = Client
188
+
170
-
171
- 'Dim TX As New StreamWriter(Client.GetStream)
172
-
173
- Dim RX As New StreamReader(Client.GetStream)
174
-
175
- If RX.BaseStream.CanRead = True Then
189
+ If RX.BaseStream.CanRead = False Then              
190
+
176
-
191
+ Client.Close()                     
192
+
177
- While RX.BaseStream.CanRead = True
193
+ Clients.Remove(Client)                          
178
-
179
- Dim RawData As String = RX.ReadLine
180
-
181
- Richtextbox1.text += Client.Client.RemoteEndPoint.ToString + ">>" + RawData + vbNewLine
182
-
183
- End While
184
194
 
185
195
  End If
186
196
 
187
197
 
188
198
 
189
- If RX.BaseStream.CanRead = False Then
190
-
191
- Client.Close()
192
-
193
- Clients.Remove(Client)
194
-
195
- End If
196
-
197
-
198
-
199
199
  End Using
200
200
 
201
201
 
202
202
 
203
203
 
204
204
 
205
- Catch ex As Exception
205
+ Catch ex As Exception                      
206
-
206
+
207
- If TempClient.GetStream.CanRead = False Then
207
+ If TempClient.GetStream.CanRead = False Then                
208
-
208
+
209
- TempClient.Close()
209
+ TempClient.Close()                 
210
-
210
+
211
- Clients.Remove(TempClient)
211
+ Clients.Remove(TempClient)                   
212
-
212
+
213
- End If
213
+ End If                                  
214
214
 
215
215
  End Try
216
216
 
217
217
 
218
218
 
219
- Return True
219
+ Return True                               
220
220
 
221
221
  End Function
222
222
 
223
223
 
224
224
 
225
- Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
225
+ Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown                      
226
-
226
+
227
- If e.KeyCode = Keys.Enter Then
227
+ If e.KeyCode = Keys.Enter Then           
228
-
228
+
229
- e.SuppressKeyPress = True
229
+ e.SuppressKeyPress = True           
230
-
230
+
231
- If TextBox1.Text.Length > 0 Then
231
+ If TextBox1.Text.Length > 0 Then                    
232
-
232
+
233
- SendToClients(TextBox1.Text)
233
+ SendToClients(TextBox1.Text)                      
234
-
234
+
235
- TextBox1.Clear()
235
+ TextBox1.Clear()                         
236
-
236
+
237
- End If
237
+ End If                                  
238
-
238
+
239
- End If
239
+ End If                                   
240
240
 
241
241
  End Sub
242
242
 
243
243
 
244
244
 
245
- Function SendToClients(ByVal Data As String)
245
+ Function SendToClients(ByVal Data As String)                      
246
-
246
+
247
- If ServerStatus = True Then
247
+ If ServerStatus = True Then                            
248
-
248
+
249
- If Clients.Count > 0 Then
249
+ If Clients.Count > 0 Then                     
250
-
250
+
251
- Try
251
+ Try                         
252
252
 
253
253
  For Each Client As TcpClient In Clients
254
254
 
255
255
  Dim TX1 As New StreamWriter(Client.GetStream)
256
256
 
257
- TX1.WriteLine(Data)
257
+ TX1.WriteLine(Data)                       
258
-
258
+
259
- TX1.Flush()
259
+ TX1.Flush()                           
260
-
260
+
261
- Next
261
+ Next                          
262
-
262
+
263
- Catch ex As Exception
263
+ Catch ex As Exception                     
264
-
264
+
265
- SendToClients(Data)
265
+ SendToClients(Data)                     
266
-
266
+
267
- End Try
267
+ End Try                              
268
-
268
+
269
- End If
269
+ End If                                  
270
-
270
+
271
- End If
271
+ End If                               
272
-
272
+
273
- Return True
273
+ Return True                               
274
274
 
275
275
  End Function
276
276
 
277
277
 
278
278
 
279
- Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
279
+ Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 
280
-
281
-
282
-
280
+
281
+
282
+
283
- End Sub
283
+ End Sub                                   
284
284
 
285
285
  End Class
286
286
 

1

ソースコードにインデントの修正を行いました。タグをVB.netに修正しました。

2020/02/19 11:38

投稿

HiroyaIto
HiroyaIto

スコア5

test CHANGED
File without changes
test CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
 
24
24
 
25
- ### 該当のソースコード
25
+ ### 該当のソースコード```
26
26
 
27
27
  Imports System.IO
28
28
 
@@ -286,6 +286,10 @@
286
286
 
287
287
 
288
288
 
289
+ ```
290
+
291
+
292
+
289
293
  ### 試したこと
290
294
 
291
295
  textbox1に入力された文字をクライアントに送る仕様です。