---
# System prepended metadata

title: Codes

---

# Codes

### Q1
```
#include <unistd.h>
#include <string.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>

using namespace std;

int main(){
        int sockfd, n;
        sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
        char buffer[10000];
        if(sockfd < 0){
                printf("Error in creating socket.\n");
                return 0;
        }
        printf("Socket created.\n");
        struct sockaddr_in cli, ser;
        socklen_t len = sizeof(struct sockaddr_in);
        while(1){
                n = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&cli, &len);
                printf("Received %d bytes.\n", n);

                struct iphdr* ip_hdr = (struct iphdr*)buffer;

                printf("Length of IP header is %d.\n", (ip_hdr->ihl)*4);

                struct icmphdr* icmp_hdr = (struct icmphdr*)((char *)ip_hdr + (ip_hdr->ihl)*4);
                printf("Message type is %d.\n", icmp_hdr->type);
        }

        return 0;
}
```

### Q2 server
```
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <netinet/in.h>

int main(int argc, char **argv){
        int sockfd, connfd;
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if(sockfd<0) printf("OHOH.\n");
        printf("Socket created.\n");
        struct sockaddr_in ser, cli;
        memset(&ser, 0, sizeof(struct sockaddr_in));
        ser.sin_family = AF_INET;
        ser.sin_addr.s_addr = htonl(INADDR_ANY);
        ser.sin_port = htons(6011);
//      socklen_t len = sizeof(cli)
        bind(sockfd, (struct sockaddr*)&ser, sizeof(ser));
        listen(sockfd, 2);
        printf("Istening.\n");
        while(1){
                socklen_t len = sizeof(cli);
                connfd = accept(sockfd, (struct sockaddr*)&cli, &len);
                printf("Accepted.\n");
                int childpid;
                if((childpid = fork()) == 0){
                        close(sockfd);
                        char msg1[10000];
                        int n1 = read(connfd, msg1, sizeof(msg1));
                        write(connfd, msg1, sizeof(msg1));
                        close(connfd);
                        return 0;
                }
                close(connfd);
        }
        close(sockfd);
        return 0;
}
```

### Q2 client
```
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <arpa/inet.h>

int main(int argc, char **argv){
        int sockfd;
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        struct sockaddr_in ser, cli;
        memset(&ser, 0, sizeof(struct sockaddr_in));
        ser.sin_family = AF_INET;
        ser.sin_addr.s_addr = inet_addr(argv[1]);
        ser.sin_port = htons(6011);
        connect(sockfd, (struct sockaddr*)&ser, sizeof(ser));
        char buffer[10000];
        fgets(buffer, sizeof(buffer), stdin);
        int n = strlen(buffer);
        write(sockfd, buffer, n);
        n = read(sockfd, buffer, 512);
        if(n<0) printf("Error reading.\n");
        buffer[n] = 0;
        fputs(buffer, stdout);

        return 0;
}
```