Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
12 kB
3
Indexable
Never
//client:chat application
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<string.h>
#include<stdio.h>

int main(){

	int csd,csd2,cport=6000,clinen,len;
	char sendmsg[20],rcvmsg[20];

	
	csd = socket(AF_INET,SOCK_STREAM,0);
	if(csd<0)
		printf("Can't create\n");
	else
		printf("Socket is created\n");

	struct sockaddr_in servaddr, cliaddr;
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(cport);

	csd2 = connect(csd,(struct sockaddr*) &servaddr, sizeof(servaddr));
	if(csd2<0)
		printf("Can't connect\n");
	else
		printf("Connected\n");

	do{
		fgets(sendmsg,20,stdin);
		len = strlen(sendmsg);
		sendmsg[len-1] = '\0';
		send(csd,sendmsg,20,0);
		recv(csd,rcvmsg,20,0);
		printf("%s\n",rcvmsg);
	}
	while(strcmp(rcvmsg,"bye")!=0);
	

	close(csd);
}
server
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include<time.h>

int main() {

    time_t ticks;
    ticks = time(NULL);

    int sd, sd2, sport = 6000, nsd, clilen;
    char rcvmsg[20],sendmsg[20];
    int len;
        
    sd = socket(AF_INET, SOCK_STREAM, 0);
    if (sd < 0)
        printf("Can't create\n");
    else
        printf("Socket is created\n");

    struct sockaddr_in servaddr, cliaddr;
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(sport);

    sd2 = bind(sd, (struct sockaddr*)&servaddr, sizeof(servaddr));
    if (sd2 < 0)
        printf("Can't bind\n");
    else
        printf("Binded\n");

    listen(sd, 5);
    clilen = sizeof(cliaddr);
    nsd = accept(sd, (struct sockaddr*)&cliaddr, &clilen);
    if (nsd < 0)
        printf("Can't accept\n");
    else
        printf("Accept\n");
    printf("Client address: %s\n", inet_ntoa(cliaddr.sin_addr));

    do {
        recv(nsd, rcvmsg, 20 , 0);
        printf("%s\n", rcvmsg);
        fgets(sendmsg,20, stdin);
        len = strlen(sendmsg);
        sendmsg[len-1]='\0';
        send(nsd,sendmsg,20,0);
    } while (strcmp(rcvmsg, "bye") != 0);

    close(sd);
    close(nsd);
    
    return 0;
}

client:time question
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<string.h>
#include<time.h>

int main(){

    time_t ticks;
    int sd,sd2,sport=7010,nsd,len,clinen;
    sd = socket(AF_INET,SOCK_STREAM,0);
    char sendmsg[100],rcvmsg[100];
    ticks = time(NULL);
    strcpy(sendmsg,ctime(&ticks));

    if(sd<0){
        printf("Cannot create\n");
    }else{
        printf("Create\n");
    }

    struct sockaddr_in servaddr, cliaddr;
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(sport);
    sd2 = bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));

    if(sd2<0){
        printf("Can't bind\n");
    }else{
        printf("Binded\n");
    }

    listen(sd,5);
    clinen = sizeof(cliaddr);
    nsd = accept(sd,(struct sockaddr*)&cliaddr,&clinen);

    if(nsd<0){
        printf("Can't accept\n");
    }else{
        printf("Accept\n");
    do{
        ticks = time(NULL);
        strcpy(sendmsg,ctime(&ticks));
        send(nsd,sendmsg,100,0);
        recv(nsd,rcvmsg,20,0);
        printf("%s\n",rcvmsg);
        fgets(sendmsg,20,stdin);
        len = strlen(sendmsg);
        sendmsg[len-1] = '\0';
        send(nsd,sendmsg,20,0);
        printf("%s\n",sendmsg);
    }while(strcmp(rcvmsg,"bye") != 0);
    close(sd);
    close(nsd);
}
    return 0;
}
server
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<string.h>
#include<time.h>

int main(){

	int sd,sd2,sport=7010,nsd,len;
	sd = socket(AF_INET,SOCK_STREAM,0);
	char sendmsg[100],rcvmsg[100];

	if(sd<0)
		printf("Can't creat\n");
	else
		printf("Created\n");

	struct sockaddr_in servaddr, cliaddr;
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(sport);
	sd2 = connect(sd,(struct sockaddr*) &servaddr,sizeof(servaddr));

	if(sd2<0){
		printf("Cannot connect\n");
		}
	else{
		printf("Connected\n");

	recv(sd,rcvmsg,100,0);
	printf("Message read %s",rcvmsg);
	do{
		fgets(sendmsg,20,stdin);
		len = strlen(sendmsg);
		sendmsg[len - 1] = '\0';
		send(sd,sendmsg,20,0);
		recv(sd,rcvmsg,20,0);
		printf("%s\n",rcvmsg);
	}while(strcmp(rcvmsg,"bye") != 0);
	close(sd);
	close(nsd);
}
	close(nsd);
	return 0;
}

echoserver
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
int main() {
    int len, sfd, b, r, s, port;
    char buff[20];
    struct sockaddr_in servaddr, cliaddr;

    sfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sfd == -1) {
        perror("socket");
        exit(-1);
    }

    memset(servaddr.sin_zero, '\0', sizeof(servaddr.sin_zero));
    printf("\n Enter port number: ");
    scanf("%d", &port);
    printf("\n Port number is %d", port);

    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = INADDR_ANY;
    servaddr.sin_port = htons(port);
    b = bind(sfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
    
    if(b == -1) {
        perror("bind");
        exit(-1);
    }

    len = sizeof(cliaddr);
    do {
        r = recvfrom(sfd, buff, sizeof(buff), 0, (struct sockaddr*)&cliaddr, &len);
        if(r == -1) {
            perror("Receive Error");
            exit(0);
        }
        write(1, buff, r); 
        s = sendto(sfd, buff, r, 0, (struct sockaddr*)&cliaddr, sizeof(cliaddr));
        if(s == -1) {
            perror("Send Error");
            exit(0);
        }
    } while (strncmp("bye",buff,3) != 0);

    return 0;
}
client
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>

int main() {
    int len, listenfd, i = 0, r, port;
    char buff[20], c;
    struct sockaddr_in servaddr, cliaddr;
    socklen_t servlen;

    listenfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (listenfd == -1) {
        perror("socket");
        exit(-1);
    }

    memset(servaddr.sin_zero, '\0', sizeof(servaddr.sin_zero));
    printf("\n Enter the port number: ");
    scanf("%d", &port);
    printf("\n Port number is: %d", port);

    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = INADDR_ANY;
    servaddr.sin_port = htons(port);

    printf("\n Client: ");

    do {
        int val = read(0, buff, sizeof(buff));
        if (val == -1) {
            perror("Read error");
            exit(-1);
        }

        int s = sendto(listenfd, buff, val, 0, (struct sockaddr*)&servaddr, sizeof(servaddr));
        if (s == -1) {
            perror("Send error");
            exit(-1);
        }

        len = sizeof(cliaddr);
        r = recvfrom(listenfd, buff, sizeof(buff), 0, (struct sockaddr*)&servaddr, &len);
        if (r == -1) {
            perror("Receive error");
            exit(-1);
        }
        write(1, buff, r);
    } while (strncmp("bye",buff,3) != 0);

    return 0;
}

server:udp
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<string.h>
#include<time.h>
#include<stdio.h>

int main(){

	time_t ticks;
	ticks = time(NULL);

	int UDP_server_socket,bind_test,clinen,sport=6006,len,lenl,prot,n;
	char sendmsg[100],rcvmsg[100];

	UDP_server_socket = socket(AF_INET,SOCK_DGRAM,0);
	if(UDP_server_socket<0)
		printf("Can't create\n");
	else
		printf("Created\n");

	struct sockaddr_in servaddr,cli;
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(sport);

	bind_test = bind(UDP_server_socket,(struct sockaddr*) &servaddr,sizeof(servaddr));
	if(bind_test<0)
		printf("Can't bind\n");
	else
		printf("Binded\n");

	len = sizeof(servaddr);
	bzero(rcvmsg,40);
	while(1){
		recvfrom(UDP_server_socket,rcvmsg,40,0,(struct sockaddr*) &servaddr,&len);
		printf("From client: %s",rcvmsg);
		bzero(rcvmsg,40);
		n=0;
		bzero(sendmsg,40);
		printf("Input message: ");
		while((sendmsg[n++] = getchar())!='\n');
		sendto(UDP_server_socket,sendmsg,40,0,(struct sockaddr*) &servaddr,len);
		if(strncmp("bye",sendmsg,3) == 0)
		break;
	}
	close(UDP_server_socket);
}
client:udp
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<string.h>
#include<time.h>
#include<stdio.h>

int main(){

	int UDP_client_socket,sport=6006,len,lenl,port,n;
	char sendmsg[100],rcvmsg[100];

	UDP_client_socket = socket(AF_INET,SOCK_DGRAM,0);
	if(UDP_client_socket<0)
		printf("Can't create\n");
	else
		printf("Created\n");

	struct sockaddr_in servaddr;
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(sport);

	len = sizeof(servaddr);
	while(1){
		bzero(sendmsg,40);
		printf("Input message: ");
		n=0;
		while((sendmsg[n++]  = getchar()) != '\n');
		sendto(UDP_client_socket,sendmsg,40,0,(struct sockaddr*)&servaddr,len);
		bzero(rcvmsg,40);
		recvfrom(UDP_client_socket,rcvmsg,40,0,(struct sockaddr*)&servaddr,&len);
		printf("From server: %s",rcvmsg);
		if(strncmp("bye",rcvmsg,3) == 0)
		break;
	}	
	close(UDP_client_socket);
}

server:servergo
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>

int main(){

	int sd,sd2,clinen,sport,i,client_socket,count=0,val;
	int port;
	char data[50],senderdata[50];
	int window_size;
	int frame = 0;
	int ACK;

	struct sockaddr_in servaddr,cliaddr;
	printf("Enter the server port:\n");
	scanf("%d",&sport);
	printf("%d\n",sport);
	
	sd = socket(AF_INET,SOCK_STREAM,0);
	if(sd<0)
		printf("Can't create\n");
	else
		printf("Created\n");

	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(sport);

	sd2 = bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));
	if(sd2<0)
		printf("Can't bind\n");
	else
		printf("Binded\n");

	listen(sd,5);
	clinen = sizeof(cliaddr);
	client_socket = accept(sd,(struct sockaddr*)&cliaddr,&clinen);
	if(client_socket<0)
		printf("Can't accept\n");
	else
		printf("Accepted\n");

	recv(client_socket,&window_size,sizeof(window_size),0);

	while(frame<window_size){
		for(int i=0;i<window_size;i++){
			recv(client_socket,&frame,sizeof(frame),0);
			printf("Transmitted frames received from the client is %d\n",frame);
			frame++;
			if(frame == window_size){
				break;
			}
		}
		printf("Enter the negative acknowlegement tp be sent to the client:\n");
		scanf("%d",&ACK);
		send(client_socket,&ACK,sizeof(ACK),0);
		if(ACK == window_size)
			break;
		else
			frame = ACK;		
	}
	
	return 0;
}
client:client go
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>

int main(){

	int sock,cport,val,count,i,cd2;
	char senddata[50],recvdata[50];
	struct sockaddr_in servaddr;
	int window_size;
	int frame = 0;
	int ACK;

	printf("Enter the port:\n");
	scanf("%d",&cport);
	printf("%d\n",cport);

	sock = socket(AF_INET,SOCK_STREAM,0);
	if(sock<0)
		printf("Can't create\n");
	else
		printf("Created\n");

	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(cport);

	cd2 = connect (sock, (struct sockaddr*)&servaddr,sizeof(servaddr));
	if(cd2<0)
		printf("Can't create\n");
	else
		printf("Created\n");

	printf("Enter the window size\n");
	scanf("%d",&window_size);
	send(sock,&window_size,sizeof(window_size),0);

	while(frame<window_size){
		for(int i=0;i<window_size;i++){
			printf("Transmitted frames received from the clinet %d\n",frame);
			send(sock,&frame,sizeof(frame),0);
			frame++;
			if(frame == window_size){
				break;
			}
		}
		recv(sock,&ACK,sizeof(ACK),0);
		printf("Enter the negative acknowledgment to be sent to the clinet%d\n",ACK);
		if(ACK == window_size)
			break;
		else
			frame = ACK;
	}

	return 0;
}
Leave a Comment