About Monkey 2 › Forums › Monkey 2 Programming Help › Client Server Separation?
This topic contains 4 replies, has 3 voices, and was last updated by 
 scurty 2 years, 3 months ago.
- 
		AuthorPosts
 - 
		
			
				
December 8, 2016 at 6:51 pm #5663
I’m trying to separate the client and server Methods from the “echoserver” banana into two separate apps to basically run the server side on one PC and the client on another and send data between these two apps
The problem is the server getting a memory access violation as soon as the client is accepted. Don’t know why…
network.monkey2
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116Namespace Network'Const HOST := "localhost" ' HOST ADDRESS [http://] [localhost] [127.0.0.1]'Const PORT := 40122Class NetServerField HOST:StringField PORT:IntMethod New(host:String, port:Int)Self.HOST = hostSelf.PORT = port'Process()End MethodMethod Process:Void()' Create the Server SocketLocal SERVER := Socket.Listen( Self.HOST, Self.PORT )If Not SERVER Print "Server: Failed to create server" ; Return ' On Failure...' On Sucess...' Initialize SERVER ConfigurationPrint "Server | "+SERVER.Address+" | Is listening..."SERVER.SetOption( "SO_REUSEADDR",1 )SERVER.SetOption( "TCP_NODELAY",1 )Repeat ' Connection Loop' Accept New Socket Connection RequestsLocal NEW_SOCKET := SERVER.Accept()If Not NEW_SOCKET Exit ' No Connection...' Connection Found...Print "Server Accepted Client | "+NEW_SOCKET.PeerAddressLocal STREAM := New SocketStream( NEW_SOCKET )' Read NEW_SOCKET Output STREAMNew Fiber( Lambda()Repeat' Extract Raw DATA Strings from STREAMLocal DATA := STREAM.ReadSizedString()If Not DATA Exit ' Exit if Failure...STREAM.WriteSizedString( DATA )Forever' Done Reading DATA from STREAMSTREAM.Close()End )Forever ' Connection LoopPrint "Server | Terminating..."SERVER.Close()EndEnd ClassClass NetClientField HOST:StringField PORT:IntMethod New(host:String, port:Int)Self.HOST = hostSelf.PORT = port'Process()End MethodMethod Process()'Fiber.Sleep( .5 )' Connect to Server on HOST and PORTLocal CLIENT := Socket.Connect( Self.HOST, Self.PORT )If Not CLIENT Print "Client | Couldn't Connect to Server!" ; Return' Configure Client...Print "Client: "+CLIENT.Address+" | Connected to Server: "+CLIENT.PeerAddressCLIENT.SetOption( "TCP_NODELAY",1 )' Send Data to Server...Local STREAM := New SocketStream( CLIENT )For Local i := 0 Until 100STREAM.WriteSizedString( "This Message Number: " + i )Print "Reply: " + STREAM.ReadSizedString()Next' Client is Closing...Print "Client | Is Closing..."STREAM.Close()EndEndserver.monkey2
Monkey12345678910111213141516171819#Import "<std>"#Import "network"Namespace APPLICATION_DEBUGUsing std..Using Network..Global HOST := "localhost"Global PORT := 40122Function Main:Void()Print "Loading Server..."Local N:NetServer = New NetServer(HOST, PORT)N.Process()End Functionclient.monkey2
Monkey1234567891011121314151617181920#Import "<std>"#Import "network"Namespace APPLICATION_DEBUGUsing std..Using Network..Global HOST := "localhost"Global PORT := 40122Function Main:Void()Print "Loading Client..."Local N:NetClient = New NetClient(HOST, PORT)N.Process()End FunctionAny ideas?
December 8, 2016 at 8:55 pm #5665Not 100% sure what’s up (it’s not crashing here, but not working either…), but for starters you can only currently use fibers with mojo based apps. I plan to change this eventually but it’s a reasonably complex-ish job…
December 9, 2016 at 12:40 am #5668Alrighty, not a problem. I’ll try and work around this for now.
January 3, 2017 at 7:42 pm #6184I want to add a question here. So Fibers are threads, right? When I want to do just a command line server without that mojo stuff I could just do a plain and dirty approach without fibers and just a blocking loop? I’ve read somewhere a single thread solution could be even better than multithreading for networking…
@scurty: Have you got this working btw?
January 6, 2017 at 12:48 pm #6247I think Fibers are an alternative way of looking at Threads in abstraction, but ultimately I think they have the same inner workings for asynchronous programming…
Sorry for the late reply @xaron.
As Mark said you can only use Client/Server functionality using Mojo/MojoX. However, he might try and implement it down the road. For now, I would just emulate a Terminal using Mojo or MojoX… There is a “Console” View for Mojo X that is used in Ted2/Go that does almost the same thing as an output terminal; though I’m sure with some tweaking you could get a nicely polished command line with Monkey2 features and support. - 
		AuthorPosts
 
You must be logged in to reply to this topic.