|
/* --------------------------- 1. Create a TCP socket --------------------------- */ s1 = socket(AF_INET, SOCK_STREAM, 0); .... /* ------------------------------------------------------------ 2. Bind socekt to a IP network address (IP addr, TCP port#) so that the socket can be identified ----------------------------------------------------------- */ if ( bind(s1, (struct sockaddr *) &server, sizeof(server) ) < 0 ) { perror("bind:"); exit(1); } /* ------------------------------------------------------------- 3. Change socket into a listen socket A listen socket run the TCP connection establishment protocol that starts a "sliding window session" ------------------------------------------------------------- */ listen(s1, 5); /* ==================================================== We want to keep the server running forever... That's why we use a while-loop... ==================================================== */ while (1) { len = sizeof(from); /* --------------------------------------------------------- 4. Make the server program wait for a connection request from a client --------------------------------------------------------- */ if ( (s2 = accept(s1, (struct sockaddr *) &from, &len)) < 0 ) { perror("accept: "); exit(1); } // A serial server will continue execution here without creating a new process /* ---------------------------------------------- Server program use socket s2 to commninicate with the client ---------------------------------------------- */ /* --------------------------- ---+ Processing loop | --------------------------- */ | while ( read( s2, buf, .... ) > 0 ) | Change this loop { | for different kinds process data in buf | of services write(s2, reply, ...); // Send reply back | } ---+ close(s2); // Close socket when done // We NOW loop back up and // accept the next connection request } |
/* ------------------------------- Processing loop ------------------------------- */ while (read(s2, &c, 1) > 0) // Receive 1 character { putchar(c); // Print it fflush(stdout); } close(s2); |
main(int argc, char *argv[]) { int s1; /* s1 = listen socket */ int s2; /* s2 = connect socket */ struct sockaddr_in me; /* My own Internet address */ struct sockaddr_in myname; /* Used to retrieve my TCP port */ int myname_len; struct sockaddr_in from; /* Internet address from client */ int len; /* length associated with `from' */ char c; /* Used to get data from connection */ if (argc == 1) { printf("Usage: %s port\n", argv[0]); exit(1); } /* ----------------------------------------------- Create a TCP socket ------------------------------------------------ */ s1 = socket(AF_INET, SOCK_STREAM, 0); /* ------------------------------------------------------------ Set up Internet address (IPaddr, Port#) to bind the socket ------------------------------------------------------------ */ me.sin_family = AF_INET; /* Put in the family */ me.sin_port = htons(atoi(argv[1])); /* Put in the port number */ me.sin_addr.s_addr = htonl(INADDR_ANY); /* Use wildcard address */ /* ------------------------------------------------- Make TCP socket "identifiable" ------------------------------------------------- */ if ( bind(s1, (struct sockaddr *) &me, sizeof(me) ) < 0) { perror("bind:"); exit(1); } /* ======================================== Change s1 into a "listen" socket (with max 5 pending connection reqs) ======================================== */ listen(s1, 5); /* --------------------------------------- Keep the server running forever.... --------------------------------------- */ while (1) { len = sizeof(from); /* ==================================================== Make the server program wait for a connection from a client.... ===================================================== */ if ( (s2 = accept(s1, (struct sockaddr *) &from, &len)) < 0) { perror("accept: "); exit(1); } /* ========================================== Use socket s2 to get data from client and print it ========================================== */ while (read( s2 , &c, 1) > 0) { putchar(c); // Print each character read fflush(stdout); } close( s2 ); printf("\n\n...Done...\n\n"); } } |
/* ========================================================== The socket creation and connection to server have been omitted for brevity ========================================================== */ char c; while ((c = getchar()) != EOF ) // Read a character and store in c { write(s, &c, 1); // Send the character to server } |
How to run the program:
|
/* ================================================== This is the processing loop of the server ================================================== */ int x, y, z; while ( 1 ) { int x1, y1, z1; if ( read( s2, &x1, 4) <= 0 ) // An int is 4 bytes break; x = ntohl(x1); // Convert to host order if ( read( s2, &y1, 4) <= 0 ) break; y = ntohl(y1); // Convert to host order z = x + y; printf("Client data: %d, %d, send sum = %d\n", x, y, z); z1 = htonl(z); write ( s2, &z1, 4 ); } close(s2); |
/* ========================================================== The socket creation and connection to server have been omitted for brevity ========================================================== */ int x, y, z; while ( 1 ) { printf("Enter first number (0 to exit): "); scanf("%d", &x); // Read first integer if ( x == 0 ) { close(s); exit(0); } printf("Enter second number: "); scanf("%d", &y); // Read second integer int x1, y1, z1; x1 = htonl(x); // Convert them to network byte order first y1 = htonl(y); write(s, &x1, 4); // Send the integers write(s, &y1, 4); /* =================================== Receive the answer from the server =================================== */ read(s, &z1, 4); z = htonl(z1); // Convert answer to host byte order printf("%d + %d = %d\n", x, y, z); // Print it } |
How to run the program:
|