TCP Sockets

BlitzMax Forums/BlitzMax Programming/TCP Sockets

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

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