|
|
|
|
/*
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
int s; /* connected socket descriptor */
struct hostent *hp; /* pointer to host info for remote
host */
struct servent *sp; /* pointer to service information */
long timevar; /* contains time returned by time() */
char *ctime(); /* declare time formatting routine */
struct sockaddr_in myaddr_in; /* for local socket address
*/
struct sockaddr_in peeraddr_in; /* for peer socket address */
/*
main(argc, argv)
int argc;
char *argv[];
{
int addrlen, i, j;
/* This example uses 10 byte messages. */
char buf[10];
if (argc != 2) {
fprintf(stderr, "Usage: %s <remote host>\n", argv[0]);
exit(1);
}
/* clear out address structures */
memset ((char *)&myaddr_in, 0, sizeof(struct sockaddr_in));
memset ((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in));
/* Set up the peer address to which we will connect.
*/
peeraddr_in.sin_family = AF_INET;
/* Get the host information for the hostname that the
hp = gethostbyname (argv[1]);
if (hp == NULL) {
fprintf(stderr, "%s: %s not found in /etc/hosts\n",
argv[0], argv[1]);
exit(1);
}
peeraddr_in.sin_addr.s_addr
= ((struct in_addr *)(hp->h_addr))->s_addr;
/* Find the information for the "example" server
sp = getservbyname ("example", "tcp");
if (sp == NULL) {
fprintf(stderr, "%s: example not found in /etc/services\n",
argv[0]);
exit(1);
}
peeraddr_in.sin_port = sp->s_port;
/* Create the socket. */
s = socket (AF_INET, SOCK_STREAM, 0);
if (s == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to create socket\n", argv[0]);
exit(1);
}
/* Try to connect to the remote server at the address
if (connect(s, &peeraddr_in, sizeof(struct sockaddr_in))
== -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to connect to remote\n", argv[0]);
exit(1);
}
/* Since the connect call assigns a random address
addrlen = sizeof(struct sockaddr_in);
if (getsockname(s, &myaddr_in, &addrlen) == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to read socket address\n", argv[0]);
exit(1);
}
/* Print out a startup message for the user. */
time(&timevar);
/* The port number must be converted first to host byte
printf("Connected to %s on port %u at %s",
argv[1], ntohs(myaddr_in.sin_port), ctime(&timevar));
/* This sleep simulates any preliminary processing
sleep(5);
/* Sent out all the requests to the remote server.
/* CAUTION:
for (i=1; i<=5; i++) {
if (send(s, buf, 10, 0) != 10) {
fprintf(stderr, "%s: Connection aborted on error ",
argv[0]);
fprintf(stderr, "on send number %d\n", i);
exit(1);
}
}
/* Now, shutdown the connection for further sends.
if (shutdown(s, 1) == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to shutdown socket\n", argv[0]);
exit(1);
}
/* Now, start receiving all of the replys from the
server.
while (i = recv(s, buf, 10, 0)) {
if (i == -1) {
errout: perror(argv[0]);
fprintf(stderr, "%s: error reading result\n", argv[0]);
exit(1);
}
/* The reason this while loop exists is that there
while (i < 10) {
j = recv(s, &buf[i], 10-i, 0);
if (j == -1) goto errout;
i += j;
}
/* Print out message indicating the identity of
printf("Received result number %d\n", *buf);
}
/* Print message indicating completion of task. */
time(&timevar);
printf("All done at %s", ctime(&timevar));
}
/*
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <netdb.h>
int s; /* connected socket descriptor */
int ls; /* listen socket descriptor */
struct hostent *hp; /* pointer to host info for remote
host */
struct servent *sp; /* pointer to service information */
long timevar; /* contains time returned by time() */
char *ctime(); /* declare time formatting routine */
struct linger linger; /* allow a lingering, graceful
close; */
/* used when setting SO_LINGER */
struct sockaddr_in myaddr_in; /* for local socket address
*/
struct sockaddr_in peeraddr_in; /* for peer socket address */
/*
main(argc, argv)
int argc;
char *argv[];
{
int addrlen;
/* clear out address structures */
memset ((char *)&myaddr_in, 0, sizeof(struct sockaddr_in));
memset ((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in));
/* Set up address structure for the listen socket.
*/
myaddr_in.sin_family = AF_INET;
/* The server should listen on the wildcard address,
myaddr_in.sin_addr.s_addr = INADDR_ANY;
/* Find the information for the "example" server
sp = getservbyname ("example", "tcp");
if (sp == NULL) {
fprintf(stderr, "%s: example not found in /etc/services\n",
argv[0]);
exit(1);
}
myaddr_in.sin_port = sp->s_port;
/* Create the listen socket. */
ls = socket (AF_INET, SOCK_STREAM, 0);
if (ls == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to create socket\n", argv[0]);
exit(1);
}
/* Bind the listen address to the socket. */
if (bind(ls, &myaddr_in, sizeof(struct sockaddr_in)) == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to bind address\n", argv[0]);
exit(1);
}
/* Initiate the listen on the socket so remote users
if (listen(ls, 5) == -1) {
perror(argv[0]);
fprintf(stderr, "%s: unable to listen on socket\n", argv[0]);
exit(1);
}
/* Now, all the initialization of the server is
setpgrp();
switch (fork()) {
case -1: /* Unable to fork, for some reason. */
perror(argv[0]);
fprintf(stderr, "%s: unable to fork daemon\n", argv[0]);
exit(1);
case 0: /* The child process (daemon) comes here. */
/* Close stdin and stderr so that they will not
close(stdin);
close(stderr);
/* Set SIGCLD to SIG_IGN, in order to prevent
signal(SIGCLD, SIG_IGN);
for(;;) {
/* Note that addrlen is passed as a pointer
addrlen = sizeof(struct sockaddr_in);
/* This call will block until a new
s = accept(ls, &peeraddr_in, &addrlen);
if ( s == -1) exit(1);
switch (fork()) {
case -1: /* Can't fork, just exit. */
exit(1);
case 0: /* Child process comes here. */
server();
exit(0);
default: /* Daemon process comes here. */
/* The daemon needs to remember
close(s);
}
}
default: /* Parent process comes here. */
exit(0);
}
}
/*
server()
{
int reqcnt = 0; /* keeps count of number of requests */
char buf[10]; /* This example uses 10 byte messages. */
char *inet_ntoa();
char *hostname; /* points to the remote host's name string */
int len, len1;
/* Close the listen socket inherited from the daemon.
*/
close(ls);
/* Look up the host information for the remote host
hp = gethostbyaddr ((char *) &peeraddr_in.sin_addr,
sizeof (struct in_addr),
peeraddr_in.sin_family);
if (hp == NULL) {
/* The information is unavailable for the remote
hostname = inet_ntoa(peeraddr_in.sin_addr);
} else {
hostname = hp->h_name; /* point to host's name */
}
/* Log a startup message. */
time (&timevar);
/* The port number must be converted first to host byte
printf("Startup from %s port %u at %s",
hostname, ntohs(peeraddr_in.sin_port), ctime(&timevar));
/* Set the socket for a lingering, graceful close.
linger.l_onoff =1;
linger.l_linger =1;
if (setsockopt(s, SOL_SOCKET, SO_LINGE, &linger,
sizeof(linger)) == -1) {
errout:printf("Connection with %s aborted on error\n", hostname);
exit(1);
}
/* Go into a loop, receiving requests from the remote
while (len = recv(s, buf, 10, 0)) {
if (len == -1) goto errout; /* error from recv */
/* The reason this while loop exists is that there
while (len < 10) {
len1 = recv(s, &buf[len], 10-len, 0);
if (len1 == -1) goto errout;
len += len1;
}
/* Increment the request count. */
reqcnt++;
/* This sleep simulates the processing of the
sleep(1);
/* Send a response back to the client. */
if (send(s, buf, 10, 0) != 10) goto errout;
}
/* The loop has terminated, because there are no
close(s);
/* Log a finishing message. */
time (&timevar);
/* The port number must be converted first to host byte
printf("Completed %s port %u, %d requests, at
%s\n",
hostname, ntohs(peeraddr_in.sin_port), reqcnt, ctime(&timevar));
}
|
|
|
|