I can't see anything wrong with the code you posted, although I could have missed something.
I wrote some test code that tests your scenario and had no problems. It would be good if you could replicate your problem using the below test code or an adjusted version because then we are better able to determine the cause in this simple version. If you are unable to replicate the problem then it is likely to be a problem with your code rather than MikeNet.
The client connects to the game server which is connected to the data server. The client can send requests to open or close a contract which it sends to the game server. The game server forwards these requests to the data server. The data server takes action and sends a confirmation back to the game server which is then forwarded to the client. This is used to determine whether the requested action was successful. Let me know if this is majorly different to the way in which your system operates.
I hope you manage to fix the problem, good luck
Data Server
sync rate 0
sync on
sync
print "DATA SERVER"
sync
` Variables
#constant NoContracts = 50
#constant MaxClients = 50
dim Contract(NoContracts) as boolean
for n = 0 to NoContracts-1
Contract(n) = 0
next n
#constant OP_GETCONTRACT = 0
#constant OP_ENDCONTRACT = 1
` Setup server
Key = mn create key 256(1000,2000,3000,4000)
RecvPacket = mn create packet()
SendPacket = mn create packet()
mn set memory size SendPacket,1024
mn start 1,0
mn disable udp 0
mn set local 0, "127.0.0.1",2525,"",0
mn start server 0,MaxClients,0,0
do
result = mn client joined(0)
if result > 0
print "Client "+str$(result)+" has joined"
sync
endif
result = mn client left(0)
if result > 0
print "Client "+str$(result)+" has left"
sync
endif
for n = 1 to MaxClients
result = mn recv tcp(0,RecvPacket,n)
if result > 0
mn decrypt RecvPacket, Key
operation = mn get byte(RecvPacket)
if operation = OP_GETCONTRACT
cont = FindContract()
client = mn get int(RecvPacket)
mn add byte SendPacket,OP_GETCONTRACT
mn add int SendPacket,client
mn add dword SendPacket,cont
mn encrypt SendPacket,Key
mn send tcp 0,SendPacket,n,0,1
if cont = -1
print "Could not setup contract on request from client " + str$(n) + ", no contracts available"
else
print "Setup contract "+str$(cont)+" on request from client "+str$(n)
endif
sync
endif
if operation = OP_ENDCONTRACT
cont = mn get dword(RecvPacket)
Contract(cont) = 0
print "Ended contract "+str$(cont)+" on request from client "+str$(n)
sync
endif
endif
next n
loop
` Finds a contract and sets it as 'in use'
function FindContract()
ReturnMe as dword
ReturnMe = -1 `Returned if no unused contract is found
for n = 0 to NoContracts-1
if Contract(n) = 0
ReturnMe = n
Contract(n) = 1
exit
endif
next n
endfunction ReturnMe
Game Server
sync rate 0
sync on
sync
print "GAME SERVER"
sync
` Variables
#constant MaxClients = 50
#constant INST_SERVER = 0 `host server for clients
#constant INST_DATA = 1 `connect to data server
#constant OP_GETCONTRACT = 0
#constant OP_ENDCONTRACT = 1
` Setup MikeNet
Key = mn create key 256(1000,2000,3000,4000)
RecvPacket = mn create packet()
SendPacket = mn create packet()
mn set memory size SendPacket,1024
mn start 2,0
` Setup server
mn disable udp INST_SERVER
mn set local INST_SERVER, "127.0.0.1",2526,"",0
mn start server INST_SERVER,MaxClients,0,0
` Setup connection to data server
mn disable udp INST_DATA
result = mn connect(INST_DATA,"127.0.0.1",2525,"",0,5,1)
if result <> 1
print "Error connecting to data server"
sync
wait key
end
endif
while mn client connected(INST_DATA,0) = 1
`INST_SERVER related
result = mn client joined(INST_SERVER)
if result > 0
print "Client "+str$(result)+" has joined"
sync
endif
result = mn client left(INST_SERVER)
if result > 0
print "Client "+str$(result)+" has left"
sync
endif
for n = 1 to MaxClients
result = mn recv tcp(INST_SERVER,RecvPacket,n)
if result > 0
mn decrypt RecvPacket,Key
operation = mn get byte(RecvPacket)
if operation = OP_GETCONTRACT
mn add byte SendPacket, operation
mn add int SendPacket, n
mn encrypt SendPacket,Key
mn send tcp INST_DATA,SendPacket,0,0,1
print "Requested contract setup from data server on request from client "+str$(n)
sync
endif
if operation = OP_ENDCONTRACT
cont = mn get dword(RecvPacket)
mn encrypt RecvPacket,Key
mn send tcp INST_DATA,RecvPacket,0,0,1
print "Requested that contract "+str$(cont)+" be ended on request from client "+str$(n)
sync
endif
endif
next n
`INST_DATA related
result = mn recv tcp(INST_DATA,RecvPacket,0)
if result > 0
mn decrypt RecvPacket,Key
operation = mn get byte(RecvPacket)
if operation = OP_GETCONTRACT
client = mn get dword(RecvPacket)
cont = mn get int(RecvPacket)
if cont = -1
print "Contract request by client " + str$(client) + " rejected by data server"
sync
mn add byte SendPacket,OP_GETCONTRACT
mn add byte SendPacket,0 ` indicates that contract was rejected
mn encrypt SendPacket,Key
mn send tcp INST_SERVER,SendPacket,client,0,1
else
print "Contract request by client " + str$(client) + " accepted, contract number is " + str$(cont)
sync
mn add byte SendPacket,OP_GETCONTRACT
mn add byte SendPacket,1 ` indicates that contract was accepted
mn encrypt SendPacket,Key
mn send tcp INST_SERVER,SendPacket,client,0,1
endif
endif
endif
endwhile
Client
sync rate 0
sync on
sync
print "CLIENT"
sync
` Variables
#constant OP_GETCONTRACT = 0
#constant OP_ENDCONTRACT = 1
` Setup connection to game server
Key = mn create key 256(1000,2000,3000,4000)
RecvPacket = mn create packet()
SendPacket = mn create packet()
mn set memory size SendPacket,1024
mn start 2,0
mn disable udp 0
result = mn connect(0,"127.0.0.1",2526,"",0,5,1)
if result <> 1
print "Error connecting to game server"
sync
wait key
end
endif
while mn client connected(0,0) = 1
` Request contract
if upkey() = 1
print "Requested contract"
sync
mn add byte SendPacket, OP_GETCONTRACT
mn encrypt SendPacket,Key
mn send tcp 0,SendPacket,0,0,1
while upkey() = 1
endwhile
endif
` Request contract end
if downkey() = 1
input "Enter the number of the contract you wish to terminate: ",endstr$
endval = val(endstr$)
print "Requested contract end"
sync
mn add byte SendPacket, OP_ENDCONTRACT
mn add dword SendPacket, endval
mn encrypt SendPacket,Key
mn send tcp 0,SendPacket,0,0,1
while downkey() = 1
endwhile
endif
result = mn recv tcp(0,RecvPacket,0)
if result > 0
mn decrypt RecvPacket, Key
operation = mn get byte(RecvPacket)
if operation = OP_GETCONTRACT
success = mn get byte(RecvPacket)
if success = 0
print "Contract request rejected"
sync
else
print "Contract request accepted"
sync
endif
endif
endif
endwhile