Quellcode durchsuchen

Updated socket bananas.

Mark Sibly vor 9 Jahren
Ursprung
Commit
8d232dddcc
2 geänderte Dateien mit 92 neuen und 18 gelöschten Zeilen
  1. 38 5
      bananas/echoserver/echoserver.monkey2
  2. 54 13
      bananas/echoserver_udp/echoserver_udp.monkey2

+ 38 - 5
bananas/echoserver/echoserver.monkey2

@@ -1,4 +1,28 @@
 
+#rem
+
+
+Quick quide to writing TCP client/server apps:
+
+
+* Server:
+
+1) Use Socket.Listen to create the server socket.
+
+2) Use Socket.Accept in a loop to accept new clients.
+
+3) Use Socket.Send and Socket.Receive to communicate with clients.
+
+
+* Client:
+
+1) Use Socket.Connect to connect to the listening server (must already be running!) and create a client socket.
+
+2) Use Socket.Send and Socket.Receive to communicate with the server.
+
+
+#end
+
 #Import "<mojox>"
 #Import "<mojo>"
 #Import "<std>"
@@ -7,6 +31,9 @@ Using mojox..
 Using mojo..
 Using std..
 
+Const HOST:="localhost"	'Note: Use "" for 'public' host?
+Const PORT:=40122
+
 Class MyWindow Extends Window
 
 	Method New()
@@ -18,11 +45,15 @@ Class MyWindow Extends Window
 	
 	Method Server()
 	
-		Local server:=Socket.Listen( 12345 )
+		Local server:=Socket.Listen( HOST,PORT )
 		If Not server print "Server: Failed to create server" ; Return
 		
 		Print "Server @"+server.Address+" listening"
 		
+		server.SetOption( "SO_REUSEADDR",1 )
+
+		server.SetOption( "TCP_NODELAY",1 )
+		
 		Repeat
 		
 			Local socket:=server.Accept()
@@ -58,12 +89,14 @@ Class MyWindow Extends Window
 	
 		Fiber.Sleep( .5 )
 	
-		Local socket:=Socket.Connect( "localhost",12345 )
-		If Not socket Print "Client: Couldn't connect to server" ; Return
+		Local client:=Socket.Connect( HOST,PORT )
+		If Not client Print "Client: Couldn't connect to server" ; Return
+		
+		Print "Client @"+client.Address+" connected to server @"+client.PeerAddress
 		
-		Print "Client @"+socket.Address+" connected to server @"+socket.PeerAddress
+		client.SetOption( "TCP_NODELAY",1 )
 		
-		Local stream:=New SocketStream( socket )
+		Local stream:=New SocketStream( client )
 
 		For Local i:=0 Until 100
 		

+ 54 - 13
bananas/echoserver_udp/echoserver_udp.monkey2

@@ -1,4 +1,27 @@
 
+#rem
+
+Quick guide to writing UDP client/server apps:
+
+* Server:
+
+1) Create server socket using Socket.Bind (not Listen!).
+
+2) Wait for client messages using Socket.ReceiveFrom.
+
+3) Use the SocketAddress filled in by ReceiveFrom to determine the client the message is from.
+
+4) Use the same SocketAddress with Socket.SendTo to reply to the client.
+
+
+* Client:
+
+1) Connect to server using Socket.Connect with SocketType.Stream as the last parameter.
+
+2) Communicate with the server using Socket.Send and Socket.Receive.
+
+#end
+
 #Import "<mojox>"
 #Import "<mojo>"
 #Import "<std>"
@@ -7,12 +30,16 @@ Using mojox..
 Using mojo..
 Using std..
 
+Const HOST:="localhost"	'Note: Use "" for 'public' host.
+Const PORT:=40123
+
 Class MyWindow Extends Window
 
 	Method New()
 	
 		New Fiber( Server )
 		
+		
 		For Local i:=0 Until 5
 			New Fiber( Client )
 		Next
@@ -20,10 +47,15 @@ Class MyWindow Extends Window
 	
 	Method Server()
 	
-		Local socket:=Socket.Bind( 12345 )
-		If Not socket print "Server: Failed to create server" ; Return
+		Local server:=Socket.Bind( HOST,PORT )
+		If Not server print "Server: Failed to create server" ; Return
+		
+		Print "Server @"+server.Address+" ready"
 		
-		Print "Server @"+socket.Address+" ready"
+		server.SetOption( "SO_REUSEADDR",1 )
+		
+		'To keep track of connected clients...
+		Local clients:=New Map<SocketAddress,Int>
 		
 		Local addr:=New SocketAddress
 				
@@ -31,17 +63,24 @@ Class MyWindow Extends Window
 		
 			Local data:Int
 			
-			If socket.ReceiveFrom( Varptr data,4,addr )<>4 Exit
+			If server.ReceiveFrom( Varptr data,4,addr )<>4 Exit
 			
 			Print "Server received msg:"+data+" from client @"+addr
 			
+			'check if client exists
+			If Not clients[addr]
+				Local id:=clients.Count()+1
+				clients[ New SocketAddress( addr ) ]=id
+				Print "New Client! id="+id
+			Endif
+			
 			data=-data
 				
-			socket.SendTo( Varptr data,4,addr )
-				
+			server.SendTo( Varptr data,4,addr )
+
 		Forever
 		
-		socket.Close()
+		server.Close()
 		
 	End
 	
@@ -55,20 +94,22 @@ Class MyWindow Extends Window
 	
 		Fiber.Sleep( .5 )	'wait a bit for server to start
 		
-		Local socket:=Socket.Connect( "localhost",12345,SocketType.Datagram )
-		If Not socket Print "Client("+id+"): Couldn't connect to server" ; Return
+		Local client:=Socket.Connect( HOST,PORT,SocketType.Datagram )
+		If Not client Print "Client("+id+"): Couldn't connect to server" ; Return
+		
+		Print "Client("+id+") @"+client.Address+" connected to @"+client.PeerAddress
 		
-		Print "Client("+id+") @"+socket.Address+" connected to @"+socket.PeerAddress
+		Local address:=New SocketAddress
 		
 		For Local i:=0 Until 10
 		
-			Fiber.Sleep( Rnd( .1,.2 ) )
+			Fiber.Sleep( Rnd( .2,.4 ) )
 		
 			Local data:Int=i*10
 			
-			socket.Send( Varptr data,4 )
+			client.Send( Varptr data,4 )
 			
-			If socket.Receive( Varptr data,4 )<>4 Exit
+			If client.Receive( Varptr data,4 )<>4 Exit
 			
 			Print "Client("+id+") received reply:"+data+" from server"