TCP Sockets
BlitzMax Forums/BlitzMax Programming/TCP Sockets
| ||
Is there a good tutorial on how to use them or a good module to make my life easyer? I need to learn how to make a Server to wich more clients can connect. I tryed to learn it myself looking on some examples but I can't manage to get them connected. |
| ||
Sorry for the old lyre, but: http://vertex.dreamfall.at/bnet/bnetex166.zip On the server side: - Server = New TTCPStream - Server.Init() - Server.SetLocalPort(Port) <- the Port that have the clients to connect - Server.SetTimeouts(Receive, Send, Accept) <- timeouts for receiving, sending and accepting(new clients) - Server.Listen() <- now, the server is listen for new clients - NewClient = Server.Accept() <- if there is a new client connected, NewClient is a new TTCPStream - The IP and Port you can get via NewClient.GetLocalIP() and NewClinet.GetLocalPort() - Save the NewClient in a list or array - In a For Each Loop(in the list or array) you check the connection with Client.GetState() <> 1 - If the state <> 1, then the client has disconnected. So the client must remove from the list/array - If the client have data sended, you can check it with Client.RecvAvail - When receiving available, so you receive all messages from it with While Client.RecvMsg() ; Wend. All packages stored in an internal buffer of the stream - Now you can readout the packages with While Not Client.EoF() and Client.ReadLine(), Client.ReadInt() or so on. - If you want to send the client any data, you have to Client.WriteLine("Hello, world"), Client.WriteInt(123) or so on and than finaly While Client.SendMsg() ; Wend On the client side: - Client = New TTCPStream - Client.Init() - Client.SetLocalPort() <- you can set here a port, or let the OS search for a free port - Client.SetRemotePort(1234) <- the remote port you have set on the server side(80 = HTTP for example) - Client.SetRemoteIP(TNetwork.IntIP("127.0.0.1")) <- set the remote ip e.g. the server ip - Client.SetTimeouts(Receive, Send) <- set the timeouts for receiving and sending(accept is ignored for clients) - Client.Connect() <- connect to the server - You can check the connection to the server with Client.GetState() <> 1 too - Check for new packages from the server with Client.RecvAvail() - When packages available then you receive it with While Client.RecvMsg() ; Wend - And read the content with While Not Client.EoF() and Client.ReadLine() or so on - If you want to send data to the server, you have to do it with Client.WriteLine("Hello, world") or so and send it with While Client.SendMsg() I hope, this will help you. cu olli |
| ||
Putting send/receive stuff in while loops, won't that make the game hang until it's completed? |
| ||
I allready found out about Bnetex and I allready made a working server and client but thx anyway. The documentation you posted shure makes it easyer :) Thanx! :) |
| ||
kronholm: Becouse this, you have to set the timeouts. But with Stream.RecvAvail() you can check faster for new packages. Normaly, the networkstuff have to put in a parallel thread to the game/server, but this is actually not possible with BMax I've heard. Synctus: hehe, ok, have fun :) cu olli |