AcceptAsync with multiples clients

Monkey Forums/Monkey Beginners/AcceptAsync with multiples clients

yiotoo(Posted 2014) [#1]
I am trying to make a server communicate with many clients.
I can start a server who is communicating with only one client.

But even if I create multitples sockets when I call from host the function nameofsocket[array].AcceptAsync(Self) only accepts the first client and ignores all the others clients.

What can I do?


nikoniko(Posted 2014) [#2]
You should implement ServerSocket type of sockets.

From M-X docs:

Class HttpServer Implements IOnAcceptComplete

	Method New( port:Int = 8090)
		_socket=New Socket( "server" )
		If Not _socket.Bind( "",port ) Error "Bind failed"
		_socket.AcceptAsync( Self )
	End
	
	Private
	
	Field _socket:Socket
	
	Method OnAcceptComplete:Void( socket:Socket,source:Socket )
		If Not socket Error "Accept error"
		Print "HttpServer: Accepted client connection"
		Local client:=New HttpServerClient( socket )
		'set accept waiting again
		_socket.AcceptAsync( Self )
	End
	


One connection client
Class HttpServerClient Implements IOnSendComplete,IOnReceiveComplete

	Method New( socket:Socket )
		_socket=socket
		_socket.ReceiveAsync _data,0,_data.Length,Self
	End
	
	Private
	
	Field _socket:Socket
	Field _data:=New DataBuffer( 2048 )
	
	Method OnReceiveComplete:Void( data:DataBuffer,offset:Int,count:Int,source:Socket )
		Print "HttpServer: Get data " + data.PeekString(0, count)
		If True 'Not count
			Print "HttpServer: Closing client connection"
			_socket.Close()
			Return
		Endif
		'_socket.SendAsync data,offset,count,Self
	End

	Method OnSendComplete:Void( data:DataBuffer,offset:Int,count:Int,source:Socket )
		'_socket.ReceiveAsync _data,0,_data.Length,Self
	End
	
	Method GetRequestHeader:Void()
	End Method
	
End Class 'HttpServerClient


But, anyway, M-X doesn't support multithread processing therefore while an one connection is processing all others are waiting when their On...events will be called.


yiotoo(Posted 2014) [#3]
I am using Tcp , i got from mark on bananas and from this topic : http://www.monkeycoder.co.nz/Community/posts.php?topic=7698
it's very similar from what you posted :).

btw, changing the ports works real nice! thanks!

But my objective is to have many connection , more than hundreds.

I could create in the server with AcceptAsync for hundreds of ports actives. And for the clients with ConnectAsync for those hundreds ports, and a FOR to try to connect to one port in the server, if doesn't , it could try another port.

but gerating one different port for each client could be bad, since it could be in conflict with other program who is using that port.

I was think something like, terraria, minecraft, tibia, who has just one port opened from the client.

One possible solution could be:
A host who opens many ports, the clients connects to those ports but the clients have the same number of port. Example:
Server port 90 responds the client 1 port 80
Server port 80 responds the client 2 port 80
Server port 70 responds the client 3 port 80
Server port 50 responds the client 4 port 80
Server port 40 responds the client 5 port 80
Server port 30 responds the client 6 port 80
Server port 20 responds the client 7 port 80
Server port 10 responds the client 8 port 80

but I don't know how to do that since ConnectAsync uses a host string to connect.


nikoniko(Posted 2014) [#4]
yiotoo wrote:
but gerating one different port for each client could be bad, since it could be in conflict with other program who is using that port.


MS Windows allows connection to 1024-65534 ports. Don't worry about free ports. Use ServerSocket/Bind Connection for.

yiotoo wrote:
I was think something like, terraria, minecraft, tibia, who has just one port opened from the client.


Usually udp is used for mega "connections" servers - it's fast and no port limitations.


yiotoo(Posted 2014) [#5]
thanks!! I will try to build something with udp!!! :)


Xaron(Posted 2014) [#6]
What's wrong with using just one port for all connections from clients?


yiotoo(Posted 2014) [#7]
Xaron, I would like to do it. It would be real nice , but I am failing to do this in TCP.
For UDP I am trying. (just need some free time to try in UDP , my work don't let me) :)