@Preben :
create the server in purebasic is a great idea
.
I have tried and it works, but only for connexion and disconnexion, not for the send/receive message.
When I try to connect to the server (made in purebasic), it works, I receive the message on the server, when the client disconnect the server receive the message, but not the other message, and I don't know why.
Here is the purebasic server test code (based on the server code from purebasic) :
Structure sClient
Name$
id.i
EndStructure
Global NewList Client.sClient()
If InitNetwork() = 0
MessageRequester("Error", "Can't initialize the network !", 0)
End
EndIf
Port = 6832
*Buffer = AllocateMemory(1000)
clientID = -1
If CreateNetworkServer(0, Port, #PB_Network_TCP|#PB_Network_IPv4,"127.0.0.1")
OpenWindow(0,0,0,400,300,"server",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MaximizeGadget|#PB_Window_MinimizeGadget)
EditorGadget(0,0,20,WindowWidth(0),WindowHeight(0)-20)
ButtonGadget(1,10,0,50,20,"Send")
AddGadgetItem(0,-1,"Server ok ")
Repeat
SEvent = NetworkServerEvent()
event = WaitWindowEvent(1)
If event = #PB_Event_Gadget
If EventGadget() =1
i+1
If clientID > -1
SendNetworkString(ClientID,"Hello guy"+Str(i))
AddGadgetItem(0,-1,"Send message to client : "+Str(ClientID))
EndIf
EndIf
ElseIf event = #PB_Event_CloseWindow
quit = 1
EndIf
If SEvent
ClientID = EventClient()
Select SEvent
Case #PB_NetworkEvent_Connect
AddGadgetItem(0,-1,"Client connected "+Str(ClientID))
AddElement(client())
Client()\id = ClientID
Case #PB_NetworkEvent_Data
result = ReceiveNetworkData(ClientID, *Buffer, 1000)
;MessageRequester("Info", "String: "+PeekS(*Buffer, -1, #PB_UTF8), 0)
Txt$ = ""
Txt$ = PeekS(*Buffer, -1, #PB_UTF8)
; Txt$ = PeekS(*Buffer, result)
FreeMemory(*Buffer)
*Buffer = AllocateMemory(1000)
AddGadgetItem(0,-1,"New Message received (ClientID) : "+Str(ClientID)+" : "+Txt$+" | "+Str(result))
ForEach Client()
SendNetworkString(Client()\id, Txt$, #PB_UTF8)
Next
Case #PB_NetworkEvent_Disconnect
;MessageRequester("PureBasic - Server", "Client "+Str(ClientID)+" has closed the connection...", 0)
; Quit = 1
AddGadgetItem(0,-1,"Client deconnected "+Str(ClientID))
ok =0
ForEach client()
If client()\id = ClientID
ok =1
Break
EndIf
Next
If ok =1
DeleteElement(client())
EndIf
EndSelect
EndIf
Until Quit = 1
; MessageRequester("PureBasic - Server", "Server closed.", 0)
CloseNetworkServer(0)
Else
; MessageRequester("Error", "Can't create the server (port in use ?).", 0)
EndIf
End
And the client in agk (based on the code from the documentation) :
// Project: Server_client
// Created: 2017-05-02
SetWindowTitle( "client" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
//networkId = JoinNetwork("ExampleNetwork", "Client")
networkId = JoinNetwork("127.0.0.1", 6832, "MyClientName")
// save the time that the connection attempt was made so that we can set a time out for connection failure
connectTime# = Timer()
// current connection state
// 0 = connecting, 1 = connected, -1 = network closed, -2 = connection failed
state = 0
envoie =0
SetNetworkLatency( networkId, 10 )
do
// if a network is running, find the number of clients
clientNum = 2 // if I don't use that, the client isn't connected :(
if state >= 0
//clientNum = GetNetworkNumClients(networkId) <--------------- here I have always "0", I don't know why...
endif
if ok = 0
// create just a message to test, but it doesn't work, I don't receive that message on the server
ok = 1
msg = CreateNetworkMessage( )
// message(str(msg))
// AddNetworkMessageInteger( msg, 1 )
AddNetworkMessageString( msg, "hello" )
serverId = GetNetworkServerId(networkId)
SendNetworkMessage(networkId,serverId,msg)
DeleteNetworkMessage(msg)
endif
if GetRawKeyPressed(65)
// create a message to test, if press key A, but it doesn't work
msg = CreateNetworkMessage( )
// AddNetworkMessageInteger( msg, 1 )
AddNetworkMessageString( msg, "hello again" )
serverId = GetNetworkServerId(networkId)
SendNetworkMessage(networkId,serverId,msg)
DeleteNetworkMessage(msg)
inc envoie
//message("ok")
endif
print(str(envoie))
// doesn't work too :(
cmessage = GetNetworkMessage(NetworkID)
// cycle through all messages
While cmessage > 0
// idmsg = GetNetworkMessageInteger(cmessage)
//ClientID1 = GetNetworkMessageFromClient(msg)
//if ClientiD1 > 0
t$ = GetNetworkMessageString(cmessage)
//endif
//print("Client : "+str(clientId1))
// delete the current message
DeleteNetworkMessage ( cmessage )
// find the next message
cmessage = GetNetworkMessage ( NetworkID )
endWhile
print("text : "+t$)
// if we are currently attempting to connect
if state = 0
Print("Connection Pending...")
// if the number of clients is greater than 1, the connection has succeeded
// this is because once connected, there must be a minimum of 2 clients (this machine and the host)
if clientNum > 1
state = 1 // indicate that we are now connected
// if the connection has not yet succeeded, check for a time out or an error
// if it takes longer than 5 seconds to connect, it is safe to assume that it has failed
// any time isnetworkactive returns 0 also indicates a failure
// reasons for failure might include there being no such network as ExampleNetwork
elseif Timer() - connectTime# > 5.0 or IsNetworkActive(networkId) = 0
state = -2 // indicate that the connection failed
// close the network so that we are free to attempt another connection
CloseNetwork(networkId)
endif
// if we are currently connected
elseif state = 1
// check that no errors have occured on the connection
if IsNetworkActive(networkId)
// print the network details and the details of this client
Print("Network Active")
Print("Network Clients Detected: " + Str(clientNum))
clientId = GetNetworkMyClientId(networkId)
Print("Server Id: " + Str(GetNetworkServerId(networkId)))
Print("Client Id: " + Str(clientId))
//Print("Client Name: " + GetNetworkClientName(networkId, clientId))
else
Print("Network Inactive")
endif
// if the network has been closed deliberately, display a success message
elseif state = -1
Print("Network Closed")
// if the network connection has failed, display an error message
elseif state = -2
Print("Network Connection Failed")
endif
// if we are currently connected, give the user the opportunity to leave the network
if state = 1
//Print("")
//Print("Press Any Key To Close Network")
// if we are not currently connected, or trying to connect, give the user the opportunity to join the network
elseif state < 0
Print("")
Print("Press Any Key To Connect")
endif
// if any key has been released
if GetRawKeyReleased(GetRawLastKey())
// if we are currently connected
if state = 1
// disconnect from the network
//CloseNetwork(networkId)
//state = -1
// if we are not currently connected
elseif state < 0
// join the network
networkId = JoinNetwork("127.0.0.1", 6832, "MyClientName")
connectTime# = Timer()
state = 0
endif
endif
Sync()
loop
If you have an idea how to fixe the problems, it could be really great
.
KISTech :
Thank you for your proposal, I'll think about it
.
For the moment. I make just some server test, I don't have a server which is ready to test (in AppGameKit, I can test on my machine, but I'm not sur it work on windows server 2012, and I'm not sur the performance will be enough good.
So i'm trying to create the server in purebasic to get better performances, and perhaps use sqllite database, and other great options.
AGK2 tier1 - http://www.dracaena-studio.com