0% found this document useful (0 votes)
78 views16 pages

Network Lab

The document contains code for implementing socket programming experiments in C including: 1) Client-server communication using UDP where the client sends a message to the server which converts it to uppercase and sends it back. 2) A chat application using TCP where the client and server can exchange messages. 3) A security protocol where the client sends a file to the server which encrypts it using a key and sends it back, and the client can decrypt it.

Uploaded by

abhijeet
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)
78 views16 pages

Network Lab

The document contains code for implementing socket programming experiments in C including: 1) Client-server communication using UDP where the client sends a message to the server which converts it to uppercase and sends it back. 2) A chat application using TCP where the client and server can exchange messages. 3) A security protocol where the client sends a file to the server which encrypts it using a key and sends it back, and the client can decrypt it.

Uploaded by

abhijeet
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/ 16

2019

ABHIJEET NAWALE
16BCE2126

CSE1004:L31+32

NETWORK AND COMMUNICATION

EXPERIMENT NO-6
AIM: Implement the following socket programming

(a) Client -Server Communication using UDP

CODE:

Client.c
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(){
int clientSocket, portNum, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr;
socklen_t addr_size;

/*Create UDP socket*/


clientSocket = socket(PF_INET, SOCK_DGRAM, 0);

/*Configure settings in address struct*/


serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

/*Initialize size variable to be used later on*/


addr_size = sizeof serverAddr;

while(1){
printf("Type a sentence to send to server:\n");
fgets(buffer,1024,stdin);
printf("You typed: %s",buffer);

nBytes = strlen(buffer) + 1;

/*Send message to server*/


sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&serverAddr,addr_size);

/*Receive message from server*/


nBytes = recvfrom(clientSocket,buffer,1024,0,NULL, NULL);

printf("Received from server: %s\n",buffer);

return 0;
}
Server.c

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>

int main(){
int udpSocket, nBytes;
char buffer[1024];
struct sockaddr_in serverAddr, clientAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size, client_addr_size;
int i;

/*Create UDP socket*/


udpSocket = socket(PF_INET, SOCK_DGRAM, 0);

/*Configure settings in address struct*/


serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(7891);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

/*Bind socket with address struct*/


bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

/*Initialize size variable to be used later on*/


addr_size = sizeof serverStorage;

while(1){
/* Try to receive any incoming UDP datagram. Address and port of
requesting client will be stored on serverStorage variable */
nBytes = recvfrom(udpSocket,buffer,1024,0,(struct sockaddr *)&serverStorage,
&addr_size);

/*Convert message received to uppercase*/


for(i=0;i<nBytes-1;i++)
buffer[i] = toupper(buffer[i]);

/*Send uppercase message back to client, using serverStorage as the address*/


sendto(udpSocket,buffer,nBytes,0,(struct sockaddr *)&serverStorage,addr_size);
}
return 0;
}
OUTPUT:
(b) Chat Application using TCP

CODE:

Client.c:

#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
void func(int sockfd)
{ char buff[MAX]; int n;
for (;;) {
bzero(buff, sizeof(buff)); printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n') ;
write(sockfd, buff, sizeof(buff)); bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff)); printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n"); break;
}}} int main()
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0); }
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
// connect the client socket to server socket
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
printf("connection with the server failed...\n");
exit(0); }
else
printf("connected to the server..\n");
// function for chat
func(sockfd);
// close the socket

close(sockfd); }

Server.c:

#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAX 80
#define PORT 8080
#define SA struct sockaddr
// Function designed for chat between client and server.
void func(int sockfd)
{
char buff[MAX];
int n;
// infinite loop for chat
for (;;) {
bzero(buff, MAX);
// read the message from client and copy it in buffer
read(sockfd, buff, sizeof(buff));
// print buffer which contains the client contents
printf("From client: %s\t To client : ", buff); bzero(buff, MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n')
;
// and send that buffer to client
write(sockfd, buff, sizeof(buff));
// if msg contains "Exit" then server exit and chat ended.
if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break; }
}}
// Driver function
int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0); }
else
printf("Socket successfully created..\n");

bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
printf("socket bind failed...\n");
exit(0); }
else
printf("Socket successfully binded..\n");
// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0); }
else
printf("Server listening..\n");
len = sizeof(cli);
// Accept the data packet from client and verification
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd < 0) {

printf("server acccept failed...\n");


exit(0); }
else
printf("server acccept the client...\n");
// Function for chatting between client and server
func(connfd);
// After chatting close the socket
close(sockfd); }
OUTPUT:
(c) Security Protocol

Code:

Client.c:

#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
// Driver code
int main()
{
int clientSocket, portNum, nBytes;
char file_buffer[3000], path[1024], buffer[3000];
// This key array stores the hidden key
char const key[3000] = "HIDDENKEY";
struct sockaddr_in serverAddr;
socklen_t addr_size;
int i;
clientSocket = socket(PF_INET, SOCK_DGRAM, 0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(5004);
serverAddr.sin_addr.s_addr = inet_addr("127.12.0.51");
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
addr_size = sizeof serverAddr;
while (1) {
printf("Specify file name: \n"); gets(path);
//
printf("%s\n", path);
FILE* fp;
fp = fopen(path, "r");
if (fp == NULL) {
printf("file does not exist\n"); }
fseek(fp, 0, SEEK_END);

size_t file_size = ftell(fp); fseek(fp, 0, SEEK_SET);


if (fread(file_buffer, file_size, 1, fp) <= 0)
{ printf("unable to copy file into buffer\n"); exit(1);
}
if (sendto(clientSocket, file_buffer, 3000, 0, (struct sockaddr*)&serverAddr,
addr_size) < 0) { printf("error in sending the file\n");
exit(1); }
bzero(file_buffer, sizeof(file_buffer));
nBytes = recvfrom(clientSocket, buffer, 1024, 0, NULL, NULL); printf("Received from server:
\n");
// printing some of the character to have a feel of encryption
for (i = 0; i < 15; ++i)
printf("%02X ", buffer[i]); printf("\n");
char x[3000];

for (i = 0; i < nBytes - 1; ++i)


x[i] = (char)(buffer[i] ^ key[i]);
// printing some of the character to have a feel of decryption
printf("Decrypted message: (First 15 characters)\n");
for (i = 0; i < 11; ++i)
printf("%c ", x[i]);
printf("\n"); }
return 0; }
Server.c:
#include <memory.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
Driver code
int main()
{
int udpSocket, nBytes;
char buffer[3000], xor[3000];
char const key[1024] = "HIDDENKEY";
struct sockaddr_in serverAddr, clientAddr;
struct sockaddr_storage serverStorage; socklen_t addr_size, client_addr_size;
int i;
udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(5004);
serverAddr.sin_addr.s_addr = inet_addr("127.12.0.51"); memset(serverAddr.sin_zero, '\0',
sizeof serverAddr.sin_zero);
bind(udpSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
addr_size = sizeof serverStorage;
puts("Waiting for client :");
int count = 0;
while (1) {
nBytes = recvfrom(udpSocket, buffer, 3000, 0, (struct sockaddr*)&serverStorage,
&addr_size); printf("Message no : %d\n", ++count);
for (i = 0; i < nBytes - 1; i++)
{
if (buffer[i] != '\n')

xor[i] = (char)(buffer[i] ^ key[i]);


else

xor[i] = buffer[i];
}
printf("Encrypted message stored in file : (First 15 characters)\n");
printing some of the character to have a feel of encryption
for (i = 0; i < 15; ++i)
printf("%02X ", xor[i]); printf("\n");
FILE* fp;
fp = fopen("temp.txt", "w+");
for (i = 0; i < nBytes - 1; i++) { if (xor[i] != '\n')
fprintf(fp, "%X", xor[i]); else
fprintf(fp, "%c", xor[i]);
}
fclose(fp);
sendto(udpSocket, xor, nBytes, 0, (struct sockaddr*)&serverStorage,
addr_size);

return 0; }
}
Output:

You might also like