Code archives/Networking/Simple Network Module
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
This network module creates a layer of abstraction over the normal BlitzMax socket system to form an interface that is very similar to the network system of BlitzPlus and Blitz3D. | |||||
SuperStrict Rem bbdoc: Net EndRem Module rds.net Import brl.stream Import brl.socket Import brl.socketstream Rem bbdoc: Server type EndRem Type TServer Field socket:TSocket Function Create:TServer(port:Int) Local server:TServer = New TServer server.socket = CreateTCPSocket() If Not BindSocket(server.socket, port) Or Not SocketListen(server.socket) CloseSocket(server.socket) Return Null EndIf Return server EndFunction Method Close() CloseSocket(socket) EndMethod Method Accept:TClient() Local accepted_socket:TSocket = SocketAccept(socket) If Not accepted_socket Return Null Local client:TClient = New TClient client.socket = accepted_socket Return client EndMethod EndType Rem bbdoc: Client type EndRem Type TClient Extends TStream Field socket:TSocket Function Create:TClient(address:String, port:Int) Local client:TClient = New TClient client.socket = CreateTCPSocket() If Not ConnectSocket(client.socket, HostIp(address), port) CloseSocket(client.socket) Return Null EndIf Return client EndFunction Method Close() CloseSocket(socket) EndMethod Method Read:Int(buffer:Byte Ptr, count:Int) Return socket.Recv(buffer, count) EndMethod Method Write:Int(buffer:Byte Ptr, count:Int) Return socket.Send(buffer, count) EndMethod Method ReadAvail:Int() Return SocketReadAvail(socket) EndMethod EndType Rem bbdoc: Create a new server EndRem Function CreateServer:TServer(port:Int) Return TServer.Create(port) EndFunction Rem bbdoc: Close a server EndRem Function CloseServer(server:TServer) server.Close() EndFunction Rem bbdoc: Accept a new client if available EndRem Function AcceptClient:TClient(server:TServer) Return server.Accept() EndFunction Rem bbdoc: Create a new client EndRem Function CreateClient:TClient(address:String, port:Int) Return TClient.Create(address, port) EndFunction Rem bbdoc: Close a client EndRem Function CloseClient(client:TClient) client.Close() EndFunction Rem bbdoc: Check the number of bytes available to be read EndRem Function ReadAvail:Int(client:TClient) Return client.ReadAvail() EndFunction |
Comments
| ||
This is great, it really helped me out alot, I wish I had found this sooner :) I just have one problem that keeps bugging me, I can't seem to check if a client is still connected with "If Not Eof(Client) Then...", even when a client disconnected, the server doesn't notice. Would there be a way to fix this? |
| ||
I had implemented an Eof method that checked SocketConnected() but that function is faulty as it will return true that the socket is connected if it still has data in the buffer, even if it really has been closed. The problem is that the only safe way to check if a socket has been closed remotely is to read or write it and have that fail, i.e. throw an exception.Local client:TClient = CreateClient("www.google.com", 80) Try Repeat Local line:String = Input() If line = "exit" Then Exit WriteLine(client, line) While ReadAvail(client) Local s:String = ReadLine(client) Print(s) Wend Forever Catch o:Object Print("Connection closed by foreign host") EndTry CloseClient(client) I'm glad that this has helped, because I think it helps make networking much easier and simpler. |
| ||
do you have an example program? |
Code Archives Forum