Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TCP SOCKET PROGRAMMING
- TCP SERVER
- #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;
- }
- TCP CLIENT
- #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);
- scanf("%s",buffer);
- printf("enter message:\n");
- 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;
- }
- EXECUTION :
- OPEN 2 TERMINALS IN SAME FOLDER
- first terminal
- RUN SERVER FIRST - gcc server.c -o server
- ./server
- 2nd terminal
- RUN client - gcc client.c -o client
- ./client
- UDP SERVER
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- int main(int argc, char **argv){
- if (argc != 2){
- printf("Usage: %s <port>\n", argv[0]);
- exit(0);
- }
- char *ip = "127.0.0.1";
- int port = atoi(argv[1]);
- int sockfd;
- struct sockaddr_in server_addr, client_addr;
- char buffer[1024];
- socklen_t addr_size;
- int n;
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- if (sockfd < 0){
- perror("[-]socket error");
- exit(1);
- }
- memset(&server_addr, '\0', sizeof(server_addr));
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(port);
- server_addr.sin_addr.s_addr = inet_addr(ip);
- n = bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
- if (n < 0) {
- perror("[-]bind error");
- exit(1);
- }
- bzero(buffer, 1024);
- addr_size = sizeof(client_addr);
- recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr*)&client_addr, &addr_size);
- printf("[+]Data recv: %s\n", buffer);
- bzero(buffer, 1024);
- strcpy(buffer, "Welcome to the UDP Server.");
- sendto(sockfd, buffer, 1024, 0, (struct sockaddr*)&client_addr, sizeof(client_addr));
- printf("[+]Data send: %s\n", buffer);
- return 0;
- }
- UDP CLIENT
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- int main(int argc, char **argv){
- if (argc != 2) {
- printf("Usage: %s <port>\n", argv[0]);
- exit(0);
- }
- char *ip = "127.0.0.1";
- int port = atoi(argv[1]);
- int sockfd;
- struct sockaddr_in addr;
- char buffer[1024];
- socklen_t addr_size;
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- memset(&addr, '\0', sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- addr.sin_addr.s_addr = inet_addr(ip);
- bzero(buffer, 1024);
- strcpy(buffer, "Hello, World!");
- sendto(sockfd, buffer, 1024, 0, (struct sockaddr*)&addr, sizeof(addr));
- printf("[+]Data send: %s\n", buffer);
- bzero(buffer, 1024);
- addr_size = sizeof(addr);
- recvfrom(sockfd, buffer, 1024, 0, (struct sockaddr*)&addr, &addr_size);
- printf("[+]Data recv: %s\n", buffer);
- return 0;
- }
- EXECUTION :
- OPEN 2 TERMINALS IN SAME FOLDER
- first terminal
- RUN SERVER FIRST - gcc server.c -o server
- ./server 4444
- 2nd terminal
- RUN client - gcc client.c -o client
- ./client 4444
- parity checking
- #include <stdio.h>
- #include<string.h>
- int main() {
- char a[10] = {0};
- gets(a);
- int ones = 0 ;
- for(int i = 0 ; i < sizeof(a); i++)
- {
- if(a[i] == '1')
- {
- ones = ones + 1 ;
- }
- }
- printf("%d\n",ones);
- if ( ones%2 == 0)
- {
- printf("even parity");
- }
- else
- {
- printf("odd parity");
- }
- return 0;
- }
- leaky bucket algorithm
- #include <stdio.h>
- #define max_size 10
- int main() {
- int n,pac_size[max_size],capacity,rate,sent,remn=0;
- printf("enter number of the packets:");
- scanf("%d",&n);
- printf("\nenter bucket input size/capacity: ");
- scanf("%d",&capacity);
- printf("\nenter bucket output size/rate: ");
- scanf("%d",&rate);
- printf("\nenter each packet size:");
- for( int i = 0 ; i < n ; i++)
- {
- scanf("%d\n",&pac_size[i]);
- }
- for(int i = 0 ; i < n ; i++)
- {
- printf("packet %d\n",i);
- if(pac_size[i] < capacity)
- {
- printf("packet is accepted into the bucket\n");
- if(pac_size[i] + remn <= rate)
- {
- sent = pac_size[i] + remn;
- remn = 0 ;
- printf("sent = %d\n",sent);
- printf("remaning = %d",remn);
- }
- else
- {
- sent = rate;
- remn = (pac_size[i]+remn)-sent;
- printf("sent = %d\n",sent);
- printf("remaning = %d\n",remn);
- }
- }
- else
- {
- printf("packet of size %d is not accepted as %d is greater than capacity\n",pac_size[i],capacity);
- }
- }
- return 0 ;
- }
- //CHECKSUM IN C
- #include<stdio.h>
- int add(int,int);
- int com(int);
- void main()
- {
- int i,j,dl,dil;
- int data1[10],data2[10],newdata[10],comp[10],checksum[10];
- printf("\n Enter the data length=");
- scanf("%d",&dl);
- printf("\n Enter the data1 : \n");
- for(i=0;i<dl;i++)
- scanf("%d",&data1[i]);
- printf("\n Enter the data2 : \n");
- for(i=0;i<dl;i++)
- scanf("%d",&data2[i]);
- for(i=dl-1;i>=0;i--)
- {
- newdata[i]=add(data1[i],data2[i]);
- }
- printf("\n\n Data 1 : ");
- for(i=0;i<dl;i++)
- printf("%d",data1[i]);
- printf("\n Data 2 : ");
- for(i=0;i<dl;i++)
- printf("%d",data2[i]);
- printf("\n\n The new data is : ");
- for(i=0;i<dl;i++)
- {
- printf("%d",newdata[i]);
- }
- printf("\n Checksum : ");
- for(i=0;i<dl;i++)
- {
- checksum[i]=com(newdata[i]);
- printf("%d",checksum[i]);
- }
- printf("\n\n Receiver Side : \n");
- printf("\n Data : ");
- for(i=0;i<dl;i++)
- printf("%d",data1[i]);printf(" ");
- for(i=0;i<dl;i++)
- printf("%d",data2[i]);printf(" ");
- for(i=0;i<dl;i++)
- printf("%d",checksum[i]);
- printf("\n Addition : ");
- for(i=dl-1;i>=0;i--)
- {
- newdata[i]=add(newdata[i],checksum[i]);
- }
- for(i=0;i<dl;i++)
- {
- printf("%d",newdata[i]);
- }
- printf("\n Compliment : ");
- for(i=0;i<dl;i++)
- {
- comp[i]=com(newdata[i]);
- printf("%d",comp[i]);
- }
- }
- int add(int x, int y)
- {
- static int carry=0;
- if(x==1 && y==1 && carry==0)
- {
- carry=1;
- return 0;
- }
- else if(x==1 && y==1 && carry==1)
- {
- carry=1;
- return 1;
- }
- else if(x==1 && y==0 && carry==0)
- {
- carry=0;
- return 1;
- }
- else if(x==1 && y==0 && carry==1)
- {
- carry=1;
- return 0;
- }
- else if(x==0 && y==1 && carry==0)
- {
- carry=0;
- return 1;
- }
- else if(x==0 && y==1 && carry==1)
- {
- carry=1;
- return 0;
- }
- else if(x==0 && y==0 && carry==0)
- {
- carry=0;
- return 0;
- }
- else
- {
- carry=0;
- return 1;
- }
- }
- int com(int a)
- {
- if(a==0)
- return 1;
- else
- return 0;
- }
- //CHECKSUM IN PYTHON
- //checksum - python
- def findChecksum(SentMessage, k):
- # Dividing sent message in packets of k bits.
- c1 = SentMessage[0:k]
- c2 = SentMessage[k:2*k]
- c3 = SentMessage[2*k:3*k]
- c4 = SentMessage[3*k:4*k]
- # Calculating the binary sum of packets
- Sum = bin(int(c1, 2)+int(c2, 2)+int(c3, 2)+int(c4, 2))[2:]
- # Adding the overflow bits
- if(len(Sum) > k):
- x = len(Sum)-k
- Sum = bin(int(Sum[0:x], 2)+int(Sum[x:], 2))[2:]
- if(len(Sum) < k):
- Sum = '0'*(k-len(Sum))+Sum
- # Calculating the complement of sum
- Checksum = ''
- for i in Sum:
- if(i == '1'):
- Checksum += '0'
- else:
- Checksum += '1'
- return Checksum
- # Function to find the Complement of binary addition of
- # k bit packets of the Received Message + Checksum
- # Driver Code
- SentMessage = "10010101011000111001010011101100"
- k = 8
- #ReceivedMessage = "10000101011000111001010011101001"
- ReceivedMessage = "10010101011000111001010011101000"
- # Calling the findChecksum() function
- Checksum = findChecksum(SentMessage, k)
- # Calling the checkReceiverChecksum() function
- ReceiverChecksum = findChecksum(ReceivedMessage, k)
- # Printing Checksum
- print("SENDER SIDE CHECKSUM: ", Checksum)
- print("RECEIVER SIDE CHECKSUM: ", ReceiverChecksum)
- if (Checksum == ReceiverChecksum):
- print("status:ACCEPTED")
- else:
- print("status:ERROR DETECTED");
- //
- PACKET TRACER-"https://gofile.io/d/8nOa4Q"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement