Well, I've always wanted to learn how to work network, like IP to IP through the internet. But we all know my skills aren't that good yet, so bare with me.
I'm just having fun with this and if you could lend me a hand that would be just awesome. Ok so I found this sample of code to work sockets easily(so I though).
I got 3 BMX file, net.bmx, server.bmx, client.bmx
I first edited out some parts about modules and import and superstrict because I was getting errors.
NET.BMX
SERVER.BMX
CLIENT.BMX
Obviously I have no clue what I'm doing. Basicly I would like to establish a connection between the server and the client and then be able to send data like text from one to the other.
My problem with most examples out there would be problems with having more then one client.
I'm trying to make the most simple chat using sockets or maybe gnet(however I heared gnet is slow, is that true?)
Thanks guys for the help in advance!!! :)
EDITED 1:22PM EASTERN
client:TClient.Write()
I would say I would have to use this however, how do I use it? ? ?
I write then Read on Server? and if i want multiple clients I guess I store clients in an array cuz that NET.BMX doesn't seem to do that :o (i think its meant for 1 client)
I'm just having fun with this and if you could lend me a hand that would be just awesome. Ok so I found this sample of code to work sockets easily(so I though).
I got 3 BMX file, net.bmx, server.bmx, client.bmx
I first edited out some parts about modules and import and superstrict because I was getting errors.
NET.BMX
' 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
SERVER.BMX
' This is the server.bmx Include "net.bmx" ' This creates a new server using port 1024 CreateServer:TServer(1024) Repeat AcceptClient:TClient(server:TServer) Flip; Cls Until KeyHit(KEY_ESCAPE) End
CLIENT.BMX
' This is the client.bmx Include "net.bmx" ' This creates a new server using port 1024 CreateClient:TClient("127.0.0.1",1024) Repeat Flip;Cls Until KeyHit(KEY_ESCAPE) End
Obviously I have no clue what I'm doing. Basicly I would like to establish a connection between the server and the client and then be able to send data like text from one to the other.
My problem with most examples out there would be problems with having more then one client.
I'm trying to make the most simple chat using sockets or maybe gnet(however I heared gnet is slow, is that true?)
Thanks guys for the help in advance!!! :)
EDITED 1:22PM EASTERN
client:TClient.Write()
I would say I would have to use this however, how do I use it? ? ?
I write then Read on Server? and if i want multiple clients I guess I store clients in an array cuz that NET.BMX doesn't seem to do that :o (i think its meant for 1 client)