network application is an echo system

Fundamentals of Developing Network Applications
CSC8415: Week 2 Practical
1
1 Objective
Understand two types of network communications
Understand the network address schema
Internet protocol and the concept of layering
The client-server model
2 Task List
Find out the difference between two types of network communications
Identify the hardware (MAC) address and internet (IP) address
Draw the TCP/IP protocol stack
Describle the client-server model
3 Practical Questions
3.1 Prepation
An network application usually consists of a server and a client program. To
test the application, an easy way is to run the server and the client on two
shell prompt or xterms.
open two xterm terminals
change to the directory where the server and the client are located.
start the server on one terminal, simple type ./server_name &
on another terminal, type ./client_name port
3.2 ECHO system
The network application is an echo system in which the server send to the
client with a simple message of its own name upon the request of connection.
The application protocol which governs the communication between the
server and the client is known as ECHO.
The server sends a string o the
server as soon as a client nakes a request of connetion.
The implementation is based on the server-client paradigm as shown in
Figure
1.
socket()
socket()
bind()
listen()
accept()
write()
read()
close()
TCP Server
write()
cconnect()
read()
close()
read()
TCP client
connection establishment
end-of-line notification
(TCP 3 way handshake)
well known
port
process request
Figure 1: Server-Client
3.2.1 ECHO server
The format of server response is aimply a string, this string contains the
server’s name and current time. The server can server a single client or multiple clients.
The code for the server is as follows.
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
2
#include <sys/wait.h>

#include <signal.h> if (result < 0){
perror(“server: listen”);
return 0;
}
int main(int argc, char *argv[])
{
struct sockaddr_in sAddr;
int
int
char
listensock;
newsock;
buffer[25];
/* to block waiting for connection requests from clients */
while (1) {
newsock = accept(listensock, NULL, NULL);

 

int nread, result, val; if ((pid = fork()) == 0 ) {
printf(“child process %i created.n”, getpid());
close(listensock);
int pid;

/* create the socket that will accept the incomping connections */

listensock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); buffer[nread] = ’’;
printf(“%sn”, buffer);
send(newsock, buffer, nread, 0);
close(newsock);
/* set SO_REUSERADDR option */
val = 1;

result = setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));

if (result < 0 ) {
perror(“server: setsockopt”);
return 0;
}
exit(0);
}
close(newsock);

/* bind it to a local port and all addresses associated with the machine */
sAddr.sin_family = AF_INET;
sAddr.sin_port = htons(1972);
sAddr.sin_addr.s_addr = INADDR_ANY;
result = bind(listensock, (struct sockaddr *)&sAddr, sizeof(sAddr));

if (result < 0) {
perror(“server: bind”);
return 0;
}
3.2.2 ECHO client
ECHO client doesn’t make any requests. There is no message format for the
requests.
The code for the client is as follows.
/* put the socket into listening mode to listen for incoming connections */
result = listen(listensock, 5); #include <stdio.h>

nread = recv(newsock, buffer, 25, 0);
printf(“child process %i finished.n”, getpid());
} }
The code can be download at ./sources/server.c.
3

#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
void child_func(int childnum)
{
int sock;

struct sockaddr_in sAddr;
char buffer[25];

int result;
int nsend, nread;
memset((void *)&sAddr, 0, sizeof(struct sockaddr_in));
sAddr.sin_family = AF_INET;
sAddr.sin_addr.s_addr = INADDR_ANY;
sAddr.sin_port = 0;
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
bind(sock, (const struct sockaddr *)&sAddr, sizeof(sAddr));
sAddr.sin_addr.s_addr = inet_addr(“127.0.0.1”);
sAddr.sin_port = htons(1972);
result = connect(sock, (const struct sockaddr *)&sAddr, sizeof(sAddr));
if (result != 0) {
perror(“client: connect”);
return;
}
snprintf(buffer, 128, “data from client #%i.”, childnum);
sleep(1);
nsend = send(sock, buffer, strlen(buffer), 0);
if (nsend > 0 )
printf(“child #%i sent %i charsn”, childnum, nsend);
sleep(1);
nread = recv(sock, buffer, 25, 0);
if (nread > 0 )
printf(“child #%i received %i charsn”, childnum, nread);
sleep(1);
close(sock);
}
int main(int argc, char *argv[])
{
int nchildren = 1;
int pid;
int x;
if (argc > 1)
nchildren = atoi(argv[1]);
for (x = 0; x < nchildren; x++) {
if ((pid = fork()) == 0 ) {
child_func(x+1);
exit(0);
} }
wait(NULL);
return 0;
}
The code can be download at ./sources/client.c.
3.2.3 makefile
# Makefile for the echo application
4
SRC=server.c client.c
OBJ=server.o client.o
HDR=
LIB=
all: serevr client
server: server.o
gcc -o server server.o $(LIB)
server.o: server.c $(HDR)
gcc -c server.c
client: client.o
gcc -o client client.o $(LIB)
client.o: client.c $(HDR)
gcc -c client.c
Similarly, the makefile can be downloaded ./sources/makefile.
5