You don't need two ports for two way communication from to client to server, if you particulary want 2 ports then don't mind this example:
Client:
Global client:TClient
Global sendbank:TBank
Global receivebank:TBank
Graphics 320,240
client=TClient.Create("localhost",112233)
If Not client RuntimeError "Could not connect to the server!"
sendbank=CreateBank(500) ; receivebank=CreateBank(500)
For i=0 To 500-1
PokeByte sendbank,i,0
Next
PokeByte sendbank,0,7
While Not KeyHit(KEY_ESCAPE)
If KeyHit(KEY_S)
WriteBank(sendbank,client,0,500)
EndIf
DrawText "updating",0,0 ; Flip ; Cls
If client.readavail() > 400
ReadBank(receivebank, Client, 0, 500)
Notify "received packet"
EndIf
Wend
Server:
Global server:TServer
Global client:TClient
Global clients:TList
Global receivebank:TBank
Graphics 320,240
server=TServer.Create(112233)
clients=CreateList()
receivebank=CreateBank(500)
While Not KeyHit(KEY_ESCAPE)
client=server.Accept()
If client
Print "New Client"
ListAddLast(clients,client)
EndIf
For client=EachIn clients
If client.ReadAvail() > 400 Then
as=ReadBank(receivebank,client,0,500)
Print PeekByte(receivebank,0)
EndIf
Next
If KeyHit(KEY_S)
For i=0 To 500-1
PokeByte(receivebank,i,0)
Next
WriteBank(receivebank,client,0,500)
EndIf
DrawText "Clients online: " + CountList(clients),0,0
Flip
Cls
Wend
In both examples you will have to add rich d's code. It uses databanks to send packets from client to server. If you press 'S' on the server the client will notify you it has received a packet, if you press 'S' on the client the server will print the first byte of the packet. Ofcourse you don't have to use databanks, it's just that I find it more comfortable like that.
|