B4J Question server socket

Jose Cadenas

Member
Licensed User
Longtime User
i am use Private server As ServerSocket Private astream As AsyncStreams, but when connect other client disconect el first cliente


B4X:
Sub Process_Globals
    Private server As ServerSocket
    Private astream As AsyncStreams
End Sub

Sub AppStart (Args() As String)
    server.Initialize(51042, "server")
    server.Listen
    StartMessageLoop
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub server_NewConnection (Successful As Boolean, NewSocket As Socket)
    Log("New connection")
    If Successful Then
        astream.
        astream.Initialize(NewSocket.InputStream, NewSocket.OutputStream, "astream")
    End If
    server.Listen
End Sub

Sub astream_NewData (Buffer() As Byte)
    Log(BytesToString(Buffer, 0, Buffer.Length, "utf8"))
    astream.Write("received!".GetBytes("utf8"))
End Sub
 
Last edited:

Knoppi

Active Member
Licensed User
Longtime User
Please use [CODE] instead of <code>
or
UseCodeTags.PNG
 
Upvote 0

Jose Cadenas

Member
Licensed User
Longtime User
Think carefully whether you want to implement the server yourself. There are simpler and better solutions such as a real http server (jServer) or MQTT.

Example of a multiclient server using ServerSocket: https://www.b4x.com/android/forum/t...d-with-socket-and-asyncstreams.74320/#content

thank you for you response... but this example open one port for client. i have more de 5000 client. is possible open one port. in visual basic 6 with array winsock manage many clients in one port. is possible?
 
Upvote 0

Jose Cadenas

Member
Licensed User
Longtime User
Think carefully whether you want to implement the server yourself. There are simpler and better solutions such as a real http server (jServer) or MQTT.

Example of a multiclient server using ServerSocket: https://www.b4x.com/android/forum/t...d-with-socket-and-asyncstreams.74320/#content

this person use poolconections. but i not know how declare poolConnection

B4X:
Sub AppStart (Args() As String)
    Try
        poolConnection.Initialize
        SenderSckt.Initialize(port,"SenderSocket")
        SenderSckt.Listen
    Catch
        Log(LastException)
    End Try
    Log("Server IP: "&SenderSckt.GetMyIP)
    Log("Ready to send")
    Log("Listening...")
    StartMessageLoop
End Sub

Private Sub SenderSocket_NewConnection(Successful As Boolean,NewSocket As Socket)
    If Not(Successful) Then
        Log("Unsuccessful connection from: "&NewSocket.RemoteAddress)
        Return
    End If
    Log("New connection from: "&NewSocket.RemoteAddress)
    poolConnection.Put(newStream,NewSocket.RemoteAddress)
    SenderSckt.Listen
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

OliverA

Expert
Licensed User
Longtime User
but this example open one port for client
I think you may not understand how sockets/server sockets work, nor how the sample program that @Erel pointed you towards handles multiple connections (because it does). On the server side, the port should always be the same (with some exceptions, such as passive mode for FTP). This same port can handle multiple client connections. This one port can distinguish the connections by the client's IP address and port number. Each time a new connection is made to the server, the ssocket_NewConnection event is fired (class FTPServer class), which then passes the newly created socket to a new instance of the FTPClient class. How many of these clients that can be spun up is pretty much limited by your server resources (processing power, memory, port availability, etc). The FTPServer software has some additional complexity because it tracks the count of clients, the logging of the client counts and the creation of a data channel. Otherwise, this is a really good example on how to handle multiple clients..
 
Upvote 0

Jose Cadenas

Member
Licensed User
Longtime User
thank you for you response.
you're right, the problem with the example FTP is that I have to open many ports to connect a large number of clients. and in my way of thinking is not convenient this example would work well if it were few customers. excuse my bad english
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I have to open many ports to connect a large number of clients. and in my way of thinking is not convenient this example would work well if it were few customers
I don't know how you get the opinion that the FTP server cannot handle many connections via a single port. Just for fun, I stripped out a lot of the innards of the FTP server and used it as a basis for a basic Echo Server (of my own creation. This may not follow any true conventions). I also created a client application by modifying the JavaFX application found here (https://www.b4x.com/android/forum/threads/b4x-network-asyncstreams-b4xserializator.72149/). This client can be used to create many connections (up to 800 per JavaFX application instance) to the server. In my own testing, I have started 10 of these clients and created 800 connections per client to the server for a total of 8000 connections without any issues, nor did I require more than one port on the server. So I don't know, nor do I understand, where the perception comes from that only very few customers could connect.

I'm attaching the files for both the server (EchoServerViaClasses.zip) and the client (EchoServerTester.zip). Both should be compiled in Release mode and ran from the command line. I was using Windows 10, opened 11 command prompts, ran the server in one and the other 10 clients in the other command prompts.When you start the server, it will log an IP address and port number. Use that information to fill out the "Other computer" input box in the client. If the IP is 192.168.1.2 and the port is 51041, then enter 192.168.1.2:51041. Click on the connect button to connect to the running server. Then fill out a number from 1 to 800 in the "To Connect" box and hit the Send button to connect. You can have up to 800 connections per client. Start more clients and repeat to see how many clients connections can be created. This is far from a complete real world problem, but it does show that one port can easily accept thousands of connections.
 

Attachments

  • EchoServerViaClasses.zip
    9.6 KB · Views: 196
  • EchoServerTester.zip
    5.2 KB · Views: 213
Upvote 0
Top