To begin, i tried with a minimalist struct wich have the connexion ID and a simple message.
The client send this struct to the server wich receive the id and the message but i always get a corrupted string ! (the id is good)
I tried differents solutions : allocate memory for the string (allocatememory, space ...), use of the copymemory(string, struct\message, len(string)) without success.
Here is the client part :
Code: Select all
Structure POUET
id.l
msg.s
EndStructure
If InitNetwork() = 0
MessageRequester("Error", "Can't initialize the network !", 0)
End
EndIf
Port = 6832
ConnectionID = OpenNetworkConnection("127.0.0.1", Port)
*p.POUET = AllocateMemory(SizeOf(POUET))
If *p = 0
MessageRequester("Error", "Memory allocation failed !")
End
EndIf
*p\id = ConnectionID
*p\msg = "pouet"+Chr(0)
If ConnectionID
MessageRequester("PureBasic - Client", "Client connected to server...", 0)
SendNetworkData(ConnectionID, *p, SizeOf(POUET) )
MessageRequester("PureBasic - Client", "Data has been sent to the server, please check it before quit...", 0)
CloseNetworkConnection(ConnectionID)
Else
MessageRequester("PureBasic - Client", "Can't find the server (Is it launched ?).", 0)
EndIf
FreeMemory(*p)
End
Code: Select all
Structure POUET
id.l
msg.s
EndStructure
If InitNetwork() = 0
MessageRequester("Error", "Can't initialize the network !", 0)
End
EndIf
Port = 6832
If CreateNetworkServer(Port)
MessageRequester("PureBasic - Server", "Server created (Port "+Str(Port)+").", 0)
Repeat
SEvent = NetworkServerEvent()
If SEvent
ClientID = NetworkClientID()
Select SEvent
Case 1
MessageRequester("PureBasic - Server", "A new client has connected !", 0)
Case 2
MessageRequester("PureBasic - Server", "Client "+Str(ClientID)+" has send a packet !", 0)
*p.POUET = AllocateMemory(SizeOf(POUET))
numbytes = ReceiveNetworkData(ClientID, *p, SizeOf(POUET) )
MessageRequester("Info", "numbytes (should be sizeof(POUET) ) : " + Str(numbytes) + " ID : "+ Str(*p\id) + " MSG : "+ *p\msg, 0)
Case 3
MessageRequester("PureBasic - Server", "Client "+Str(ClientID)+" has send a file via the network !", 0)
ReceiveNetworkFile(ClientID, "C:\TEST_Network.ftp3")
Case 4
MessageRequester("PureBasic - Server", "Client "+Str(ClientID)+" has closed the connexion...", 0)
Quit = 1
EndSelect
EndIf
Until Quit = 1
MessageRequester("PureBasic - Server", "Click to quit the server.", 0)
CloseNetworkServer()
Else
MessageRequester("Error", "Can't create the server (port in use ?).", 0)
EndIf
End
Thanx for reading

