Socket Programming - Progress Community

advertisement
PROGRESS WORLDWIDE
Summer Technical Institute
2001
Socket Programming
Edsel Garcia
Principal Technical Support Engineer
How Do Applications Talk?
Web
4GL
Sockets
& Protocols
X App
Java
Summer Technical Institute 2001, Bedford MA, USA
2
© 2001, Progress Software Corporation
Why Sockets in the 4GL?
Stock
Market
Web Server
Mail
Server
4GL
4GL
Java
Summer Technical Institute 2001, Bedford MA, USA
3
© 2001, Progress Software Corporation
Agenda



TCP/IP and socket basics
Application protocol design
4GL sockets
Summer Technical Institute 2001, Bedford MA, USA
4
© 2001, Progress Software Corporation
PROGRESS WORLDWIDE
Summer Technical Institute
2001
TCP/IP and Socket Basics
Clients and Servers


The terms “client” and “server” are in the
eye of the beholder
The Server provides some service
– It waits for connections
– It must be started first
– It does not know who will connect or when

The Client connects to a running server
– It knows who it is connecting to

Both agree on how to exchange data
Summer Technical Institute 2001, Bedford MA, USA
6
© 2001, Progress Software Corporation
Protocol Layers
Application
Telnet
HTTP Your App
TCP
Transport
UDP
Network
IP
Datalink
Ethernet
Summer Technical Institute 2001, Bedford MA, USA
7
© 2001, Progress Software Corporation
Transmission Control Protocol (TCP)


A transport layer protocol
Connection-oriented
– Point-to-point

Byte stream
– Looks like reading and writing files
– You must design the messages

Reliable
– But not guaranteed

Usually called “TCP/IP”
Summer Technical Institute 2001, Bedford MA, USA
8
© 2001, Progress Software Corporation
Sockets

A TCP socket is one end of a virtual
communication link connecting two
programs
– A client and a server

Once a socket is created and a connection
established, you just read and write data
Summer Technical Institute 2001, Bedford MA, USA
9
© 2001, Progress Software Corporation
TCP Addressing
Host herta
Host zeus
Application 1
Application 2
Port 3028
Port 5124
TCP
TCP
IP 172.18.103.49
IP 172.18.103.48
Ethernet
Ethernet
Summer Technical Institute 2001, Bedford MA, USA
10
© 2001, Progress Software Corporation
Servers

Listens for connection requests on a
specified port
– A well-known port
– A registered or ephemeral port




Accept connection requests and get a
socket for each connection
Read and write data as required
Close the connections
Delete the sockets
Summer Technical Institute 2001, Bedford MA, USA
11
© 2001, Progress Software Corporation
Clients





Create a socket
Connect to a server on a specified host
and port – client port is usually
dynamically assigned
Read and write data as necessary
Disconnect from the server
Delete socket
Summer Technical Institute 2001, Bedford MA, USA
12
© 2001, Progress Software Corporation
TCP/IP References

http://www.faqs.org/rfcs
– Complete set of IETF RFC documents

UNIX Network Programming
W. Richard Stevens
ISBN 0-13-490012-X
– Excellent book (mostly) on TCP/IP


Uses C, but very complete
http://www.private.org.il/tcpip_rl.html
– A huge annotated list of TCP/IP resources

Books, web sites, papers, etc.
Summer Technical Institute 2001, Bedford MA, USA
13
© 2001, Progress Software Corporation
PROGRESS WORLDWIDE
Summer Technical Institute
2001
DEMO
PROGRESS WORLDWIDE
Summer Technical Institute
2001
Application Protocol
Design
Protocol

A set of rules that define how we should
behave

Data communication protocols are very
formal and precise
– If you break the rules, you can’t
communicate
– Use messages
Summer Technical Institute 2001, Bedford MA, USA
16
© 2001, Progress Software Corporation
Message Types



Most protocols use more than one kind of
message
Each kind of message is identified by a
message type of some kind
Types are typically identified by
– An integer type code

Login = 1, logout = 2, get time = 3, etc.
– A sequence of characters

“GET”, “POST”, etc.
Summer Technical Institute 2001, Bedford MA, USA
17
© 2001, Progress Software Corporation
Message Formats




Field layouts
Message and field size
Numeric byte-order
Character encoding
Summer Technical Institute 2001, Bedford MA, USA
18
© 2001, Progress Software Corporation
Example Message Layout
csnet
header
version
csmssg
header
version
length
seq-num
hdl
type
Optional data
Summer Technical Institute 2001, Bedford MA, USA
19
© 2001, Progress Software Corporation
Field Layout
Type = connect
length
username
length
password
length
info
Summer Technical Institute 2001, Bedford MA, USA
20
© 2001, Progress Software Corporation
Numeric Byte Ordering
Low-order
byte
High-order
byte
07 00
address a
address a+1
big-endian
High-order
byte
Low-order
byte
00 07
address a
address a + 1
little-endian
0x0007 does not equal 0x0700
Summer Technical Institute 2001, Bedford MA, USA
21
© 2001, Progress Software Corporation
Character Encoding






ASCII
EBCDIC
Unicode
UTF-8
ISO8859-1
ISO8859-8
Summer Technical Institute 2001, Bedford MA, USA
22
© 2001, Progress Software Corporation
PROGRESS WORLDWIDE
Summer Technical Institute
2001
DEMO
PROGRESS WORLDWIDE
Summer Technical Institute
2001
4GL Sockets
4GL Sockets

Connecting a client to a server

Accepting connections on a server

Reading and writing data on a socket

Marshalling and unmarshalling data
Summer Technical Institute 2001, Bedford MA, USA
25
© 2001, Progress Software Corporation
Connecting a Client to a Server


CREATE SOCKET handle
handle:CONNECT(connection-params-string)
– connection-params-string



-H - Host or IP Address to connect to
-S - port to connect to - a number or service
handle:DISCONNECT()
Summer Technical Institute 2001, Bedford MA, USA
26
© 2001, Progress Software Corporation
Connecting a Client to a Server
DEFINE VARIABLE sckt-hndl AS HANDLE.
DEFINE VARIABLE ret AS LOGICAL.
CREATE SOCKET sckt-hndl.
ret = sckt-hndl:CONNECT ("-H acura -S 80").
/* Do fun stuff with the socket */
ret = sckt-hndl:DISCONNECT ().
Summer Technical Institute 2001, Bedford MA, USA
27
© 2001, Progress Software Corporation
Accepting Connections on a Server

SERVER-SOCKET object

ENABLE-CONNECTIONS(connectionparams-string) method
– Use –S parameter to specify the port to
listen on
Summer Technical Institute 2001, Bedford MA, USA
28
© 2001, Progress Software Corporation
Accepting Connections on a Server



CONNECT event on SERVER-SOCKET
object accepts connections
SET-CONNECT-PROCEDURE(internalproc) method to process new connections
Input parameter to connect event
procedure is the new socket
Summer Technical Institute 2001, Bedford MA, USA
29
© 2001, Progress Software Corporation
Accepting Connections on a Server
CREATE SERVER-SOCKET sckt-hndl NO-ERROR.
log = sckt-hndl:ENABLE-CONNECTIONS("-S 5555").
sckt-hndl:SET-CONNECT-PROCEDURE
("connHandler",THIS-PROCEDURE).
WAIT-FOR CLOSE OF THIS-PROCEDURE.
PROCEDURE connHandler. /* Handle Connect Events */
DEFINE INPUT PARAMETER connect-sckt AS HANDLE.
/* Do fun stuff with connect-sckt */
END PROCEDURE.
Summer Technical Institute 2001, Bedford MA, USA
30
© 2001, Progress Software Corporation
Reading and Writing Data on a Socket

Using MEMPTRs

READ() method

WRITE () method

Supporting the I/O models
Summer Technical Institute 2001, Bedford MA, USA
31
© 2001, Progress Software Corporation
Using Memptrs




Define a MEMPTR variable
Allocate a region of memory using
SET-SIZE(ptr) = n
Read and write to specified locations
within the MEMPTR
Free the memory using
SET-SIZE(ptr)=0
Summer Technical Institute 2001, Bedford MA, USA
32
© 2001, Progress Software Corporation
Read/Writing a Memptr
Numeric Datatypes

PUT-datatype ( memptr , byte-position ) = expression

GET-datatype ( memptr , byte-position )
GET Methods
PUT Methods
GET-BYTE
GET-SHORT
GET-LONG
GET-FLOAT
GET-DOUBLE
PUT-BYTE
PUT-SHORT
PUT-LONG
PUT-FLOAT
PUT-DOUBLE
Summer Technical Institute 2001, Bedford MA, USA
33
Length
1
2
4
4
8
© 2001, Progress Software Corporation
Read/Writing a Memptr
Setting Byte Order

SET-BYTE-ORDER( memptr ) = int-expression
– BIG-ENDIAN
– LITTLE-ENDIAN
Summer Technical Institute 2001, Bedford MA, USA
34
© 2001, Progress Software Corporation
Reading/Writing a Memptr
Strings


Either NULL terminated or based on number
of bytes specified
You need to convert to the correct code page
with CODEPAGE-CONVERT() function
PUT-STRING ( memptr, position [ , numbytes ] ) = expression
String = GET-STRING ( memptr, position [ , numbytes ] )
Summer Technical Institute 2001, Bedford MA, USA
35
© 2001, Progress Software Corporation
Reading/Writing a Memptr
A few others

PUT-BYTES, GET-BYTES
– moves RAW and MEMPTRs

PUT-BITS, GET-BITS
– manipulates bits within INTEGERs
Summer Technical Institute 2001, Bedford MA, USA
36
© 2001, Progress Software Corporation
Writing to a Socket

WRITE(memptr,
position,
bytes-to-write )

Only means that the data has been written
No guarantee that the other endpoint
received it or understood it

Summer Technical Institute 2001, Bedford MA, USA
37
© 2001, Progress Software Corporation
Example Message
4
ID
4
LEN
LEN
NAME
Total Length = 8 + Len
Summer Technical Institute 2001, Bedford MA, USA
38
© 2001, Progress Software Corporation
Example - Writing to a Socket
DEFINE
DEFINE
DEFINE
DEFINE
DEFINE
VARIABLE
VARIABLE
VARIABLE
VARIABLE
VARIABLE
hSocket AS HANDLE.
mBuffer AS MEMPTR.
id AS INTEGER INITIAL 543.
len as INTEGER.
name AS CHAR.
name = ”YourNameHere".
len = LENGTH(name, “RAW”).
SET-SIZE(mBuffer) = len + 8.
SET-BYTE-ORDER(mBuffer) = BIG-ENDIAN.
PUT-LONG(mbuffer,1) = id.
PUT-LONG(mbuffer,5) = len.
PUT-STRING(mBuffer,9,len) = name.
hSocket:WRITE(mBuffer,1,GET-SIZE(mBuffer)).
Summer Technical Institute 2001, Bedford MA, USA
39
© 2001, Progress Software Corporation
Reading from a Socket



READ(memptr,
position,
bytes-to-read
[,mode] )
Read is blocking - You will get a least 1 byte or
an error
How many bytes read depends on the mode and
the number specified
– Read Available
– Read Exactly
Summer Technical Institute 2001, Bedford MA, USA
40
© 2001, Progress Software Corporation
Example - Reading from a Socket
DEFINE
DEFINE
DEFINE
DEFINE
DEFINE
VARIABLE
VARIABLE
VARIABLE
VARIABLE
VARIABLE
hSocket AS HANDLE.
mBuffer AS MEMPTR.
id AS INTEGER.
len AS INTEGER.
name AS CHAR.
SET-SIZE(mBuffer) = 8.
SET-BYTE-ORDER(mBuffer) = BIG-ENDIAN.
hSocket:READ(mBuffer,1,8).
id = GET-LONG(mBuffer, 1).
len = GET-LONG(mBuffer,5).
SET-SIZE(mBuffer) = 0. SET-SIZE(mBuffer) = len.
hSocket:READ(mBuffer,1,len).
name = GET-STRING(mBuffer,1,len).
Summer Technical Institute 2001, Bedford MA, USA
41
© 2001, Progress Software Corporation
Supporting Different I/O Models
Blocking

Use the Read() method
SET-SIZE(mBuffer) = 4.
SET-BYTE-ORDER(mBuffer) = BIG-ENDIAN.
hSocket:READ(mBuffer,1,4).
Len = GET-LONG(mBuffer,1).
SET-SIZE(mBuffer) = 0.
SET-SIZE(mBuffer) = len.
hSocket:READ(mBuffer,1,len).
Summer Technical Institute 2001, Bedford MA, USA
42
© 2001, Progress Software Corporation
Supporting Different I/O Models
Non-Blocking



Use the CONNECTED() method to check
for valid connection
Use the BYTES-AVAILABLE() method to
check if bytes are available and how many
Use the READ() method to read the data
that is available
Summer Technical Institute 2001, Bedford MA, USA
43
© 2001, Progress Software Corporation
Non-blocking Read()
SET-SIZE(mptr) = msgsize.
got = 0.
REPEAT WHILE got < msgsize:
IF NOT hSocket:CONNECTED() THEN LEAVE.
len = hSocket:GET-BYTES-AVAILABLE().
IF len > 0 THEN do:
hSocket:READ(mptr, got + 1, len).
got = got + len.
END.
ELSE do: /* something while waiting */ end.
END.
rsp = GET-STRING(mptr,2,got - 2).
Summer Technical Institute 2001, Bedford MA, USA
44
© 2001, Progress Software Corporation
Supporting Different I/O Models
Event-Driven



READ-RESPONSE event on a SOCKET
object
SET-READ-RESPONSE-PROCEDURE
(internal-proc)
- method to set event procedure
Use SELF to reference SOCKET object
Summer Technical Institute 2001, Bedford MA, USA
45
© 2001, Progress Software Corporation
Event-Driven Input for a Client
Data
Wait-for
Read-ResponseProcedure
SELF:Read()
Summer Technical Institute 2001, Bedford MA, USA
46
© 2001, Progress Software Corporation
Event-Driven Client
CREATE SOCKET sckt-hndl.
ret = sckt-hndl:CONNECT ("-H acura -S 80").
ret = sckt-hndl:SET-READ-RESPONSE-PROCEDURE
("readHandler", THIS-PROCEDURE).
REPEAT ON STOP UNDO, LEAVE ON QUIT UNDO, LEAVE:
/* … leave condition */
WAIT-FOR READ-RESPONSE OF sckt-hndl.
END.
PROCEDURE readHandler.
/* read-response event */
SELF:READ(mem1,...).
END PROCEDURE.
Summer Technical Institute 2001, Bedford MA, USA
47
© 2001, Progress Software Corporation
Event-Driven Input for a Server
Connect Procedure
Connect
Wait-for
Data
hdl:set-read-procedure()
Read-ResponseProcedure
SELF:Read()
Summer Technical Institute 2001, Bedford MA, USA
48
© 2001, Progress Software Corporation
Event-Driven Server
CREATE SERVER-SOCKET sckt-hndl NO-ERROR.
log = sckt-hndl:ENABLE-CONNECTIONS("-S 5555").
sckt-hndl:SET-CONNECT-PROCEDURE
("connectHandler",THIS-PROCEDURE).
WAIT-FOR U1 OF THIS-PROCEDURE.
PROCEDURE connectHandler.
/* Connect Event */
DEFINE INPUT PARAMETER read-sckt AS HANDLE.
read-sckt:SET-READ-RESPONSE-PROCEDURE
("readHandler",THIS-PROCEDURE).
END PROCEDURE.
Summer Technical Institute 2001, Bedford MA, USA
49
© 2001, Progress Software Corporation
Event-Driven Server (cont)
PROCEDURE readHandler.
/* read-response event */
SET-SIZE(mBuffer) = 8.
SET-BYTE-ORDER(mBuffer) = BIG-ENDIAN.
hSocket:READ(mBuffer,1,8).
id = GET-LONG(mBuffer, 1).
len = GET-LONG(mBuffer,5).
SET-SIZE(mBuffer) = 0.
SET-SIZE(mBuffer) = len.
hSocket:READ(mBuffer,1,len).
name = GET-STRING(mBuffer,1,len).
END PROCEDURE.
Summer Technical Institute 2001, Bedford MA, USA
50
© 2001, Progress Software Corporation
PROGRESS WORLDWIDE
Summer Technical Institute
2001
DEMO
Sockets, Another Method to
Communicate Applications
Web
4GL
Sockets
& Protocols
X App
Java
Summer Technical Institute 2001, Bedford MA, USA
52
© 2001, Progress Software Corporation
Summary

4GL socket communication is easy

Protocol design is hard

You can talk to many things if you know
the rules, but…
– You must know the rules
– You must obey the rules
Summer Technical Institute 2001, Bedford MA, USA
53
© 2001, Progress Software Corporation
Questions
Summer Technical Institute 2001, Bedford MA, USA
54
© 2001, Progress Software Corporation
Download