gtpc1m3e | Transmission Control Protocol/Internet Protocol |
Following are examples of code segments showing the main socket function calls and the input and output for the calls.
socket function call |
---|
int socket(int domain, int type, int protocol); |
The previous example allocates socket descriptor server_sock in the internet addressing family, AF_INET, using a socket stream type and the default protocol TCP indicated by 0.
bind function call |
---|
int bind(int s, struct sockaddr *name, int namelen); |
The previous example binds socket server_sock to internet address 129.5.24.1 and port 5001.
In this example, the network address and the port address were in the network byte order:
inet_addr was used to convert a character internet address to network byte order.
See each of these functions in the alphabetic reference section of Socket Application Programming Interface Functions Reference.
bind function call |
---|
int bind(int s, struct * *name, int namelen); |
listen function call |
---|
int listen(int s, int backlog); |
This example indicates that the server is ready to accept calls, and that a maximum of 5 connect requests can be queued for the server. Additional requests are ignored.
connect function call |
---|
int connect(int s, struct sockaddr *name, int namelen); |
This example connects socket client_sock to the server with an address servername. This is the same server that was shown in the previous bind. The client could optionally be blocked until the connection is accepted by the server. On a successful return, socket server_sock is associated with the connection to the server.
accept function call |
---|
int accept(int s, struct sockaddr *addr, int *addrlen); |
When the server accepts a connection request on socket server_sock, the name of the client and the length of the client name are returned, along with a new socket descriptor. The new socket descriptor is associated with the client that began the connection, and server_sock is available to accept new connections.
send and recv function calls |
---|
int send(int s, char *msg, int len int flags); int recv(int s, char *msg, int len, int flags); |
The previous example shows an application sending data on a connected socket and receiving data in response. The flag fields can be used to specify additional options for send or recv.
Clients and servers can use many function calls to transfer data, such as:
sendto and recvfrom function calls |
---|
int sendto(int socket, char *buf, int buflen, int flags; struct sockaddr *addr, int addrlen); int recvfrom(int socket, char *buf, int buflen, int flags; struct sockaddr *addr, int addrlen); |
If the socket is not connected, additional socket address information must be passed to sendto and can be optionally returned from recvfrom. The caller must specify the recipient of the data or to be notified of the sender of the data.
Usually, sendto and recvfrom are used for datagram sockets; read, send, and recv are used for stream sockets.
activate_on_receipt function call issued from ECB 1 |
---|
int accept(int s, struct sockaddr *addr, int *addrlen); int activate_on_receipt(unsigned int s, unsigned char *parm, unsigned char *pgm); |
The activate_on_receipt function call allows the issuing ECB to exit and activates a different ECB at program abcd. After the information has been received, the activated program, called the child server program, must issue a read, recv, or recvfrom function call to receive the information. See activate_on_receipt -- Activate a Program after Data Received for more information.
read function call issued from ECB 2 |
---|
: abcd() { |
close function call |
---|
int close(int s); |
In the previous example socket server_sock is closed. The close call shuts down the socket descriptor server_sock and frees up its resources.