Linux program entry - Socketinetd programming

zhaozj2021-02-08  244

Linux program entry - Socket / inetd programming

Unix socket programing is basically a book name. Socket programing actually needs

When the degree of the foundation, I don't want to include the sea to the sea. If you need thorough research, you can buy this

The book is seen. Here I want to provide some simple writing on both ends of Server / Client, let you have a start.

Point, as a foundation for further research. Many more complicated content, I am not detailed here.

Ming, you can use this Xuankai, take it, and then familiarize yourself.

inetd provides a passive server service, that is, the server is started to be used, usually

exist. For example, FTP, Telnetd, POP3, IMAP, AUTH, etc. When these services are used,

No need to start. In addition, inetd converts socket into stdin / stdout, thus making network services

The design is greatly simplified, you can use Printf and FGETS to complete the complex network protocol.

Client Int Sock_connect (Char * Domain, INT Port) {INT White_sock

Struct Hostent * Site; Struct SockAddr_in Me; Site = GethostByName (Domain); if (Site == Null) Return -2; White_Sock = Socket (AF_INET, SOCK_STREAM, 0); if (White_Sock <0) Return -1; Memset & me, 0, SIZEOF (Struct SockAddr_in); Memcpy (& Me.SIN_ADDR, Site-> H_ADDR_List [0], Site-> H_LENGTH); Me.SIN_FAMILY = AF_INET; Me.SIN_PORT = HTONS (port); Return (Connect) White_sock, (struct socmeddr *) & me, sizeof (struct sockaddr)) <0)? -1: white_sock;}

The steps to connect to the servo end to the server side, first you have to find out the location of the other party, can

use:

gethostbyname ()

Next to build a socket, then use this socket to establish a connection.

Next, we use this simple Socket program to write a simple browser that reads the WWW web page (see

HTML SOURCE).

#include #include #include #include #include #include #include int htconnect (char * domain, int port) {int white_sock; struct hostent * site; struct sockaddr_in me; site = gethostbyname (domain); if (site == NULL) return -2; white_sock = socket (AF_INET, SOCK_STREAM, 0); if (White_Sock <0) Return -1; Memset (& me, 0, sizeof (Struct SockAddr_in)); Memcpy (& Me.SIN_ADDR, Site-> h_addr_list [0], site-> h_length); me. SIN_FAMILY = AF_INET; Me.SIN_PORT = HTONS (Port); Return (Connect (White_Sock, (Struct SockAddr *) & Me, Sizeof (Struct SockAddr)) <0)? -1: White_Sock;} int htsend (int sock, char * FMT, ...) {Char BUF [1024]; VA_List Argptr; VA_Start (Argptr, FMT); vsprintf (BUF, FMT, Argptr); VA_END (Argptr); Return Send (Sock, BUF, Strlen (BUF), 0 } void main (int Argc, char ** argv) {int black_sock; char bus_bunny [3]; if (argc <2) returnne; black_sock = htconnect (Argv [1], 80); if (Black_Sock <0)Return; htsend (Black_Sock, "GET / HTTP / 1.0% C", 10); HTSEND (Black_Sock, "Host:% S% C", Argv [1], 10); Htsend (Black_Sock, "% C", 10 ); while (Read (Black_Sock, Bugs_bunny, 1)> 0) Printf ("% c", bugs_bunny [0]);} close (black_sock);} Compile:

GCC -O EX1 Client.c

carried out

./ex1 www.linux.org.tw server listen to a port

To build a network server, the first step is to "listen to the distance", that is, to listen.

The following is a method of building a service:

int DaemonSocket; struct sockaddr_in DaemonAddr; int BindSocket (void) {DaemonSocket = socket (AF_INET, SOCK_STREAM, 0); if (DaemonSocket == - 1) return 0; DaemonAddr.sin_family = AF_INET; DaemonAddr.sin_port = htons (DAEMON_PORT); IF (Bind (DaemonSocket, & Daemonaddr, Sizeof (Daemonaddr)) <0) {Printf ("can not bind! / n"); return 0;}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} NOT LISTEN! / N "); Return 0;} Return 1;} INCOMING CALL To see if there is a connection, you can use the following way:

INT INCOMING_CALL (Void) {FD_SET SOCK; STRUCT TIMEVAL TV; INT T; fd_zero (& Sock); fd_set (daemonpsignal (); if (! bindsocket ()) {Printf ("can not bind socket! / n"); exit 1);} Writelock ();} Printf ("Chess Daemon IS Up, Have Fun! / N"); Now = Time (NULL); DLOG ("--------------- ----------------------------- / n "); DLOG (" I am back!% s "" chess daemon comes To alive again./n ", asctime (constim"); do {if (incoming_call ()) {if (connectClient ()) {fd_set sock; struct TimeVal TV; int T; char BUF [128]; CHAR CC [2]; INT N; DAEMON_PRINTF ("Welcome to Chinese Chess Game Center! / N"); fd_zero (& Sock); fd_set (clientsocket, & sock); n = 0; do {tv.tv_sec = 60; tv.tv_usec = 0; T = SELECT (ClientSocket 1, & Sock, NULL, NULL, & TV); if (t <= 0 ||! Fd_isset (clientsocket, & sock); Read (ClientSocket, CC, 1 ); if (CC [0] == 13 || CC [0] == 10 || CC [0] == 0) {BUF [N] = 0; DLOG ("% S / N", BUF); IF (strncasecmp (buf, "exit", 4) == 0) {close (clientSocket); Break;} n = 0;} ELS e {buf [n] = cc [0]; n ;}} while (1);}}} while (1); return 1;}

Telnet Localhost 9901

In dealing with the Connect Client, in fact, fork or thread can be used to handle multiple connectors.

inetd protemming

Using inetd to do a network program design is a simple and stable design method, you don't need to consider

Miscellaneous socket programing. Your design work is almost completed after designing the communication agreement.

The tricks needed, only for simple text analysis skills.

Goodie inet service

First, let's write a service called Goodie.

Goodie.c #include #include #include void main (void) {Printf ("Welcome to Goodie Service! / N");}

This program is very simple, isn't it?

Compile

GCC -O Goodie GoodIE.c

Set / etc / services and /etc/inetd.conf Add this line in / etc / services

Goodie 20001 / TCP

Its significance is that the Goodie This service is in Port 20001, TCP Agreement.

Next, add the following line in /etc/inetd.conf

Goodie Stream TCP NOWAIT ROOT / FULL_GOODIE_PATH_NAME / Goodie

The significance of each number

Service_name needs to be named in Services.

Sock_type has many kinds, mostly STREAM / DGRAM.

Proto generally uses TCP / UDP.

Flags has Wait / NOWAIT.

User is you specified that the program is to start with that user, this example is root, if

Safety is considered, you should use Nobody. In general, it is recommended that you use low privileges.

Do not open root use rights unless necessary.

Server_Path and Args, this is your location of your service and the number you want to join.

Next to restart inetd

KILLALL INETD inetd

This way we build a port 20001 Goodie Service.

Now let's check if your good can perform:

Telnet Localhost 20001

or

Telnet your_host_name 20001

Results of the

Trying 127.0.0.1 ... connect to localhost. Escape character is '^]'. Welcome to Goodie Service! Connection Closed by Foreign Host.

Very simple? Letter do not believe in you, Telnet / POP3 / IMAP / FTP is established by this way

Service.

We now create a little "Internet Agreement", this agreement allows us to enter "exit",

Outline, while other instructions are output and input the same string.

#include #include #include void main (void)

{Char BUF [1024]; int OK; "Welcome to Goodie Service! / N"); fflush (stdout); ok = 0; do {while (Fgets (BUF, 1023, stdin) == null); if (StrncaseCMP (BUF, "exit", 4) == 0) OK = 1; Printf (buf); fflush (stdout);} while (!!);}

Results of the

Telnet Localhost 20001

or

Telnet your_host_name 20001 Trying 127.0.0.1 ... connection to localhost. escape character is '^]'. Welcome to Goodie Service!

Enter "Help"

Help Help

Enter "EXIT"

EXIT EXIT CONNECTION CLOSED by foreIGN Host.

Next, we will design a slightly complex communication agreement, which is more common to general purpose. #include #include #include char * cmds [] = {"Help", "Say", "Hello",

"BYE", "exit", null}; int getcmd (char * cmd) {INT N = 0; while (cmds [n]! = null) {

IF (strncasecmp (cmd, cmds [n], strlen (cmds [n])) == 0) return n; n ;} return -1;} void main (void) {charf [1024];

INT OK; Printf ("Welcome to Goodie Service! / N); fflush (stdout); ok = 0; do {while (fgets (buf, 1023, stdin) == null); Switch (getCmd (BUF)) { Case -1: Printf ("Unknown Command! / N"); Break; Case 0: Printf ("How May I Help You, SIR? / N"); Break; Case 1: Printf ("I Will Say% S" , & buf [3]); Break; Case 2: Printf ("How're you doing today? / n"); Break; Case 3: Printf ("Si Ya, Mate! / N"); OK = 1; Break Case 4: Printf ("Go Ahead! / N"); OK = 1; Break;} fflush (stdout);} while (!!);

} Telnet localhost 20001

or

Telnet your_host_name 20001

Try to enter "Help", "Say", "Hello", "BYE", "EXIT", etc., and others do not

The instructions in the command column.

When designing the inetd service program, pay special attention to the problem of buffer overflow, that is, the following

Status:

Char buffer_overflow [64]; fscanf (stdin, "% s", buffer_overflow);

Almost all security vulnerabilities have come.

You must not use this, no matter whether any reason, the same usage is not available. Cracker can pass through

Your Buffer is burst, then stuffed into his own program coming in.

Ok Station, Webmaster, Brian Lin

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

New Post(0)