Thanks a bunch, works great. :] Solved my prob.
------------------
Announcement
Collapse
No announcement yet.
UDP Query
Collapse
X
-
Guest replied
-
Guest repliedHello Matt,
Here is some code to help you get started. The first is the client side. You will need to put your own valid IP address in the $SERVERADDRESS constant. The second source is the server side. You should run the server first and press the start button. Next run and client and press it's start button. The two programs will send UDP packets back and forth until one of them is stopped by pressing the stop button. Great care should be taken with these examples as they use an edit box as a console. If you are running 9x you will probably encounter an error after a while because edit boxes under 9x can only hold 64k worth of text. I hope this helps!
OOOOPS!!!
I forgot that I was using TCP here! I am really sorry but this might still be a good example.
CLIENT SOURCE CODE:
===================
Code:#compile exe #include "win32api.inc" #include "wsock32.inc" $SERVERADDRESS = "" rem put IP address of server here ie: 127.0.0.1 %SERVERTIMEOUT = 1 %SERVERPORT = 2000 %BUFFERSIZE = 2048 %IDLABEL = 100 %IDSTART = 102 %IDCONSOLE = 103 %IDSERVER = 104 global hClient as long function cprint(byval hdlg as long,byval Message as string) as long local Buffer as string local Lines as long replace "\\" with "\" in Message replace "\n" with $CRLF in Message control get text hdlg,%IDCONSOLE to Buffer control set text hdlg,%IDCONSOLE,Buffer & Message control send hdlg,%IDCONSOLE,%EM_GETLINECOUNT,0,0 to Lines control send hdlg,%IDCONSOLE,%EM_LINESCROLL,0,Lines end function function ErrorMessage(byval ErrorCode as long) as string select case (ErrorCode) case 0:function = "ok\n" case 5:function = "Illegal function all\n" case 7:function = "Out of memory\n" case 9:function = "Subscript/Pointer out of range\n" case 24:function = "Device time-out\n" case 51:function = "Internal error\n" case 52:function = "Bad file name or number\n" case 53:function = "File not found\n" case 54:function = "Bad file mode\n" case 55:function = "File is already open\n" case 57:function = "Device I/O error\n" case 58:function = "File already exists\n" case 61:function = "Disk full\n" case 62:function = "Input past end\n" case 63:function = "Bad record number\n" case 64:function = "Bad file name\n" case 67:function = "Too many files\n" case 68:function = "Device unavailable\n" case 70:function = "Permission denied\n" case 71:function = "Disk not ready\n" case 72:function = "Disk media error\n" case 74:function = "Rename across disks\n" case 75:function = "Path/File access error\n" case 76:function = "Path not found\n" case else:function = "Internal error\n" end select end function callback function StartButton as long if (hClient) then call cprint(cbhndl,"tcp close...") control set text cbhndl,%IDSTART,"Start" tcp close hClient hClient = 0 call cprint(cbhndl,ErrorMessage(err)) if errclear then exit function else call cprint(cbhndl,"tcp open...") hClient = freefile tcp open port %SERVERPORT at $SERVERADDRESS as hClient timeout %SERVERTIMEOUT call cprint(cbhndl,ErrorMessage(err)) if errclear then hClient = 0 exit function end if call cprint(cbhndl,"tcp notify(CLOSE,SEND,RECV)...") tcp notify hClient,close send recv to cbhndl as %WM_USER call cprint(cbhndl,ErrorMessage(err)) if errclear then call cprint(cbhndl,"tcp close...") tcp close hClient hClient = 0 call cprint(cbhndl,ErrorMessage(errclear)) exit function end if control set text cbhndl,%IDSTART,"Stop" end if end function callback function DialogProc as long local Buffer as string local lAddress as long local lIndex as long local lPort as long local Item as DRAWITEMSTRUCT ptr select case cbmsg case %WM_INITDIALOG host name to Buffer control set text cbhndl,%IDSERVER,Buffer case %WM_DESTROY if (hClient) then tcp close hClient case %WM_DRAWITEM Item = cblparam control get text cbhndl,cbwparam to Buffer if (@Item.ItemState and %ODS_SELECTED) then FillRect @Item.hdc,@Item.rcItem,GetSysColorBrush(%COLOR_3DFACE) DrawText @Item.hdc,bycopy Buffer,len(Buffer),@Item.rcItem,%DT_CENTER or %DT_SINGLELINE or %DT_VCENTER DrawEdge @Item.hdc,@Item.rcItem,%BDR_SUNKENINNER,%BF_RECT else FillRect @Item.hdc,@Item.rcItem,GetSysColorBrush(%COLOR_3DFACE) DrawText @Item.hdc,bycopy Buffer,len(Buffer),@Item.rcItem,%DT_CENTER or %DT_SINGLELINE or %DT_VCENTER DrawEdge @Item.hdc,@Item.rcItem,%BDR_RAISEDINNER,%BF_RECT end if case %WM_USER select case lowrd(cblparam) case %FD_CLOSE control set text cbhndl,%IDSTART,"Start" call cprint(cbhndl,"tcp message(CLOSE)...") tcp close hClient hClient = 0 call cprint(cbhndl,ErrorMessage(errclear)) case %FD_READ call cprint(cbhndl,"tcp message(READ)...") tcp recv hClient,%BUFFERSIZE,Buffer call cprint(cbhndl,ErrorMessage(err)) if errclear then exit function if (Buffer = "next") then call cprint(cbhndl,"tcp send...") tcp send hClient,string$(%BUFFERSIZE,rnd(65,127)) call cprint(cbhndl,ErrorMessage(err)) if errclear then exit function end if case %FD_WRITE call cprint(cbhndl,"tcp message(WRITE)...ok\n") call cprint(cbhndl,"tcp send...") tcp send hClient,"send:mark.txt" call cprint(cbhndl,ErrorMessage(err)) if errclear then exit function end select end select end function function pbmain as long local hdlg as long dialog new %HWND_DESKTOP,"Client",0,0,256,218,%WS_POPUPWINDOW or %WS_CAPTION or %DS_CENTER to hdlg control add label,hdlg,%IDLABEL,"Server",4,4,24,10,%SS_CENTERIMAGE or %SS_RIGHT control add label,hdlg,%IDSERVER,"",32,4,176,10,%SS_CENTERIMAGE or %SS_SUNKEN control add textbox,hdlg,%IDCONSOLE,"",4,20,248,194,%ES_AUTOHSCROLL or %ES_AUTOVSCROLL or %ES_MULTILINE,%WS_EX_STATICEDGE control add button,hdlg,%IDSTART,"Start",212,4,40,10,%BS_OWNERDRAW call StartButton dialog show modal hdlg call DialogProc end function
===================
Code:#compile exe #include "win32api.inc" #include "wsock32.inc" %SERVERTIMEOUT = 1 %SERVERPORT = 2000 %BUFFERSIZE = 2048 %IDLABEL = 100 %IDSTART = 101 %IDCONSOLE = 102 %IDSERVER = 103 global hClient as long global hServer as long function cprint(byval hdlg as long,byval Message as string) as long local Buffer as string local Lines as long replace "\\" with "\" in Message replace "\n" with $CRLF in Message control get text hdlg,%IDCONSOLE to Buffer control set text hdlg,%IDCONSOLE,Buffer & Message control send hdlg,%IDCONSOLE,%EM_GETLINECOUNT,0,0 to Lines control send hdlg,%IDCONSOLE,%EM_LINESCROLL,0,Lines end function function ErrorMessage(byval ErrorCode as long) as string select case (ErrorCode) case 0:function = "Successfull\n" case 5:function = "Illegal function all\n" case 7:function = "Out of memory\n" case 9:function = "Subscript/Pointer out of range\n" case 24:function = "Device time-out\n" case 51:function = "Internal error\n" case 52:function = "Bad file name or number\n" case 53:function = "File not found\n" case 54:function = "Bad file mode\n" case 55:function = "File is already open\n" case 57:function = "Device I/O error\n" case 58:function = "File already exists\n" case 61:function = "Disk full\n" case 62:function = "Input past end\n" case 63:function = "Bad record number\n" case 64:function = "Bad file name\n" case 67:function = "Too many files\n" case 68:function = "Device unavailable\n" case 70:function = "Permission denied\n" case 71:function = "Disk not ready\n" case 72:function = "Disk media error\n" case 74:function = "Rename across disks\n" case 75:function = "Path/File access error\n" case 76:function = "Path not found\n" case else:function = "Internal error\n" end select end function callback function StartButton as long if (hServer) then if (hClient) then call cprint(cbhndl,"tcp close(CLIENT)...") control set text cbhndl,%IDSTART,"Start" tcp close hClient hClient = 0 call cprint(cbhndl,ErrorMessage(errclear)) end if call cprint(cbhndl,"tcp close(SERVER)...") control set text cbhndl,%IDSTART,"Start" tcp close hServer hServer = 0 call cprint(cbhndl,ErrorMessage(errclear)) else control set text cbhndl,%IDSTART,"Stop" call cprint(cbhndl,"tcp open(SERVER)...") hServer = freefile tcp open server port %SERVERPORT as hServer timeout %SERVERTIMEOUT call cprint(cbhndl,ErrorMessage(err)) if err then hServer = 0 exit function end if call cprint(cbhndl,"tcp notify(ACCEPT)...") tcp notify hServer,accept to cbhndl as %WM_USER call cprint(cbhndl,ErrorMessage(err)) if errclear then call cprint(cbhndl,"tcp close(SERVER)...") tcp close hServer hServer = 0 call cprint(cbhndl,ErrorMessage(errclear)) exit function end if end if end function callback function DialogProc as long local Buffer as string local lAddress as long local lIndex as long local lPort as long local Item as DRAWITEMSTRUCT ptr select case cbmsg case %WM_INITDIALOG host name to Buffer control set text cbhndl,%IDSERVER,Buffer case %WM_DESTROY if (hClient) then tcp close hClient if (hServer) then tcp close hServer case %WM_DRAWITEM Item = cblparam control get text cbhndl,cbwparam to Buffer if (@Item.ItemState and %ODS_SELECTED) then FillRect @Item.hdc,@Item.rcItem,GetSysColorBrush(%COLOR_3DFACE) DrawText @Item.hdc,bycopy Buffer,len(Buffer),@Item.rcItem,%DT_CENTER or %DT_SINGLELINE or %DT_VCENTER DrawEdge @Item.hdc,@Item.rcItem,%BDR_SUNKENINNER,%BF_RECT else FillRect @Item.hdc,@Item.rcItem,GetSysColorBrush(%COLOR_3DFACE) DrawText @Item.hdc,bycopy Buffer,len(Buffer),@Item.rcItem,%DT_CENTER or %DT_SINGLELINE or %DT_VCENTER DrawEdge @Item.hdc,@Item.rcItem,%BDR_RAISEDINNER,%BF_RECT end if case %WM_USER select case lowrd(cblparam) case %FD_ACCEPT call cprint(cbhndl,"tcp message(ACCEPT)...") hClient = freefile tcp accept hServer as hClient call cprint(cbhndl,ErrorMessage(err)) if errclear then exit function call cprint(cbhndl,"tcp notify(CLOSE,SEND,RECV)...") tcp notify hClient,close send recv to cbhndl as %WM_USER call cprint(cbhndl,ErrorMessage(errclear)) case %FD_CLOSE call cprint(cbhndl,"tcp message(CLOSE)...") tcp close hClient hClient = 0 call cprint(cbhndl,ErrorMessage(errclear)) case %FD_READ call cprint(cbhndl,"tcp message(READ)...") tcp recv hClient,%BUFFERSIZE,Buffer call cprint(cbhndl,ErrorMessage(err)) if errclear then exit function call cprint(cbhndl,"tcp send...") tcp send hClient,"next" call cprint(cbhndl,ErrorMessage(errclear)) case %FD_WRITE call cprint(cbhndl,"tcp message(WRITE)...ok\n") end select end select end function function pbmain as long local hdlg as long dialog new %HWND_DESKTOP,"Server",0,0,256,218,%WS_POPUPWINDOW or %WS_CAPTION or %DS_CENTER to hdlg control add label,hdlg,%IDLABEL,"Server",4,4,24,10,%SS_CENTERIMAGE or %SS_RIGHT control add label,hdlg,%IDSERVER,"",32,4,176,10,%SS_CENTERIMAGE or %SS_SUNKEN control add textbox,hdlg,%IDCONSOLE,"",4,20,248,194,%ES_AUTOHSCROLL or %ES_AUTOVSCROLL or %ES_MULTILINE,%WS_EX_STATICEDGE control add button,hdlg,%IDSTART,"Start",212,4,40,10,%BS_OWNERDRAW call StartButton dialog show modal hdlg call DialogProc end function
Cheers
[This message has been edited by mark smit (edited January 13, 2001).]
Leave a comment:
-
UDP Query
I'm trying to write a program that sends a query to a UDP server, and in turn the UDP server returns some information, now the problem I'm having is not being able to capture the data the server is returning, do I need two seperate sockets, one to listen on port 27666 and another to send data over port 27666, or can it be done with 1 socket? Do any of you know of any good UDP examples?
Thanks in advance,
Matt
------------------
[This message has been edited by Matt Grace (edited January 12, 2001).]Tags: None
Leave a comment: