0% found this document useful (0 votes)
35 views

#Include #Include #Include #Include #Include "Helper.h" #Include #Include

This document describes a simple TCP/IP echo server program written in C. The program creates a listening socket bound to port 2002 that waits for client connections. When a client connects, it reads a line of input from the socket and writes it back, effectively echoing anything the client sends. It uses helper functions for reading/writing lines and loops continuously to service multiple clients.

Uploaded by

Nihal Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

#Include #Include #Include #Include #Include "Helper.h" #Include #Include

This document describes a simple TCP/IP echo server program written in C. The program creates a listening socket bound to port 2002 that waits for client connections. When a client connects, it reads a line of input from the socket and writes it back, effectively echoing anything the client sends. It uses helper functions for reading/writing lines and loops continuously to service multiple clients.

Uploaded by

Nihal Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1:

2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:

/*
ECHOSERV.C
==========
(c) Paul Griffiths, 1999
Email: [email protected]
Simple TCP/IP echo server.
*/

#include
#include
#include
#include

<sys/socket.h>
<sys/types.h>
<arpa/inet.h>
<unistd.h>

#include "helper.h"

/*
/*
/*
/*

socket definitions
socket types
inet (3) funtions
misc. UNIX functions

*/
*/
*/
*/

/*

our own helper functions

*/

#include <stdlib.h>
#include <stdio.h>

/*

Global constants

#define ECHO_PORT
#define MAX_LINE

*/
(2002)
(1000)

int main(int argc, char *argv[]) {


int
list_s;
int
conn_s;
short int port;
struct
sockaddr_in servaddr;
char
buffer[MAX_LINE];
char
*endptr;

/*

/*
/*
/*
/*
/*
/*

listening socket
connection socket
port number
socket address structure
character buffer
for strtol()

Get port number from the command line, and


set to default port if no arguments were supplied

*/
*/
*/
*/
*/
*/

*/

if ( argc == 2 ) {
port = strtol(argv[1], &endptr, 0);
if ( *endptr ) {
fprintf(stderr, "ECHOSERV: Invalid port number.\n");
exit(EXIT_FAILURE);
}
}
else if ( argc < 2 ) {
port = ECHO_PORT;
}
else {
fprintf(stderr, "ECHOSERV: Invalid arguments.\n");
exit(EXIT_FAILURE);
}

/*

Create the listening socket

*/

if ( (list_s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {


fprintf(stderr, "ECHOSERV: Error creating listening socket.\n");
exit(EXIT_FAILURE);

63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116: }
117:

/*

Set all bytes in socket address structure to


zero, and fill in the relevant data members

*/

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family
= AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port
= htons(port);

/* Bind our socket addresss to the


listening socket, and call listen()

*/

if ( bind(list_s, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) {


fprintf(stderr, "ECHOSERV: Error calling bind()\n");
exit(EXIT_FAILURE);
}
if ( listen(list_s, LISTENQ) < 0 ) {
fprintf(stderr, "ECHOSERV: Error calling listen()\n");
exit(EXIT_FAILURE);
}

/*

Enter an infinite loop to respond


to client requests and echo input

*/

while ( 1 ) {
/*

Wait for a connection, then accept() it

*/

if ( (conn_s = accept(list_s, NULL, NULL) ) < 0 ) {


fprintf(stderr, "ECHOSERV: Error calling accept()\n");
exit(EXIT_FAILURE);
}

/*

Retrieve an input line from the connected socket


then simply write it back to the same socket.

*/

Readline(conn_s, buffer, MAX_LINE-1);


Writeline(conn_s, buffer, strlen(buffer));

/*

Close the connected socket

*/

if ( close(conn_s) < 0 ) {
fprintf(stderr, "ECHOSERV: Error calling close()\n");
exit(EXIT_FAILURE);
}
}

You might also like