B4J Question i want MORE than one connection

Discussion in 'B4J Questions' started by ArminKH, May 27, 2015.

  1. ArminKH

    ArminKH Well-Known Member

    hello here is my code
    'Non-UI application (console / server application)
    #Region  Project Attributes
    #MergeLibraries: True
    #AdditionalJar: mysql-connector-java-5.1.35-bin.jar
    #End Region

    Sub Process_Globals
    Public ServerSocket1 As ServerSocket
    Dim SH As SocketHandler
    Private Socket1 As Socket
    Private AStreams As AsyncStreamsObject
    Dim timer1 As Timer
    End Sub

    Sub AppStart (Args() As String)
    'only required in a console app
    End Sub
    Sub ServerSocket1_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
            Socket1 = NewSocket

    End If
    'Continue listening to new incoming connections
    End Sub

    Sub AStreams_Terminated
    End Sub

    Sub AStreams_NewObject(Key As String, Value As Object)
    Select Key
    Case "Image"
    Dim bmp As Image = Value
    Log("Received image")
    Case "Message"
    Dim message As String = Value
    Log("Received message: " & message)      
    End Select
    End Sub

    Sub AStreams_ObjectSent (Key As String)
    Log(Key & " Sent")
    End Sub

    Sub SendMessage2Client
    '    AStreams.WriteObject("Message", "Hello From Server My frieandzzzzzzzzzz")
    End Sub

    Sub timer1_tick
    End Sub
    my server send time to client
    but when a new device connected to server then my previeus connection closed and sending data to my first connected device stoped
  2. Daestrum

    Daestrum Well-Known Member Licensed User

    It's probably because you keep overwriting socket1 every time you get a new connection.
    When you get a new connection, stick it in a list, then in your timer_tiick routine, read the list and set the astreams to the socket from the list, send the data, then go to next item and repeat until you reach the end of the list.

    (I am tired at present so it may not make sense when I read it tomorrow).
  3. Erel

    Erel Administrator Staff Member Licensed User

    ArminKH likes this.
  4. ArminKH

    ArminKH Well-Known Member

    Erel likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice