Page 1 of 1
TCP/IP Help needed
Posted: Mon Aug 06, 2012 9:02 pm
by cbrooks
Can anyone help me alter this code so that after a message is received, it sends back a message "Got Message"
thx
Code: Select all
If InitNetwork() = 0
MessageRequester("Error", "Can't initialize the network !", 0)
End
EndIf
OpenConsole()
Port = 6800
*Buffer = AllocateMemory(1000)
If CreateNetworkServer(0, Port)
PrintN("Server Created")
EndIf
sevent=0
While sevent=0
SEvent = NetworkServerEvent()
Wend
ClientID = EventClient()
While clientid<>0
Select NetworkServerEvent()
Case #PB_NetworkEvent_Connect
; MessageRequester("PureBasic - Server", "A new client has connected !", 0)
Print ("client connect")
PrintN (Str(clientID))
Case #PB_NetworkEvent_Data
;MessageRequester("PureBasic - Server", "Client "+Str(ClientID)+" has send a packet !", 0)
ReceiveNetworkData(ClientID, *Buffer, 1000)
;MessageRequester("Info", "String: "+PeekS(*Buffer), 0)
Print("client:")
PrintN (Str(clientid))
Print ("data:")
msg$= (PeekS(*buffer))
While Left(msg$,1)<>"Z"
msg$=Right(msg$,Len(msg$)-1)
Wend
msg$=Right(msg$,Len(msg$)-3)
PrintN (msg$)
; the following is to clear the buffer - hopefully wont lose data or run out of mem
*buffer=0
*Buffer = AllocateMemory(1000)
Case #PB_NetworkEvent_Disconnect
MessageRequester("PureBasic - Server", "Client "+Str(ClientID)+" has closed the connection...", 0)
Quit = 1
EndSelect
Wend
Re: TCP/IP Help needed
Posted: Mon Aug 06, 2012 9:38 pm
by infratec
Hi,
your code looks not very nice and I think you have not really understand what happens.
So please take a deeper look to the help of the used commands.
This could work (not tested):
Code: Select all
If InitNetwork() = 0
MessageRequester("Error", "Can't initialize the network !", 0)
End
EndIf
Port = 6800
*Buffer = AllocateMemory(1000)
If CreateNetworkServer(0, Port)
Debug "Server Created"
Else
End
EndIf
sevent=0
Quit =#False
Repeat
SEvent = NetworkServerEvent()
If SEvent
ClientID = EventClient()
Select SEvent
Case #PB_NetworkEvent_Connect
Debug "A new client has connected !"
Case #PB_NetworkEvent_Data
Debug "Client "+Str(ClientID)+" has send a packet !"
Rcv = ReceiveNetworkData(ClientID, *Buffer, 1000)
Debug "String: "+PeekS(*Buffer, Rcv)
msg$= PeekS(*buffer, Rcv)
While Left(msg$,1)<>"Z"
msg$=Right(msg$,Len(msg$)-1)
Wend
msg$=Right(msg$,Len(msg$)-3)
Debug msg$
SendNetworkString(ClientID, "Got Message")
Case #PB_NetworkEvent_Disconnect
Debug "Client "+Str(ClientID)+" has closed the connection..."
Quit = #True
EndSelect
EndIf
Delay(3)
Until Quit
CloseNetworkServer(0)
Bernd
Re: TCP/IP Help needed
Posted: Mon Aug 06, 2012 10:12 pm
by cbrooks
yeah I tried exactly that
SendNetworkString(ClientID, "Got Message")
but the msg never arrives.
any thoughts?
Re: TCP/IP Help needed
Posted: Mon Aug 06, 2012 10:41 pm
by cbrooks
I'm using QB64 as the client and PB as the server
The 2 connect np - the client sends msgs to the server np - but the return msg does not appear
When I read the details on SendNetworkString(), I'm thinking I need a null-character to get qb64 to accept it???
What is the null-character? Any other thoughts?
Remarks
There is no ReceiveNetworkString() command. SendNetworkString() only provides a solution to quickly send strings. The string will be sent without the terminating null-character, and can be received using ReceiveNetworkData(), after NetworkServerEvent() / NetworkClientEvent() returned #PB_NetworkEvent_Data). In unicode mode the string is sent as UTF-8, which is processor independant (unlike UTF-16 which is tied to processor endianness).
Re: TCP/IP Help needed
Posted: Mon Aug 06, 2012 11:10 pm
by cbrooks
if this helps, I tried this and it works 50% of the time - but of course I need 100%.
the msg was then received by the client. For some reason, it works half the time. the received character by the client is random.
Code: Select all
For t=0 To 255
SendNetworkString(ClientID, Chr(t))
Next t
Re: TCP/IP Help needed
Posted: Tue Aug 07, 2012 12:28 am
by cbrooks
I figured it out - at least a work around, in case anyone else is looking
by sending the original msg back, it contains the
ReceiveNetworkData(ClientID, *Buffer, 1000)
msg$= (PeekS(*buffer))
.
.
.
.
SendNetworkString(ClientID, msg$)
[/code]
Re: TCP/IP Help needed
Posted: Tue Aug 07, 2012 2:15 am
by RichAlgeni
Actually, you had a number of things wrong. First, I would strongly recommend that if you are new to PB or coding, make sure use 'EnableExplicit'.
Second, you were using multiple instances of the same variable for different purposes.
Third: You never opened a client connection in your code.
Fourth: The null character, or Chr(0), is used to terminate strings. It's how the system knows how long your string is. The string ends on the last character before the null. Note that you can use the null character in other ways, but don't worry about that now.
I rewrote your program so that it works correctly. Take a look at it, then ask any question you want. There are many ways to accomplish what you wanted, this is just one of them.
Rich
Code: Select all
EnableExplicit
Define Rcv.i
Define Port.i = 6800
Define Quit.i = #False
Define Recvd.i = 0
Define *Buffer
Define NSEvent.i
Define ClientID.i
Define amtRevcd.i = 0
Define connectNumber.i
; initialize the network
If InitNetwork() = 0
MessageRequester("Error", "Can't initialize the network!", 0)
End
EndIf
*Buffer = AllocateMemory(1000)
If CreateNetworkServer(0, Port)
Debug "Server Created on port " + Str(Port)
Else
End
EndIf
; open the client connection, send the data, then close the conection
connectNumber = OpenNetworkConnection("localhost", Port)
SendNetworkString(connectNumber, "Got Message")
CloseNetworkConnection(connectNumber)
; loop here to receive the data that was sent
Repeat
NSEvent = NetworkServerEvent()
Select NSEvent
Case #PB_NetworkEvent_Connect
Debug "A new client has connected !"
Case #PB_NetworkEvent_Data
ClientID = EventClient()
Debug "Client " + Str(ClientID) + " has send a packet!"
; loop here to make sure we get all of the data sent
Repeat
Rcv = ReceiveNetworkData(ClientID, *Buffer + amtRevcd, 1000)
amtRevcd = amtRevcd + Rcv
Debug "Rcv = " + Str(Rcv)
Until Rcv = 0
Debug "String: " + PeekS(*Buffer, amtRevcd)
Case #PB_NetworkEvent_Disconnect
Debug "Client "+ Str(ClientID) + " has closed the connection..."
Quit = #True
Default
Delay(100)
EndSelect
Until Quit
CloseNetworkServer(0)
Re: TCP/IP Help needed
Posted: Tue Aug 07, 2012 2:14 pm
by infratec
Hi,
with the following PB code you get the answer from my code above:
Code: Select all
If InitNetwork()
ConID = OpenNetworkConnection("127.0.0.1", 6800, #PB_Network_TCP)
If ConID
SendNetworkString(ConID, "Z0 Hello, a test")
Buffer$ = Space(100)
Timeout = 2000
Repeat
If NetworkClientEvent(ConID) = #PB_NetworkEvent_Data
Rcv = ReceiveNetworkData(ConID, @Buffer$, 100)
If Rcv > 0
Debug Buffer$
EndIf
EndIf
Delay(1)
Timeout - 1
Until Timeout = 0
CloseNetworkConnection(ConID)
EndIf
EndIf
And....
With your while loop you get an endless loop if you not transmit a string with 'Z' inside.
Because than you receive nothing more and you still wait for a 'Z'
@RichAlgeni:
You don't need OpenNetworkconnection()
The connection is established, so you can use it.
Re: TCP/IP Help needed
Posted: Tue Aug 07, 2012 4:33 pm
by infratec
Found an additional bug.
(Or better Marlin

)
NetworkServerEvent() was called twice.
Which results in lost of the disconect event.
I modified my Server code above.
Bernd