teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

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

2020/02/19 11:41

投稿

HiroyaIto
HiroyaIto

スコア5

title CHANGED
File without changes
body CHANGED
@@ -11,135 +11,135 @@
11
11
  受信用richtextboxにデータが表示しようとしてしまって、クライアント側にデータが送られません。
12
12
 
13
13
  ### 該当のソースコード```
14
- Imports System.IO                             
14
+ Imports System.IO
15
- Imports System.Net                       
15
+ Imports System.Net
16
- Imports System.Net.Sockets                  
16
+ Imports System.Net.Sockets
17
17
  Imports System.Threading
18
18
 
19
19
 
20
20
  Public Class Form1
21
21
 
22
- Dim ServerStatus As Boolean = False                
22
+ Dim ServerStatus As Boolean = False
23
- Dim ServerTrying As Boolean = False                
23
+ Dim ServerTrying As Boolean = False
24
- Dim Server As TcpListener                 
24
+ Dim Server As TcpListener
25
25
  Dim Clients As New List(Of TcpClient)
26
26
 
27
27
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
28
- CheckForIllegalCrossThreadCalls = False                   
28
+ CheckForIllegalCrossThreadCalls = False
29
29
  End Sub
30
30
 
31
31
 
32
32
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
33
- StartServer()                                 
33
+ StartServer()
34
34
  End Sub
35
35
 
36
36
 
37
37
 
38
- Function StartServer()                  
38
+ Function StartServer()
39
- If ServerStatus = False Then                
39
+ If ServerStatus = False Then
40
- ServerTrying = True                            
40
+ ServerTrying = True
41
- Try                
41
+ Try
42
- Server = New TcpListener(IPAddress.Any, 4305)           
42
+ Server = New TcpListener(IPAddress.Any, 4305)
43
- Server.Start()                   
43
+ Server.Start()
44
44
  ServerStatus = True
45
45
  Threading.ThreadPool.QueueUserWorkItem(AddressOf Handler_Client)
46
- Catch ex As Exception                
46
+ Catch ex As Exception
47
- ServerStatus = False                       
47
+ ServerStatus = False
48
- End Try                         
48
+ End Try
49
- ServerTrying = False                           
49
+ ServerTrying = False
50
- End If                                 
50
+ End If
51
- Return True                                    
51
+ Return True
52
52
  End Function
53
53
 
54
54
  Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
55
- StopServer()                                  
55
+ StopServer()
56
56
  End Sub
57
57
 
58
- Function StopServer()                              
58
+ Function StopServer()
59
- If ServerStatus = True Then                            
59
+ If ServerStatus = True Then
60
- ServerTrying = True                          
60
+ ServerTrying = True
61
- Try                            
61
+ Try
62
- For Each Client As TcpClient In Clients                  
62
+ For Each Client As TcpClient In Clients
63
- Client.Close()                          
63
+ Client.Close()
64
- Next                                    
64
+ Next
65
- Server.Stop()                              
65
+ Server.Stop()
66
- ServerStatus = False                            
66
+ ServerStatus = False
67
- Catch ex As Exception                             
67
+ Catch ex As Exception
68
- StopServer()                              
68
+ StopServer()
69
- End Try                                 
69
+ End Try
70
- End If                                 
70
+ End If
71
- Return True                               
71
+ Return True
72
72
  End Function
73
73
 
74
74
 
75
- Function Handler_Client(ByVal state As Object)               
75
+ Function Handler_Client(ByVal state As Object)
76
76
  Dim TempClient As TcpClient
77
77
 
78
- Try                            
78
+ Try
79
79
  Using Client As TcpClient = Server.AcceptTcpClient
80
80
 
81
81
  If ServerTrying = False Then
82
82
  Threading.ThreadPool.QueueUserWorkItem(AddressOf Handler_Client)
83
- End If                        
83
+ End If
84
- Clients.Add(Client)                        
84
+ Clients.Add(Client)
85
85
  TempClient = Client
86
86
  'Dim TX As New StreamWriter(Client.GetStream)
87
87
  Dim RX As New StreamReader(Client.GetStream)
88
88
  If RX.BaseStream.CanRead = True Then
89
89
  While RX.BaseStream.CanRead = True
90
90
  Dim RawData As String = RX.ReadLine
91
- Richtextbox1.text += Client.Client.RemoteEndPoint.ToString + ">>" + RawData + vbNewLine                          
91
+ Richtextbox1.text += Client.Client.RemoteEndPoint.ToString + ">>" + RawData + vbNewLine
92
- End While                            
92
+ End While
93
93
  End If
94
94
 
95
- If RX.BaseStream.CanRead = False Then              
95
+ If RX.BaseStream.CanRead = False Then
96
- Client.Close()                     
96
+ Client.Close()
97
- Clients.Remove(Client)                          
97
+ Clients.Remove(Client)
98
98
  End If
99
99
 
100
100
  End Using
101
101
 
102
102
 
103
- Catch ex As Exception                      
103
+ Catch ex As Exception
104
- If TempClient.GetStream.CanRead = False Then                
104
+ If TempClient.GetStream.CanRead = False Then
105
- TempClient.Close()                 
105
+ TempClient.Close()
106
- Clients.Remove(TempClient)                   
106
+ Clients.Remove(TempClient)
107
- End If                                  
107
+ End If
108
108
  End Try
109
109
 
110
- Return True                               
110
+ Return True
111
111
  End Function
112
112
 
113
- Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown                      
113
+ Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
114
- If e.KeyCode = Keys.Enter Then           
114
+ If e.KeyCode = Keys.Enter Then
115
- e.SuppressKeyPress = True           
115
+ e.SuppressKeyPress = True
116
- If TextBox1.Text.Length > 0 Then                    
116
+ If TextBox1.Text.Length > 0 Then
117
- SendToClients(TextBox1.Text)                      
117
+ SendToClients(TextBox1.Text)
118
- TextBox1.Clear()                         
118
+ TextBox1.Clear()
119
- End If                                  
119
+ End If
120
- End If                                   
120
+ End If
121
121
  End Sub
122
122
 
123
- Function SendToClients(ByVal Data As String)                      
123
+ Function SendToClients(ByVal Data As String)
124
- If ServerStatus = True Then                            
124
+ If ServerStatus = True Then
125
- If Clients.Count > 0 Then                     
125
+ If Clients.Count > 0 Then
126
- Try                         
126
+ Try
127
127
  For Each Client As TcpClient In Clients
128
128
  Dim TX1 As New StreamWriter(Client.GetStream)
129
- TX1.WriteLine(Data)                       
129
+ TX1.WriteLine(Data)
130
- TX1.Flush()                           
130
+ TX1.Flush()
131
- Next                          
131
+ Next
132
- Catch ex As Exception                     
132
+ Catch ex As Exception
133
- SendToClients(Data)                     
133
+ SendToClients(Data)
134
- End Try                              
134
+ End Try
135
- End If                                  
135
+ End If
136
- End If                               
136
+ End If
137
- Return True                               
137
+ Return True
138
138
  End Function
139
139
 
140
- Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 
140
+ Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
141
141
 
142
- End Sub                                   
142
+ End Sub
143
143
  End Class
144
144
 
145
145
  ```

2

2020/02/19 11:41

投稿

HiroyaIto
HiroyaIto

スコア5

title CHANGED
File without changes
body CHANGED
@@ -11,135 +11,135 @@
11
11
  受信用richtextboxにデータが表示しようとしてしまって、クライアント側にデータが送られません。
12
12
 
13
13
  ### 該当のソースコード```
14
- Imports System.IO
14
+ Imports System.IO                             
15
- Imports System.Net
15
+ Imports System.Net                       
16
- Imports System.Net.Sockets
16
+ Imports System.Net.Sockets                  
17
17
  Imports System.Threading
18
18
 
19
19
 
20
20
  Public Class Form1
21
21
 
22
- Dim ServerStatus As Boolean = False
22
+ Dim ServerStatus As Boolean = False                
23
- Dim ServerTrying As Boolean = False
23
+ Dim ServerTrying As Boolean = False                
24
- Dim Server As TcpListener
24
+ Dim Server As TcpListener                 
25
25
  Dim Clients As New List(Of TcpClient)
26
26
 
27
27
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
28
- CheckForIllegalCrossThreadCalls = False
28
+ CheckForIllegalCrossThreadCalls = False                   
29
29
  End Sub
30
30
 
31
31
 
32
32
  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
33
- StartServer()
33
+ StartServer()                                 
34
34
  End Sub
35
35
 
36
36
 
37
37
 
38
- Function StartServer()
38
+ Function StartServer()                  
39
- If ServerStatus = False Then
39
+ If ServerStatus = False Then                
40
- ServerTrying = True
40
+ ServerTrying = True                            
41
- Try
41
+ Try                
42
- Server = New TcpListener(IPAddress.Any, 4305)
42
+ Server = New TcpListener(IPAddress.Any, 4305)           
43
- Server.Start()
43
+ Server.Start()                   
44
44
  ServerStatus = True
45
45
  Threading.ThreadPool.QueueUserWorkItem(AddressOf Handler_Client)
46
- Catch ex As Exception
46
+ Catch ex As Exception                
47
- ServerStatus = False
47
+ ServerStatus = False                       
48
- End Try
48
+ End Try                         
49
- ServerTrying = False
49
+ ServerTrying = False                           
50
- End If
50
+ End If                                 
51
- Return True
51
+ Return True                                    
52
52
  End Function
53
53
 
54
54
  Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
55
- StopServer()
55
+ StopServer()                                  
56
56
  End Sub
57
57
 
58
- Function StopServer()
58
+ Function StopServer()                              
59
- If ServerStatus = True Then
59
+ If ServerStatus = True Then                            
60
- ServerTrying = True
60
+ ServerTrying = True                          
61
- Try
61
+ Try                            
62
- For Each Client As TcpClient In Clients
62
+ For Each Client As TcpClient In Clients                  
63
- Client.Close()
63
+ Client.Close()                          
64
- Next
64
+ Next                                    
65
- Server.Stop()
65
+ Server.Stop()                              
66
- ServerStatus = False
66
+ ServerStatus = False                            
67
- Catch ex As Exception
67
+ Catch ex As Exception                             
68
- StopServer()
68
+ StopServer()                              
69
- End Try
69
+ End Try                                 
70
- End If
70
+ End If                                 
71
- Return True
71
+ Return True                               
72
72
  End Function
73
73
 
74
74
 
75
- Function Handler_Client(ByVal state As Object)
75
+ Function Handler_Client(ByVal state As Object)               
76
76
  Dim TempClient As TcpClient
77
77
 
78
- Try
78
+ Try                            
79
79
  Using Client As TcpClient = Server.AcceptTcpClient
80
80
 
81
81
  If ServerTrying = False Then
82
82
  Threading.ThreadPool.QueueUserWorkItem(AddressOf Handler_Client)
83
- End If
83
+ End If                        
84
- Clients.Add(Client)
84
+ Clients.Add(Client)                        
85
85
  TempClient = Client
86
86
  'Dim TX As New StreamWriter(Client.GetStream)
87
87
  Dim RX As New StreamReader(Client.GetStream)
88
88
  If RX.BaseStream.CanRead = True Then
89
89
  While RX.BaseStream.CanRead = True
90
90
  Dim RawData As String = RX.ReadLine
91
- Richtextbox1.text += Client.Client.RemoteEndPoint.ToString + ">>" + RawData + vbNewLine
91
+ Richtextbox1.text += Client.Client.RemoteEndPoint.ToString + ">>" + RawData + vbNewLine                          
92
- End While
92
+ End While                            
93
93
  End If
94
94
 
95
- If RX.BaseStream.CanRead = False Then
95
+ If RX.BaseStream.CanRead = False Then              
96
- Client.Close()
96
+ Client.Close()                     
97
- Clients.Remove(Client)
97
+ Clients.Remove(Client)                          
98
98
  End If
99
99
 
100
100
  End Using
101
101
 
102
102
 
103
- Catch ex As Exception
103
+ Catch ex As Exception                      
104
- If TempClient.GetStream.CanRead = False Then
104
+ If TempClient.GetStream.CanRead = False Then                
105
- TempClient.Close()
105
+ TempClient.Close()                 
106
- Clients.Remove(TempClient)
106
+ Clients.Remove(TempClient)                   
107
- End If
107
+ End If                                  
108
108
  End Try
109
109
 
110
- Return True
110
+ Return True                               
111
111
  End Function
112
112
 
113
- Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
113
+ Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown                      
114
- If e.KeyCode = Keys.Enter Then
114
+ If e.KeyCode = Keys.Enter Then           
115
- e.SuppressKeyPress = True
115
+ e.SuppressKeyPress = True           
116
- If TextBox1.Text.Length > 0 Then
116
+ If TextBox1.Text.Length > 0 Then                    
117
- SendToClients(TextBox1.Text)
117
+ SendToClients(TextBox1.Text)                      
118
- TextBox1.Clear()
118
+ TextBox1.Clear()                         
119
- End If
119
+ End If                                  
120
- End If
120
+ End If                                   
121
121
  End Sub
122
122
 
123
- Function SendToClients(ByVal Data As String)
123
+ Function SendToClients(ByVal Data As String)                      
124
- If ServerStatus = True Then
124
+ If ServerStatus = True Then                            
125
- If Clients.Count > 0 Then
125
+ If Clients.Count > 0 Then                     
126
- Try
126
+ Try                         
127
127
  For Each Client As TcpClient In Clients
128
128
  Dim TX1 As New StreamWriter(Client.GetStream)
129
- TX1.WriteLine(Data)
129
+ TX1.WriteLine(Data)                       
130
- TX1.Flush()
130
+ TX1.Flush()                           
131
- Next
131
+ Next                          
132
- Catch ex As Exception
132
+ Catch ex As Exception                     
133
- SendToClients(Data)
133
+ SendToClients(Data)                     
134
- End Try
134
+ End Try                              
135
- End If
135
+ End If                                  
136
- End If
136
+ End If                               
137
- Return True
137
+ Return True                               
138
138
  End Function
139
139
 
140
- Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
140
+ Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 
141
141
 
142
- End Sub
142
+ End Sub                                   
143
143
  End Class
144
144
 
145
145
  ```

1

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

2020/02/19 11:38

投稿

HiroyaIto
HiroyaIto

スコア5

title CHANGED
File without changes
body CHANGED
@@ -10,7 +10,7 @@
10
10
  ところが、サーバからクライアントにデータを送信(writeline)すると、サーバの
11
11
  受信用richtextboxにデータが表示しようとしてしまって、クライアント側にデータが送られません。
12
12
 
13
- ### 該当のソースコード
13
+ ### 該当のソースコード```
14
14
  Imports System.IO
15
15
  Imports System.Net
16
16
  Imports System.Net.Sockets
@@ -142,6 +142,8 @@
142
142
  End Sub
143
143
  End Class
144
144
 
145
+ ```
146
+
145
147
  ### 試したこと
146
148
  textbox1に入力された文字をクライアントに送る仕様です。
147
149
  また、クライアントからの文字は、Richtextbox1に表示される仕様です。