Home > Software engineering >  Consult the program how to handle multiple TCP connections at the same time
Consult the program how to handle multiple TCP connections at the same time

Time:09-26

Every brother, younger brother novice VB, please grant instruction:
Environment: visual studio 2015 and visual Basic
Reference: https://msdn.microsoft.com/zh-cn/library/system.net.sockets.tcplistener.aspx? Cs - save - lang=1 & amp; Cs - lang=vb# code snippet - 2
Transformation: the original program inside the try removing the catch program consists of a button control start
Operation: basically successful, can accept the request from the client and make the corresponding right
Problem: the program after entering while statement, what other things can't do it, are merely waiting for the client's request; Even I did a quit button in the program interface to exit the program also not line,
Hope: application in monitoring the client request, can also allow me to do some other things, must do some other input operation on the interface, allow me to press button in the interface,

Thank you master!

The main program is as follows:
 Dim server As TcpListener 
Server=Nothing

'Set the TcpListener on port 13000.
Dim the port As Int32=13000
Dim localAddr As IPAddress=IPAddress. Parse (" 127.0.0.1 ")
Server=New TcpListener (localAddr, port)

'Start listening for client requests.
Server. The Start ()

'Buffer for reading data
Dim bytes (1024) As Byte
Dim the data As String=Nothing
While True
'Enter the listening loop.
MsgBox (" Waiting for a connection...
")'Perform a blocking call to accept requests.
'You could also user for server AcceptSocket () here.
Dim client As TcpClient=server. AcceptTcpClient ()
MsgBox (" Connected!" )
Data=https://bbs.csdn.net/topics/Nothing

'Get a stream object for reading and writing
Dim stream As NetworkStream=client. GetStream ()
Dim As Int32 I

'Loop to receive all the data sent by the client.
I=stream. Read (0 bytes, bytes. The Length)
While (I & lt;> 0)
'Translate data bytes to a ASCII string.
Data=(0 bytes, I https://bbs.csdn.net/topics/System.Text.Encoding.ASCII.GetString
MsgBox (" Received: "& amp; Data)
'the Process the data sent by the client.
Data=(https://bbs.csdn.net/topics/data.ToUpper)
Dim MSG As Byte ()=System. Text. Encoding. The ASCII. GetBytes (data)

'the Send back a response.
Stream. Write (MSG, 0, MSG. Length)
MsgBox (" Sent: "& amp; Data)

I=stream. Read (0 bytes, bytes. The Length)

End While

'the Shutdown and end connection
Client. The Close ()


End While

CodePudding user response:

Why not use WinScok controls?

CodePudding user response:

Vb.net?
Use multithreading.
Put the monitored while in the sub

CodePudding user response:

 Private Sub Button1_Click (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles for. Click 
Dim t1 As Threading. Thread
Dim t2 As Threading. Thread
T1=New Threading. Thread (AddressOf your sub)
T2=New Threading. Thread (AddressOf your sub)
T1. The Start ()
T2. The Start ()
End Sub
  • Related