General steps to write a Socket program under UNIX

zhaozj2021-02-11  190

General steps to write a Socket program under UNIX

Yongyang · Yesky

Write a Socket program under UNIX may be the most convenient, you only need to master the general steps, you can loose the application of the surface to the transport layer. 1. Understand a few common Socket functions #include

#include

INT Socket (int Domain, int type, int portocol);

Domain refers to the protocol (Family) used for AF_UNIX and AF_INET, which generally only uses AF_INET (referred to in Internet TYPE), which can be SOCK_STERAM (connected TCP), and SOCK_DGRAM (facile-free UDP)

INT Bind (int S, const struct sockaddr * address, size_t address_len);

s file descriptor returned for socket

Address is the name and other information of the protocol

The specific structure is struct sockaddr_in {

Short sin_family; / * protocol

U_SHORT SIN_PORT; / * Port * /

Struct in_addr sin_addr; / * address * /

Char sin_zero [8];

}

INT Listen (int S, int backlog);

BACKLOG is the number of requests

INT Accept (int S, struct sockaddr * address, int * address_len);

The first two parameters here are

Addres_len is to deliver an address that has a structured size?

INT Connect (int S, Struct SockAddr * Address, Size_t Address_len);

The parameters here are bind

2.

Understand the general call process of establishing a program

To create a server-side program that processes the connection, first call the Socket function to create a socket, return a file handle FD, so that it will read and write the ordinary file device.

Since the server side must listen to other machines for a fracture, then call the bind function, incoming the FD, define the address and port, because it is necessary to accept the connection from any Host, so SIN_ADDR is intended to INADDR_Any , Port is the port you set.

Note: The address and port here is the network byte order, so to call HTONL, HTons Complete the host byte order

Transformation to the network byte

The next is to listen to Listen, call Accept Accept request from the client, and the Accpet returns a file descriptor after the connection, you can use it to send and receive information (corresponding to Read, Write), Socket-> Bind-> Listen -> Accpet-> Read, Write

And for the client is Socket-> Connect-> Read, Write

3. A complete program

#include

#include

#include

/

* Contains header files with functions such as HTons *

/

#include

#include

void main ()

{

INT Listenfd, Clifd;

Long PID;

Struct SockAddr_in myaddr, cliaddr;

int R;

Int Len;

Listenfd = Socket (AF_INET, SOCK_STREAM, 0);

Listenfd <0)

{

PERROR ("socket error");

EXIT (-1);

}

myaddr.sin_family = af_INet;

myaddr.sin_addr.s_addr = HTONL (INADDR_Any);

myaddr.sin_port = htons (8888);

Ret = bind (Struct SockAddr *) & myaddr, sizeof (myaddr); if (RET <0)

{

PERROR ("Bind Error");

EXIT (-1);

}

Listen (Listenfd, 10);

Len = sizeof (struct sockaddr);

While (1)

{

CLIFD = Accept (Listenfd, Struct SockAddr *) & cliaddr, & len;

/

* Note that the third parameter of Accept is also the address * /

IF (clifd == - 1)

{

Perror ("Accept Error");

CONTINUE;

}

Printf ("Connect FROM% S% D / N", INET_NTOA (Cliaddr), NTOHS (Cliaddr.sin_Port);

Switch (PID = for ())

{

Case 0: /

* Child process * /

Close (Listenfd);

; /

* Sub-process for other operations * /

Close (Clifd);

exit (0);

Break;

Case -1:

PERROR ("fork error");

Break;

DEFAULT: /

* Parent Process * /

Close (Clifd);

Break;

}

}

}

4.

Program description

The functionality of the program is to listen to the connection of the 8888 port. Display the address of all the 8888 ports displaying the address and the other party's port number This program is powered by the SCO UNIX, please note that INT_NTOA, the HTons function should be approved in other UNIX and Linux platforms. Name of header file

At the same time, the program uses concurrent views, because Accept, Read, Write are block-up function, once the process Block will not handle other requests, so use the master process to perform Listen, which is responsible for transmitting data on the client.

You can use Telnet localhost 8888 with the same UNIX machine to output Connect from 127.0.0.1 xxxx

转载请注明原文地址:https://www.9cbs.com/read-4509.html

New Post(0)