1246
01-15-2011, 04:40 PM
Hello.
I am having an error in my server-client application where the first connect attempt is successful but the second is not.
Server code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not serving Then
Dim slots As Integer = CInt(Replace(TextBox1.Text, " ", Nothing))
If IsNumeric(slots) Then
If slots > 0 And slots < 11 Then
serving = True
Button1.Text = "Serving..."
max_players = slots
Dim port As Integer = 21760
ReDim tcp_client(max_players)
Dim thread As New System.Threading.Thread(AddressOf Listen)
thread.IsBackground = True
thread.Start()
End If
End If
End If
End Sub
Dim port As Integer = 21760
Dim number As Integer = 0
Private Sub Listen(ByVal index As Object)
Do Until number = max_players -1
Try
tcp_listener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, port)
tcp_listener.Start()
tcp_client(number) = tcp_listener.AcceptTcpClient
tcp_listener.Stop()
Dim data As String = "connection1|"
Dim stream_writer As New System.IO.StreamWriter(tcp_client(number).GetStream)
stream_writer.Write(data)
stream_writer.Flush()
tcp_client(number).GetStream.BeginRead(readBuffer, 0, 255, AddressOf Data_Arrival, Nothing)
port = port + 1
number = number + 1
Catch ex As Exception
MsgBox(ex.Message)
End Try
Loop
End Sub
Client code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not connecting Then
connecting = True
Button1.Text = "Connecting..."
TextBox1.ReadOnly = True
username = TextBox1.Text
Dim thread As New System.Threading.Thread(AddressOf Connect)
thread.IsBackground = True
thread.Start(current_port)
Timer4.Start()
End If
End Sub
Private Sub Connect(ByVal port As Object)
Try
current_port = CInt(port)
tcp_client = New System.Net.Sockets.TcpClient("ip.ip.ip.ip", CInt(port))
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
If you need any further info just ask.
I am having an error in my server-client application where the first connect attempt is successful but the second is not.
Server code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not serving Then
Dim slots As Integer = CInt(Replace(TextBox1.Text, " ", Nothing))
If IsNumeric(slots) Then
If slots > 0 And slots < 11 Then
serving = True
Button1.Text = "Serving..."
max_players = slots
Dim port As Integer = 21760
ReDim tcp_client(max_players)
Dim thread As New System.Threading.Thread(AddressOf Listen)
thread.IsBackground = True
thread.Start()
End If
End If
End If
End Sub
Dim port As Integer = 21760
Dim number As Integer = 0
Private Sub Listen(ByVal index As Object)
Do Until number = max_players -1
Try
tcp_listener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, port)
tcp_listener.Start()
tcp_client(number) = tcp_listener.AcceptTcpClient
tcp_listener.Stop()
Dim data As String = "connection1|"
Dim stream_writer As New System.IO.StreamWriter(tcp_client(number).GetStream)
stream_writer.Write(data)
stream_writer.Flush()
tcp_client(number).GetStream.BeginRead(readBuffer, 0, 255, AddressOf Data_Arrival, Nothing)
port = port + 1
number = number + 1
Catch ex As Exception
MsgBox(ex.Message)
End Try
Loop
End Sub
Client code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not connecting Then
connecting = True
Button1.Text = "Connecting..."
TextBox1.ReadOnly = True
username = TextBox1.Text
Dim thread As New System.Threading.Thread(AddressOf Connect)
thread.IsBackground = True
thread.Start(current_port)
Timer4.Start()
End If
End Sub
Private Sub Connect(ByVal port As Object)
Try
current_port = CInt(port)
tcp_client = New System.Net.Sockets.TcpClient("ip.ip.ip.ip", CInt(port))
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
If you need any further info just ask.