Try my new website: www.urcho.com - the new SIMPLE social network
blitz code snippets Sign up | 284 members | 114 snippets
Search for:

Username:
Password:


TCP Socket Server by CoderLaureate

[back]
Author: Archive | Viewed: 760 times | Language: BlitzMax | Category: Multiplayer Code
Author Comments:
TCPSocketServer I thought Id throw my hat into the "TCPSocket Ring". This is an OOP approach to creating a TCPSocket server. Callbacks are used for extensibility. You can create functions to handle events and add whatever functionality you like. You can create many different servers listening to different ports, and assign different functionality to each server. The benefit of doing this is that each server is running withing the same process, and therefor causes less overhead drain on your system resources. The Event Callback handles are: NewConnectionCallback Triggered whenever a new client connects. The callback function takes one parameter of type TCPSocketConnection. Your callback function can access the various fields and methods of the connection. A link back to the connections server is also provided to give you access to the various fields and methods of the server. LostConnectionCallback Triggered whenever a client disconnects from the server. This callback also takes a TCPSocketConnection. After the server calls the callback function it removes the connection from its collection. MsgRcvdCallback Triggered whenever a client sends a message to the server. The callback function takes one parameter of type TCPSocketConnection. Your callback function can access the message in the connection objects "Buffer" field. The field is a string, but can easily be converted to an array of bytes for whatever processing you need to do. This function is where you would add any "Intelligence" to your server, ie, processing commands, etc. Methods Broadcast(Message:String, [conn:TCPSocketConnection = Null]) If a TCPSocketConnection is provided the server only broadcasts the message to that specific connection. Otherwise the server will broadcast the message to all of its connections. Listen() Updates the server connections and triggers events as they occure. TCPSocketConnections A TCPSocketConnection is the "pipeline" through which the server communicates with the clients that are connected. Each server object has its own collection of TCPSocketConnection objects. Methods Recieves messages from your program and tells the server to trigger the MsgRcvdCallback event. This works in a "Non-Blocking" fashion, so as not to hold up your application while waiting for input. Youre program causes the server to trigger this event by sending a "Stop Sequence". The server defaults this stop sequence to a Carriange Return/Line Feed {~r~n}. But you can change it to whatever you want by setting the servers "StopSeq" field.
Login or create an account to comment on this snippet