Results 1 to 26 of 26

Thread: Socket connection error

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2010
    Posts
    20

    Resolved Socket connection error

    Hello.
    I'm having an error in a server-client application I made using arrays. I don't exactly know what the problem is, but it's something like the second socket and higher don't connect to the listener. (?)

    Here is my server code:


    Code:
    Public Class Form1
    
    
    
        Dim max_players As Integer = 0
        Dim tcp_listener() As System.Net.Sockets.TcpListener
        Dim socket() As System.Net.Sockets.Socket
        Dim network_stream As System.Net.Sockets.NetworkStream
        Dim streamreader As System.IO.StreamReader
        Dim serving As Boolean = False
        Dim player_connected() As Boolean
     
       
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            CheckForIllegalCrossThreadCalls = False
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            If serving = False Then
                Dim slots As Integer = Replace(TextBox1.Text, " ", Nothing)
                If IsNumeric(slots) Then
                    If slots > 0 And slots < 11 Then
                        serving = True
                        Button1.Text = "Serving..."
                        Dim start_port As Integer = 21760
                        max_players = slots
                        ReDim socket(max_players)
                        ReDim tcp_listener(max_players)
                        ReDim player_connected(max_players)
                        For a As Integer = 0 To max_players - 1
                            player_connected(a) = False
                            tcp_listener(a) = New System.Net.Sockets.TcpListener(start_port)
                            tcp_listener(a).Start()
                            start_port = start_port + 1
                            Timer1.Start()
                            Timer5.Start()
                        Next
                        Dim thread As New System.Threading.Thread(AddressOf Listen)
                        thread.Start()
                    End If
                End If
            End If
    
        End Sub
    
    
    ' I added these test_connected booleans, these msgboxes and changed some things so I could trap the error as I was trying to chat with a friend of mine (two connections). Didn't work as well :(
      
        Dim test1_connected As Boolean = False
        Dim test2_connected As Boolean = False
    
        Private Sub Listen()
    
            Do While serving = True
                For a As Integer = 0 To max_players - 1
                    If test1_connected = True And test2_connected = True Then
                        MsgBox("0")
                        'Exit Sub
                    Else
                        If test1_connected = False Then
                            MsgBox("1")
                            socket(0) = tcp_listener(0).AcceptSocket()
                            test1_connected = True
                            network_stream = New System.Net.Sockets.NetworkStream(socket(0))
                            Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes("connection1|")
                            network_stream.Write(data, 0, data.Length)
                        Else
                            MsgBox("2")
                            socket(1) = tcp_listener(1).AcceptSocket()
                            MsgBox("yes123")
                            test2_connected = True
                            network_stream = New System.Net.Sockets.NetworkStream(socket(1))
                            Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes("connection1|")
                            network_stream.Write(data, 0, data.Length)
                            'Exit For
                        End If
                    End If
                    'network_stream = New System.Net.Sockets.NetworkStream(socket(a))
                    'Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes("connection1|")
                    'network_stream.Write(data, 0, data.Length)
                Next
            Loop
    
        End Sub
    
        
    End Class


    Client code:


    Code:
    Dim connecting As Boolean = False
        Dim tcp_client As System.Net.Sockets.TcpClient
        Dim current_port As Integer = "21760"
        Dim network_stream As System.Net.Sockets.NetworkStream
    
    
        Private Sub Connect(ByVal port As Integer)
    
            Try
                current_port = port
                tcp_client = New System.Net.Sockets.TcpClient
                tcp_client.Connect("ip.ip.ip.ip", port)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            If connecting = False Then
                connecting = True
                Button1.Text = "Connecting..."
                TextBox1.ReadOnly = True
                username = TextBox1.Text
                tcp_client = New System.Net.Sockets.TcpClient
                Dim thread As New System.Threading.Thread(AddressOf Connect)
                thread.Start(current_port)
                Timer4.Start()
            Else
                MsgBox("Connection failed.", MsgBoxStyle.Critical, "Error")
            End If
    
        End Sub
    
        Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
    
            If tcp_client.Connected = True Then
                Timer4.Stop()
            Else
                If connecting = True Then
                    Me.Text = Me.Text & "123"
                    If current_port = "21760" Then ' max port number
                        connecting = False
                    End If
                    Dim thread As New System.Threading.Thread(AddressOf Connect)
                    thread.Start(current_port + 1)
                    Timer4.Stop()
                    Timer4.Start()
                Else
                    Button1.Text = "Connect"
                    TextBox1.ReadOnly = False
                    Timer4.Stop()
                    MsgBox("Connection timeout.", MsgBoxStyle.Critical, "Error")
                End If
            End If
    
        End Sub
    
    
    
    End Class


    Thanks.
    Last edited by 1246; Jan 16th, 2011 at 10:01 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width