Basic chat server client example
Server
Code: Select all
;basic chat server
;
;1) include ooNetWork
XIncludeFile "PureNetWork.pbi"
;2) Define and create a net work object
; New_NetWork(*obj,MaxClients,MaxConnections,AmServer)
; Where Max Clients is the maximum number of clients accepted on the server
; Max Connections is the additional number of connections each client may have
; #True for I am the server
; note to enable multiple clients from a single IP (same machine)
; set MaxConnections To 0 otherwise only one client from a single IP may connect
Global net.NetWork_obj = New_NetWork(@net,10,1,#True) ;
#nick = 10
#Messege = 20
#data = 30
;3) Create a callback procedure with these parameters
Procedure Listen(client.i,reason.i,*mdata,len.i)
Protected *tdata,ts.s,nlen.i,text.s,a,aret.i,tlen,name.s
Static ct
;6) process a requests from an authenitcated client
Select Reason
Case #nick
net\Set_UserID(client,PeekS(*mdata,len))
Case #data
text = Str(ct) +" Recived " + Str(reason) + " " + Str(len)
AddGadgetItem(0,0,Text)
aret = Net\BroadCast(client,*mdata,Len)
ct+1
Case #Messege,#NetWork_Authorized
text = Str(ct) +" Recived " + Str(reason) + " " + Str(len)
AddGadgetItem(0,0,Text)
*tdata = AllocateMemory(len+64)
;who said what
name = net\Get_UserId(client)
PokeS(*tdata,name,64)
;copy the message
CopyMemory(*mdata,*tdata+64,len)
;7) send message to all clients
aret = Net\BroadCast(client,*tdata,len+64)
FreeMemory(*tdata)
ct+1
EndSelect
If ct > 2000
ClearGadgetItems(0)
ct=0
EndIf
EndProcedure
Procedure Status(*Status)
Static lstatus.s
Protected status.s
status = PeekS(*status)
If Status <> lStatus
AddGadgetItem(0,0,status)
StatusBarText(1,0,Status)
lStatus = Status
EndIf
EndProcedure
;4) Set the Server CallBack Address
net\Set_ServerCallBack(@listen())
net\Set_StatusCallBack(@Status())
;5) Start the server with the port and password
Define port.s,pass.s
port = InputRequester("server","Set Port","7500")
pass = InputRequester("server","Set PassWord","qwerty")
net\InitNetServer(Val(port),pass)
Define EV
OpenWindow(0,0,0,250,250,"Server test",#PB_Window_SystemMenu | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget | #PB_Window_TitleBar)
ListViewGadget(0,3,3,246,226)
CreateStatusBar(1,WindowID(0))
AddStatusBarField(200)
Repeat
EV = WaitWindowEvent()
Until EV = #WM_CLOSE
Debug "ended"
;8) Close and destroy the server object
If net
net\Destroy(@net)
EndIf
client
Code: Select all
;basic chat client example
;1) include ooNetWork
XIncludeFile "PureNetwork.pbi"
;2) Define and create a net work object
; New_NetWork(*obj,MaxClients,MaxConnections,AmServer)
; Where Max Clients is the maximum number of clients accepted on the server
; Max Connections is the additional number of connections each client may have
; #True for I am the server
; note to enable multiple clients from a single IP (same machine)
; set MaxConnections To 0 other wise only one client from a single IP may connect
;Global net.NetWork_obj = rNew_NetWork(@net,0,5,#False) 6 tcp connections
Global net.NetWork_obj; = New_NetWork(@net,0,1,#False) ;single tcp connection
;need to keep track of the connection number lcon
Global lcon,ct,nickname.s,gsent.i
#nick = 10
#Messege = 20
#data = 30
;3) Create a callback procedure with these parameters
Procedure Listen(client.i,reason.i,*mdata,len.i)
Protected *tdata,nlen.i,text.s,a.i,aret.i,tlen,name.s
;6) process a messages from the server
lcon=client
Select Reason
Case #Data
Text = PeekS(*mdata,len) ;"Data Recieved"
AddGadgetItem(0,ct,Text)
ct+1
Case #NetWork_Authorized
;If you get here your authenticated
;Enable the send button and return the nickname to the server
DisableGadget(2,0)
DisableGadget(3,0)
net\send(lcon,#Nick,@nickname,Len(nickname))
Case #NetWork_BroadCast
;ok we got a message display it
name = PeekS(*mdata,64)
If len < 256
text = name + " said " + PeekS(*mdata+64,len-64)
Else
text = "got data"
EndIf
AddGadgetItem(0,ct,Text)
ct+1
EndSelect
If ct > 2000
ClearGadgetItems(0)
ct=0
EndIf
;Delay(80)
EndProcedure
Procedure send(msg.s)
;7) send a message to the server
If msg <> ""
AddGadgetItem(0,ct,msg)
ct+1
net\send(lcon,#Messege,@msg,Len(msg))
SetGadgetText(1,"")
EndIf
EndProcedure
Procedure senddata()
Protected *tdata,size,a
DisableGadget(3,1)
size = (1024*1024)
*tdata = AllocateMemory(size)
For a = 0 To size-1
PokeB(*tdata+a,a%255)
Next
net\send(lcon,#data,*tdata,size)
FreeMemory(*tdata)
DisableGadget(3,0)
EndProcedure
Procedure Status(*Status)
Static lstatus.s
Protected status.s
Status = PeekS(*status)
If IsStatusBar(0)
If Status <> lStatus
StatusBarText(0,0,Status)
lStatus = Status
EndIf
EndIf
EndProcedure
Define ConnectToIP.s,port.s,pass.s
ConnectToIP = InputRequester("Client","Enter IP","127.0.0.1")
port = InputRequester("Client","Enter port","7500")
pass = InputRequester("Client","Enter password","qwerty")
nickname = InputRequester("NickName","Enter a nick name","")
;
Define EV,evt,txt.s
OpenWindow(0,0,0,250,293,"Client",#PB_Window_SystemMenu)
ListViewGadget(0,3,3,246,220)
StringGadget(1,3,225,195,20,"")
ButtonGadget(2,200,225,50,20,"send")
ButtonGadget(3,200,247,50,20,"data")
CreateStatusBar(0,WindowID(0))
AddStatusBarField(200)
DisableGadget(2,1)
net.NetWork_obj = New_NetWork(@net,0,1,#False)
;4) Set the Clinet CallBack Address
net\Set_ClientCallBack(@listen())
net\Set_StatusCallBack(@Status())
;5) When Ready to Connect Init Client with IP, port, Password
net\Init_NetClient(ConnectToIP,Val(port),pass)
Repeat
EV = WaitWindowEvent()
If ev = #PB_Event_Gadget
evt = EventGadget()
If evt = 2
txt = GetGadgetText(1)
send(txt)
ElseIf evt = 3
Senddata()
EndIf
EndIf
Until EV = #WM_CLOSE
;8) Destroy the object
If net
net\Destroy(@net)
EndIf