Hi, I wanted to make a chat program using DBP and DarkNet, so I started looking at the documentation and writing one.
Here is my code:
Server:
`Setup Variables
Instances = 1
Threads = 0
MaxClients = 10
MaxOperations = 5
UDPMode = 2
LocalIp$ = ""
LocalPort = 31337
Black = rgb(0,0,0)
White = rgb(255,255,255)
Red = rgb(255,0,0)
Green = rgb(0,255,0)
`Show GUI
Print "Starting Server..."
Print "Please allow Port:";
Ink Green,Black;
Print Str$(LocalPort);
Ink White,Black
`Start DarkNet
Mn Start Instances,Threads
`Set Local Settings
Mn Set Local 0,LocalIp$,LocalPort,LocalIp$,LocalPort
`Start The Server
Server = Mn Start Server(0,MaxClients,MaxOperations,UDPMode)
If Server = 0
`Server Started Successfully
Cls
Print "Server Started Successfully!"
`External IP
Print "----------------------------"
Print "Server External IP: ";
Ink Red,Black;
Print getIP$()
Ink White,Black
`Local TCP
Print "Server Local TCP IP: ";
Ink Red,Black;
Print Mn Get Local IP TCP(0);
Ink White,Black;
Print ":";
Ink Green,Black;
Print Str$(Mn Get Local Port TCP(0))
Ink White,Black
`Local UDP
Print "Server Local UDP IP: ";
Ink Red,Black;
Print Mn Get Local IP UDP(0);
Ink White,Black;
Print ":";
Ink Green,Black;
Print Str$(Mn Get Local Port UDP(0))
Ink White,Black
Print "----------------------------"
Else
`Server Failed To Start
Print "Server failed to start. Press a key to exit..."
Wait Key
End
Endif
Do
`Log Client Joined
Joined = Mn Client Joined(0)
If Joined > 0
Print "A new Client has Joined the server. IP: " + Mn Get Client Ip Tcp(0,Joined) + Str$(Mn Get Client Port Tcp(0,Joined))
Endif
`Log Client Left
Left = Mn Client Left(0)
If Left > 0
Print "A Client has Left the server. IP: " + Mn Get Client Ip Tcp(0,Left) + Str$(Mn Get Client Port Tcp(0,Left))
Endif
Loop
`Get IP Address (By IanM)
Function getIP$()
Local IP as String
Local ReadLine as String
If File Exist("temp.dat") Then Delete File "temp.dat"
Load Dll "urlmon.dll", 1
Status = Call Dll(1, "URLDownloadToFileA", 0, "http://www.whatismyip.org", "temp.dat", 0, 0)
Delete Dll 1
If Status = 0
Open to Read 1, "temp.dat"
Read String 1, IP
Close File 1
Endif
If IP = "" Then IP = "unknown"
If File Exist("temp.dat") Then Delete File "temp.dat"
Endfunction IP
Client:
`Variables
ConnectPort = 31337
TimeOut = 4
Instances = 1
Threads = 0
Black = rgb(0,0,0)
White = rgb(255,255,255)
Red = rgb(255,0,0)
Green = rgb(0,255,0)
MaximumClients as Integer
MaximumOperations as Integer
ConnectIp$ as String
MyName$ as String
`Setup Packets
SendPacket = Mn Create Packet()
RecvPacket = Mn Create Packet()
Mn Set Memory Size SendPacket,1024
`Start DarkNet
Mn Start Instances,Threads
`Get IP To Connect To
Input "Enter the IP that you would like to connect to: ", ConnectIP$
Input "Enter a name to use: ", MyName$
`Connect To Server
Connect = Mn Connect(0,ConnectIP$,ConnectPort,ConnectIP$,ConnectPort,Timeout,1)
Cls
If Connect = 1
`Connected Successfully
Print "You have connected to: ";
Ink Red,Black;
Print ConnectIP$;
Ink White,Black;
Print ":";
Ink Green,Black;
Print ConnectPort;
Ink White,Black;
Print " successfully!"
Print "Client ID: " + Str$(Mn Get Client Id(0))
Print "----------------------------"
MaximumClients = mn Get Max Clients(0)
MaximumOperations = mn Get Max Operations(0)
Endif
`If The Connection Timed Out
If Connect = 0
Cls
Print "Connection timed out. The server may not be available. Press any key to exit..."
Wait Key
End
Endif
`If An Error Occurred During Connection
If Connect = -1
Cls
Print "An error occurred while trying to connect. Press any key to exit..."
Wait Key
End
Endif
`While Connected
While Mn Client Connected(0,0) = 1
Sync
`Check For New Messages
TcpPackets = Mn Recv Tcp(0,RecvPacket,NULL)
`If There Is A New Message
If TcpPackets > 0
Operation = mn Get Int(RecvPacket)
Person = mn Get Int(RecvPacket)
`If A Player Has Joined
If Operation = 0
Print "A new person has joined!"
Endif
`If A Player Has Left
If Operation = 1
Print "A person has left!"
Endif
PersonA as Integer
For PersonA = 1 to MaximumClients
OperationA as Integer
For OperationA = 0 to MaximumOperations-1
UDPPackets = Mn Recv UDP(0,RecvPacket,PersonA,OperationA)
If UDPPackets = 1
Name$ = Mn Get String(RecvPacket,0,1)
Chat$ = Mn Get String(RecvPacket,0,1)
Print Name$ + ": " + Chat$
Endif
Next OperationA
Next PersonA
Endif
`Send Messages
If KeyState(28) = 1
Input "",Message$
Mn Add String SendPacket,MyName$,1024,1
Mn Add String SendPacket,Message$,1024,1
Endif
`Send Message
Mn Send Udp 0,SendPacket,NULL,0,1
Endwhile
`If We Have Become Disconnected From The Server
While ScanCode() <> 0
Wait 1
Endwhile
While ScanCode() = 0
Print "Lost connection to server. Press any key to exit..."
Sync
Endwhile
When I try running the Server, then connecting with the client, everything seems to work, but when I input a name to use, I get the following error:
Error whilst error reporting, could not allocate memory to store error message!
Am I doing something wrong with packet size? or is there something else I am missing?
Any help would be appreciated, not just on this problem, but on any others that you see!
Thanks.
Edit::
Well apparently I cannot code when I have a headache and it is Midnight, because it looks as though I have forgotten to send packets that the server receives to the clients. I do not believe this would fix the error I am getting, but if it does please share!