What is the best way for a tcp server to accept multiple tcp clients? The clients have no way of knowing what ports are already used by the server. Is there some way to setup portforwarding on the server?
Printable View
What is the best way for a tcp server to accept multiple tcp clients? The clients have no way of knowing what ports are already used by the server. Is there some way to setup portforwarding on the server?
Well most servers use a constant port number such as 10000 or something like that. Then the user connects to the server's ip address and that port number. The port number may be in the program by default or available on the program/game's website.
I understand the constant port number. But if there are multiple clients can they all be connected on the same port?
Yes.
Could you explain to me how to do that?
This is what I have so far:
Server
ClientCode:Dim client(9) As TcpClient
Dim server As TcpListener
Dim listthread As New System.Threading.Thread(AddressOf accept)
Sub createthread()
buttonstate(True)
Dim i As Integer
For i = 0 To client.Length - 1
client(i) = New TcpClient()
Next
server = New TcpListener(IPAddress.Any, 2999)
server.Start()
listthread.Start()
End Sub
Sub accept()
Dim j As Integer = 0
Dim i As Integer
While 1
For i = 0 To client.Length - 1
If client(i).Client.Connected Then
j = i
Exit For
End If
Next
Try
server.Start()
client(j) = server.AcceptTcpClient()
setcresults(client(j).Client.RemoteEndPoint.ToString & ": Connected")
Dim stream As NetworkStream = client(j).GetStream
Catch ex As Exception
MsgBox(ex.Message)
End Try
End While
End Sub
Code:Dim networkstream As System.Net.Sockets.NetworkStream
Dim tcpclient As New Net.Sockets.TcpClient()
Sub connect()
While 1
Try
Try
tcpclient.Connect("sccrstvn93.no-ip.org", 2999)
Catch ex As Exception
MsgBox(ex.Message)
End Try
While 1
networkstream = tcpclient.GetStream
acceptdata()
executecommand(clientdata)
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
End While
End Sub