0% found this document useful (0 votes)
86 views5 pages

CN Labsheet 5

This document describes setting up an HTTP proxy using Tinyproxy on Ubuntu and testing it using Wireshark. It also describes implementing a basic client-server chat application using TCP and UDP sockets in C. For the chat application, the server listens on a well-known port for connections from clients. Clients can send messages independently to the server, and the server responds with "OK" for all messages except "Bye", for which it responds with "Goodbye". Sample server code is provided to write the client code. Designing a simple chat application using this client-server model where the server listens for an initial "Hello" message is also described.

Uploaded by

Vidit jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views5 pages

CN Labsheet 5

This document describes setting up an HTTP proxy using Tinyproxy on Ubuntu and testing it using Wireshark. It also describes implementing a basic client-server chat application using TCP and UDP sockets in C. For the chat application, the server listens on a well-known port for connections from clients. Clients can send messages independently to the server, and the server responds with "OK" for all messages except "Bye", for which it responds with "Goodbye". Sample server code is provided to write the client code. Designing a simple chat application using this client-server model where the server listens for an initial "Hello" message is also described.

Uploaded by

Vidit jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

BITS Pilani, Hyderabad Campus

Computer Networks (CS F303)


LabSheet 5 (Week 5)
(HTTP proxy set-up and Socket Programming)

Part-I: HTTP proxy set-up:

1. HTTP proxy set-up (Work in a team of two. First member will install proxy server and other will
connect from a client)
i. Install tinyproxy in Ubuntu system using sudo apt-get update
sudo apt-get install tinyproxy
ii. Open tinyproxy.conf file using vi (In the Terminal: sudo vi /etc/tinyproxy.conf)
iii. Uncomment the line “Allow 172.16.0.0/12” (Refer the figure below). By this, you are allowing
anyone on 172.16.x.x to connect to your machine. Alternatively, you may restrict access to one
or few machines. To allow access to only your client, type the client machine’s IP address (add
the following entry in the file: Allow <<IP of client>>). If you choose to do this, DO NOT
uncomment the entry which was specified above.
iv. Save the file and exit.
v. Now, restart Tinyproxy for the changes to take place. In the terminal:
sudo /etc/init.d/tinyproxy stop
sudo /etc/init.d/tinyproxy start

Figure 6. Tinyproxy configuration file

9. Do the following on the other teammate’s PC (i.e., the client):


i. In Firefox, go to Edit → Preferences → Advanced tab →Network tab → Click on Settings
→Check ‘Manual proxy configurations’ →enter IP of proxy server and port (default port is 8888).
Figure 7: Firefox settings

ii. Now, on your client’s Firefox, connect to www.google.com and capture packets on Wireshark.
▪ Firstly, when you are connected to the proxy, and
▪ Secondly, without being connected to the proxy.
What differences do you observe? When not connected to proxy, can you see the connection being
made with www.google.com?

Figure 8. Response while not connected to proxy

iii) When connected to the proxy, you can only see the proxy server!! (In this case, 172.26.90.4)

Figure 9. Response while connected to proxy


iii. After you are done with your experiments, it is recommended to stop tinyproxy because if you're going
to leave Tinyproxy running all the time, it will eventually eat all your memory and lock up your server.
sudo /etc/init.d/tinyproxy stop

iv. Before you leave, please remove tinyproxy from your machine so that the next lab’s students can have
the opportunity to configure it themselves. sudo apt-get remove tinyproxy

Part II: Socket Programming:

In this labsheet, we will build a simple client-server system, where you use the client to chat with a
dummy server. The protocol between the client and server is as follows.
 The server is first started on a known port.
 The client program is started (server IP and port are provided on the commandline).
 The client connects to the server, and then asks the user for input. The user types his message on
the terminal (e.g., "Hi", "Bye", "How are you"). The user's input is sent to the server via the
connected socket.
 The server reads the user's input from the client socket. If the user has typed "Bye" (without the
quotes), the server must reply with "Goodbye". For any other message, the server must reply with
"OK".
 The client then reads the reply from the server, and checks that it is accurate (either "OK" or
"Goodbye").
 If the user had typed "Bye", and the server replied with a "Goodbye" correctly, the client quits.
Otherwise, the client asks the user for the next message to send to the server.
Do the above for: i) Both TCP and UDP sockets ii) Both localhost and anyother host (for e.g. using your
neighbor’s IP address).
1) Find out the different classes of address families that exist.
2) What can be different values of the field protocol? In what scenarios will these different protocol
values be applicable?
ChatApp:

A simple client server program is blocking. That is the end systems cannot communicate independent of
each other. To do this we need to create separate thread for read and write, wherein one of the thread will
handle the write process and the other (the main process) will handle the read process or vice-versa. Sample
server program is given. Use this code to write the client code.

#include"stdio.h"
#include"stdlib.h"
#include"sys/types.h"
#include"sys/socket.h"
#include"string.h"
#include"netinet/in.h"
#include"pthread.h"

#define PORT 4444


#define BUF_SIZE 2000
#define CLADDR_LEN 100
void * receiveMessage(void * socket) {
int sockfd, ret;
char buffer[BUF_SIZE];
sockfd = (int) socket;
memset(buffer, 0, BUF_SIZE);
for (;;) {
ret = recvfrom(sockfd, buffer, BUF_SIZE, 0, NULL, NULL);
if (ret < 0) {
printf("Error receiving data!\n");
} else {
printf("client: ");
fputs(buffer, stdout);
//printf("\n");
}
}
}

void main() {
struct sockaddr_in addr, cl_addr;
int sockfd, len, ret, newsockfd;
char buffer[BUF_SIZE];
pid_t childpid;
char clientAddr[CLADDR_LEN];
pthread_t rThread;

sockfd = socket(AF_INET, SOCK_STREAM, 0);


if (sockfd < 0) {
printf("Error creating socket!\n");
exit(1);
}
printf("Socket created...\n");

memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = PORT;

ret = bind(sockfd, (struct sockaddr *) &addr, sizeof(addr));


if (ret < 0) {
printf("Error binding!\n");
exit(1);
}
printf("Binding done...\n");

printf("Waiting for a connection...\n");


listen(sockfd, 5);

len = sizeof(cl_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cl_addr, &len);
if (newsockfd < 0) {
printf("Error accepting connection!\n");
exit(1);
}

inet_ntop(AF_INET, &(cl_addr.sin_addr), clientAddr, CLADDR_LEN);


printf("Connection accepted from %s...\n", clientAddr);
memset(buffer, 0, BUF_SIZE);
printf("Enter your messages one by one and press return key!\n");

//creating a new thread for receiving messages from the client


ret = pthread_create(&rThread, NULL, receiveMessage, (void *) newsockfd);
if (ret) {
printf("ERROR: Return Code from pthread_create() is %d\n", ret);
exit(1);
}

while (fgets(buffer, BUF_SIZE, stdin) != NULL) {


ret = sendto(newsockfd, buffer, BUF_SIZE, 0, (struct sockaddr *) &cl_addr,
len);
if (ret < 0) {
printf("Error sending data!\n");
exit(1);
}
}

close(newsockfd);
close(sockfd);

pthread_exit(NULL);
return;
}

Design a simple chat-based application using client server socket programming using C.
1. The server listens to well-known ports from the client for a chat request.
2. The client must send the first message as “Hello” to start the chat communication.
3. In this any or both the parties should be able to send the message
independent of each other.

You might also like