Simple server and customer program

zhaozj2021-02-16  69

The simple server is made by this server to send a string "Hello, World! / N" on a streaming connection. If you want to test this program, you can run the program on a machine and then log in on another machine:

$ telnet remotehostname 3490

RemotehostName is the name of the machine running.

Server code:

#include

#include

#include

#include

#include

#include

#include

#include

#define myport 3490 / * Defines the user connection port * /

#define backlog 10 / * How much waiting for connection control * /

Main ()

{

INT SOCKFD, New_FD; / * Listen on Sock_fd, New Connection On New_FD

* /

Struct SockAddr_in my_addr; / * my address information * /

Struct SockAddr_in their_addr; / * connector's address information * /

INT SIN_SIZE;

IF ((SockFD = Socket (AF_INET, SOCK_STREAM, 0) == -1) {Perror ("socket"); exit (1);}

my_addr.sin_family = AF_INET; / * host byte order * / my_addr.sin_port = htons (MYPORT); / * short, network byte order * / my_addr.sin_addr.s_addr = INADDR_ANY; / * auto-fill with my IP * / bzero (& (my_addr.sin_zero) ,; / * zero the rest of the struct * /

IF (Bind (Sockfd, (Struct SockAddr *) & my_addr, sizeof (struct sockaddr)) == -1) {Perror ("bind"); exit (1);} if (listen (sockfd, backlog) == -1 ) {PERROR ("listen"); exit (1);}

While (1) {/ * main accept () loop * / sin_size = sizeof (struct sockaddr_in); if ((new_fd = accept (sockfd, (strunt socketdr *)) == -1) {PERROR "accept"); Continue;} Printf ("Server: Got Connection from% S / N", / inet_ntoa (their_addr.sin_addr)); if (! fork ()) {/ * this is the child process * / if ( Send (new_fd, "hello, world! / n", 14, 0) == -1) PERROR ("send"); close (new_fd); exit (0);} close (new_fd); / * Parent doesn ' T NEED THIS * / WHILE (Waitpid (-1, null, wnohang> 0); / * Clean up child processes * /}} If you are very picky, you must not satisfy all my code is in a big main. () In the function. If you don't like it, you can divide more points. You can also use the programs in our next chapter to get the string sent by the server. Simple customer program This program is simpler than the server. All of this program is connected to the host specified in the command line through the 3490 port, and then get the string sent by the server. Customer code: #include

#include

#include

#include

#include

#include

#include

#include

#define port 3490 / * Client connection remote host port * /

#define maxDataSize 100 / * Maximum bytes you can receive each time * /

Int main (int Argc, char * argv [])

{

Int Sockfd, Numbytes;

Char buf [maxdatasize];

Struct hostent * he;

Struct SockAddr_in their_addr; / * connector's address information * /

IF (Argc! = 2) {fprintf (stderr, "usage: client hostname / n"); exit (1);} if ((he = gethostbyname) == null) {/ * get the Host Info * / Herror ("gethostByName"); exit (1);

IF ((SockFD = Socket (AF_INET, SOCK_STREAM, 0) == -1) {Perror ("socket"); exit (1);}

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

New Post(0)