-
Notifications
You must be signed in to change notification settings - Fork 0
Request Response
Jiowcl edited this page Dec 14, 2024
·
1 revision
EnableExplicit
IncludeFile "../Core/Nanomsg.pbi"
Global lpszCurrentDir.s = GetCurrentDirectory()
; Nanomsg version (x64)
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
Global lpszLibNnDir.s = "Library/x64"
Global lpszLibNnDll.s = lpszCurrentDir + lpszLibNnDir + "/nanomsg.dll"
SetCurrentDirectory(lpszCurrentDir + lpszLibNnDir)
CompilerEndIf
Global lpszServerAddr.s = "tcp://*:1700"
Global hLibrary.i = NnDllOpen(lpszLibNnDll)
If hLibrary
OpenConsole()
Define Socket.i = NnSocket(hLibrary, #AF_SP, #NN_REP)
Define Rc.i = NnBind(hLibrary, Socket, lpszServerAddr)
PrintN("Bind an IP address: " + lpszServerAddr)
Define lTotal.l = 0
While 1
lTotal = lTotal + 1
Define *lpszBuffer = AllocateMemory(32)
Define lpszMessage.s = "Hi " + lTotal
NnRecv(hLibrary, Socket, *lpszBuffer, MemorySize(*lpszBuffer), 0)
Delay(10)
PrintN("Received: ")
PrintN( PeekS(*lpszBuffer, -1, #PB_UTF8) )
NnSend(hLibrary, Socket, lpszMessage, Len(lpszMessage), 0)
FreeMemory(*lpszBuffer)
Wend
NnClose(hLibrary, Socket)
CloseConsole()
NnDllClose(hLibrary)
EndIf
EnableExplicit
IncludeFile "../Core/Nanomsg.pbi"
Global lpszCurrentDir.s = GetCurrentDirectory()
; Nanomsg version (x64)
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
Global lpszLibNnDir.s = "Library/x64"
Global lpszLibNnDll.s = lpszCurrentDir + lpszLibNnDir + "/nanomsg.dll"
SetCurrentDirectory(lpszCurrentDir + lpszLibNnDir)
CompilerEndIf
Global lpszServerAddr.s = "tcp://localhost:1700"
Global hLibrary.i = NnDllOpen(lpszLibNnDll)
If hLibrary
OpenConsole()
Define Socket.i = NnSocket(hLibrary, #AF_SP, #NN_REQ)
Define Rc.i = NnConnect(hLibrary, Socket, lpszServerAddr)
PrintN("Connect to Server: " + lpszServerAddr)
Define i.i
For i = 0 To 10
Define *lpszBuffer = AllocateMemory(32)
Define lpszMessage.s = "From Client"
NnSend(hLibrary, Socket, lpszMessage, Len(lpszMessage), 0)
NnRecv(hLibrary, Socket, *lpszBuffer, MemorySize(*lpszBuffer), 0)
PrintN("Reply From Server: ")
PrintN( PeekS(*lpszBuffer, -1, #PB_UTF8) )
FreeMemory(*lpszBuffer)
Next
NnClose(hLibrary, Socket)
Input()
CloseConsole()
NnDllClose(hLibrary)
EndIf