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

CN Assignment5 Siddharth

The document describes code for implementing client-server communication using TCP and UDP sockets in C. It includes code for both the server and client sides of a TCP-based chat application, as well as code to send UDP datagrams and determine the maximum transmission unit size.

Uploaded by

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

CN Assignment5 Siddharth

The document describes code for implementing client-server communication using TCP and UDP sockets in C. It includes code for both the server and client sides of a TCP-based chat application, as well as code to send UDP datagrams and determine the maximum transmission unit size.

Uploaded by

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

ASSIGNMENT-5

Siddharth Swami Warke


0801CS201089
ľCP Client-Seíveí Implementation in C

Seíveí-Side Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

int main(){

char *ip = "127.0.0.1";


int port = 5566;

int server_sock, client_sock;


struct sockaddr_in server_addr, client_addr;
socklen_t addr_size;
char buffer[1024];
int n;

server_sock = socket(AF_INET, SOCK_STREAM, 0);


if (server_sock < 0){
perror("[-]Socket error");
exit(1);
}
printf("[+]TCP server socket created.\n");

memset(&server_addr, '\0', sizeof(server_addr));


server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);

n = bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr));


if (n < 0){
perror("[-]Bind error");
exit(1);
}
printf("[+]Bind to the port number: %d\n", port);

listen(server_sock, 5);
printf("Listening...\n");
while(1){
addr_size = sizeof(client_addr);
client_sock = accept(server_sock, (struct sockaddr*)&client_addr,
&addr_size);
printf("[+]Client connected.\n");

bzero(buffer, 1024);
recv(client_sock, buffer, sizeof(buffer), 0);
printf("Client: %s\n", buffer);

bzero(buffer, 1024);
strcpy(buffer, "HI, THIS IS SERVER. HAVE A NICE DAY!!!");
printf("Server: %s\n", buffer);
send(client_sock, buffer, strlen(buffer), 0);

close(client_sock);
printf("[+]Client disconnected.\n\n");

}
return 0;
}
Client-Side Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

int main(){

char *ip = "127.0.0.1";


int port = 5566;

int sock;
struct sockaddr_in addr;
socklen_t addr_size;
char buffer[1024];
int n;

sock = socket(AF_INET, SOCK_STREAM, 0);


if (sock < 0){
perror("[-]Socket error");
exit(1);
}
printf("[+]TCP server socket created.\n");

memset(&addr, '\0', sizeof(addr));


addr.sin_family = AF_INET;
addr.sin_port = port;
addr.sin_addr.s_addr = inet_addr(ip);

connect(sock, (struct sockaddr*)&addr, sizeof(addr));


printf("Connected to the server.\n");

bzero(buffer, 1024);
strcpy(buffer, "HELLO, THIS IS CLIENT.");
printf("Client: %s\n", buffer);
send(sock, buffer, strlen(buffer), 0);

bzero(buffer, 1024);
recv(sock, buffer, sizeof(buffer), 0);
printf("Server: %s\n", buffer);

close(sock);
printf("Disconnected from the server.\n");

return 0;
}
UDP fíagments

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define DGRAM_SIZE 2020

int main()
{
int sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if ( sockfd == -1 ) { perror("Socket"); exit(0); }

int optval = IP_PMTUDISC_DO;


if (setsockopt(sockfd, IPPROTO_IP, IP_MTU_DISCOVER, &optval,
sizeof(optval)) == -1) { perror("MTU Discover"); exit(0); }

void *buf = malloc(DGRAM_SIZE);


memset(buf, 0x1f, DGRAM_SIZE);

struct sockaddr_in svraddr;

svraddr.sin_family = PF_INET;
svraddr.sin_port = htons(69);
svraddr.sin_addr.s_addr = inet_addr("192.168.1.107");

if ( connect(sockfd, (struct sockaddr*)&svraddr, sizeof(svraddr)) == -1


) { perror("connect"); exit(0); }

int dgram_size, dg_len = sizeof(dgram_size);


/*
if ( getsockopt(sockfd, IPPROTO_IP, IP_MTU, &dgram_size,
(socklen_t*)&dg_len) == -1 ) {
perror("getsockopt"); exit(0);
}

printf("Dimensione max fragment ( prima dell'invio ): %d\n",


dgram_size);
*/
if (sendto(sockfd, buf, DGRAM_SIZE, 0, (struct sockaddr*)&svraddr,
sizeof(svraddr)) == -1 ) {
dgram_size = 0;

if ( getsockopt(sockfd, IPPROTO_IP, IP_MTU, &dgram_size,


(socklen_t*)&dg_len) == -1 ) {
perror("getsockopt"); exit(0);
}

perror("Sendto");
printf("Dimensione max fragment ( dopo l'invio dei dati ): %d\n",
dgram_size);
} else printf("Successo\n");

return 0;
}

You might also like