The fire died, help me.

Category: Linux/Unix Community -> Applications Author: deogratias Date: 2001-07-10 01:51:15
 
deogratias
2001-07-10 01:51:15
int InitPassiveSock (char * servicename)
/ * Initialize Passive Socket. If succeed then return 1, else return error code ( <0) * /
/ * This function is used to require passive socket server program, which in addition to obtaining service information, but also to establish a passive
sockets. * /
{
Int mainsock, flag = 1;
struct ; servent * servrec;
struct sockaddr_in serv_addr;
if ((servrec = getservbyname (servicename, "tcp")) == NULL) {
return (-1);
} ;
bzero ((char *) & Mysock, sizeof (Sockets));
Mysock.Port = servrec-> s_port; / * Service Port in Network Byte Order * /
if ((mainsock = socket (AF_INET, SOCK_STREAM, 0)) <0) {
; return (-2);
}
bzero ((char *) & serv_addr, sizeof (serv_addr));
serv_addr.sin_family ; = AF_INET;
serv_addr.sin_addr.s_addr = htonl (INADDR_ANY); / * any network interface * /
serv_addr.sin_port = servrec-> s_port;
if (bind (mainsock, (struct sockaddr *) & serv_addr, sizeof (serv_addr)) <0) {
; close (mainsock);
return (-3 );
}
if (listen (mainsock, 5) == -1) {/ * Set the active socket into a passive socket is ready to accept connection * /
close (mainsock);
; return (-4);
}
/ * Set this socket as a Non-blocking socket. * /
if (ioctl (mainsock, FIONBIO , & flag) == -1) {
; close (mainsock);
return (-5);
}
Mysock.DaemonSock = mainsock;
FD_SET (mainsock, & Mysock.readfds); / * declare the main socket " readable "interesting * /
FD_SET (mainsock, & Mysock.exceptfds); / * declare an exception to the main event on a socket interested in * /
return (1);
}

/ * File Name: server.c * /

/ * ; This is a very simple repetition server program, it initializes a good passive socket, the loop waiting to receive connections. If the connection is received, it shows the data socket number and the client's IP address; if the data with the arrival of data on the socket, which receives the data and display the number of the connection socket and the received data string. * /

# Include "tcpsock.h"

main (argc, argv)

int argc;

char ** argv;

{

struct in_addr sin_addr;

int retcode, i;

char buf [ 32];
/ * For the server program, it is often in an infinite loop state, only the user is actively kill the process or system shutdown when it until the end. For use kill kill server program, as the main socket is not closed, resources are not active release, it might give the subsequent impact on the server restarts. Therefore, the initiative is a good off the main socket becomes habit. The following statement causes the program receives a SIGINT, SIGQUIT and SIGTERM signal when the first such execution CloseMainSock () function closes the main socket, and then end the program. Therefore, when forced to terminate the server process using the kill, you should first use kill -2 PID program a message to the server to make it off the main socket, and then use kill -9 PID forced to end the process. * /

(Void) signal (SIGINT, CloseMainSock);

(void) signal (SIGQUIT, CloseMainSock);

(void) signal (SIGTERM , CloseMainSock);

if ((retcode = ; InitPassiveSock ("tcp")) <0) {

printf ("InitPassiveSock: error code =% d \ n", retcode);

exit (-1);

}

while (1) {

; retcode = QuerySocketsMsg (); / * Query the network message * /

; if (SockMsg.AcceptNum == 1) {/ * have to wait to receive incoming connections? * /

retcode = AcceptConnection (& sin_addr);

printf ("retcode =% d, IP =% s \ n" ;, retcode, inet_ntoa (sin_addr));

}

; else if (SockMsg.AcceptNum == -1) / * main socket error? * /

printf ("Daemon Sockets error. \ n");

; for (i = 0; i <SockMsg.ReadNum; i + +) { ; / * Receive incoming data * /

if ((retcode = RecvPacket (SockMsg.ReadQueue [i], buf, 32)) ;> 0)

; printf ("sockno% d Recv string =% s \ n ", SockMsg.ReadQueue [i], buf);
else / * return data length of zero, indicating that connection is interrupted, the socket is closed . * /

CloseConnection (SockMsg.ReadQueue [i]);

}

;} / * end while * /

}

InitPassiveSock ("tcp" ;) I should fill in what? ? ?