Main Menu

KB#00783-Modes Available and Sample Code for TCP & UDP Sockets

Title:

Modes Available and Sample Code for TCP & UDP Sockets

Description:

Access to TCP/IP and UDP/IP Sockets from PRO/5 Alias 
The alias name must start with "N" and device is either "TCP" for tcp sockets or "UDP" for udp sockets. 


OPEN() Statement TCP Modes: 
HOST=<host> Host to connect to as a client 
PORT=<port> If a host is specified this is the port to connect to. 
                  Otherwise it is the port to start a server on. 
REUSEADDR Allows the reuse of local addresses. 
KEEPALIVE Enables the periodic transmission on messages on the TCP socket 
DONTROUT Bypass standard routing. 
LINGER Block on close until all data has been sent. 
NODELAY Do not delay send waiting for a full send Buffer; send immediately. 


OPEN() Statement UDP Modes: 
HOST=<host> Host to send packets to by default. 
PORT=<port> If a host is specified this is the port to send packets to by default on 
                  that host. Otherwise it is the port to listen to. 
REUSEADDR Allows the reuse of local addresses. 
DONTROUTE Bypass standard routing. 
BROADCAST Enables the transmission of broadcast messages. 


READ() 
READ RECORD() 
On a tcp client this will give an EOF error when the connection is closed by the server. 

WRITE() 
WRITE RECORD() 

TCP Server: 
KEY=<ip> Which connected client to send message to, if not specified the default 
                  is the last one that data was received from. 

UDP: 
KEY=<ip>:<port> Which machine and port to send message to, if not specified the 
                          default is the last one that data was received from. 

CLOSE() 

KEY() 
IP of connected machine or last received packet. 

KEYF() 
Port signature "IP;port;hostname". Servers will not have an IP or hostname. 

IND() 
Socket number (Each open socket has it's own). 



Resolution:

TCP/IP Server Example Program: 

0005 BEGIN 
0010 OPEN (1,MODE="port=12000")"N0" 
0015 LET IN$="" 
0020 READ (1,ERR=0025)X$; 
PRINT X$; 
LET IN$=IN$; 
GOTO 0020 
0025 PRINT ERR 
0030 PRINT "Connection closed." 
0040 GOTO 0020 

**************************************************************** 

Example config.bbx: 
alias N0 tcp "" NODELAY 
alias N1 udp 

Example Program that uses above ALIAS lines: 
0010 OPEN (1,mode="host=mail,port=25")"N0" 
0020 LET O$="",I$="220"; GOSUB MAIL 
0030 LET O$="HELO myhost",I$="250"; GOSUB MAIL 
0040 LET O$="MAIL From:<me@here.com>",I$="250"; GOSUB MAIL 
0050 LET O$="RCPT To:<you@there.com>",I$="250"; GOSUB MAIL 
0060 LET O$="DATA",I$="354"; GOSUB MAIL 
0070 LET O$="Test email sent directly to sendmail",I$=""; GOSUB MAIL 
0080 LET O$=".",I$="250"; GOSUB MAIL 
0090 LET O$="QUIT",I$="221"; GOSUB MAIL 
0100 PRINT 'RB',"Done!" 
0110 STOP 
0120 MAIL: 
0130 IF LEN(O$) THEN LET OUT$=O$+$0D0A$; WRITE RECORD (1)OUT$ 
0140 LET IN$="" 
0160 READ RECORD (1,SIZ=1,TIM=3,ERR=0165)X$; PRINT X$,; 
LET IN$=IN$+X$; GOTO 0160 
0170 IF LEN(I$) AND POS(I$=IN$)<>1 THEN PRINT 'RB',; 
PRINT (1)'CR','LF'; GOTO 0140 
0180 RETURN 

****************************************************** 
TCP/IP Client Example 

0010 OPEN (1,MODE="host=192.168.1.33,port=12000")"N0" 
0020 INPUT "String to send: ",I$ 
0030 LET I$=I$+$0D0A$; 
WRITE RECORD(1)I$ 
0040 GOTO 0020 



Last Modified: 02/23/2004 Product: PRO/5

BASIS structures five components of their technology into the BBx Generations.

  Google+ View BASIS LinkedIN ProfileVisit our Twitter Feed Check out our Facebook Public Profile Click to View the BASIS youTube channel