├── .gitignore ├── 03-tcp-ip-client-server ├── README.md ├── client.c └── server.c ├── 04-udp-echo-client-server ├── README.md ├── client.c └── server.c ├── 05-day-time-server-tcp-ip ├── README.md ├── client.c └── server.c ├── 06-half-duplex-chat-tcp-ip ├── README.md ├── client.c └── server.c ├── 07-full-duplex-chat-tcp-ip ├── README.md ├── client.c └── server.c ├── 08-file-transfer-protocol ├── README.md ├── client.c ├── server.c └── text.txt ├── 09-remote-command-execution-udp ├── README.md ├── client.c └── server.c ├── 10-ARP-implementation-using-UDP ├── README.md └── arp.c ├── LICENSE ├── Packet-tracer-experiments ├── 12-NAT-packet-tracer │ ├── EX 12 NAT.docx │ ├── NAT.pkt │ └── README.md └── 13-VPN-packet-tacer │ ├── README.md │ └── VPN.pkt └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | .dropbox 54 | server 55 | -------------------------------------------------------------------------------- /03-tcp-ip-client-server/README.md: -------------------------------------------------------------------------------- 1 | # Socket in C 2 | 3 | _Sockets are low level endpoint used for processing information across a network.Common networking protocols like HTTP and FTP rely on the sockets underneath to make connection._ 4 | 5 | ## Client Socket Workflow 6 | 7 | **socket() 8 | | 9 | connect() 10 | | 11 | recv()** 12 | 13 | * The client socket is created with a socket() call, and the connected to a remote address with the connect() call, and then finally can retrive data with recv() call. 14 | 15 | * The client socket is created with a socket() call, and the connected to a remote address with the connect() call, and then finally can retrive data with recv() call. 16 | 17 | ## Server Socket Workflow 18 | 19 | * On the "server" end of the socket,we need to also create a socket with a socket() call, but we need to bind() that socket to an IP and port where it can then listen() for connections, finally accept() a connection and then send() or recv() data to the other sockets it has connected to. 20 | 21 | **socket() 22 | | 23 | bind() 24 | | 25 | listen() 26 | | 27 | accept()** 28 | 29 | ### Understanding Client Side Code 30 | 31 | ```C 32 | //adding includes 33 | #include 34 | #include 35 | //for socket and related functions 36 | #include 37 | #include 38 | //for including structures which will store information needed 39 | #include 40 | #include 41 | 42 | //main functions 43 | int main(){ 44 | 45 | //creating a socket 46 | int network_socket; 47 | //calling socket function and storing the result in the variable 48 | //socket(domainOfTheSocket,TypeOfTheSocket,FlagForProtocol{0 for default protocol i.e, TCP}) 49 | //AF_INET = constant defined in the header file for us 50 | //TCP vs UDP --- SOCK_STREAM for TCP 51 | // flag 0 for TCP (default protocol) 52 | network_socket = socket(AF_INET,SOCK_STREAM,0); 53 | //creating network connection 54 | //in order to connect to the other side of the socket we need to declare connect 55 | //with specifying address where we want to connect to 56 | //already defined struct sockaddr_in 57 | struct sockaddr_in server_address; 58 | //what type of address we are woking with 59 | server_address.sin_family = AF_INET; 60 | //for taking the port number and htons converts the port # to the appropriate data type we want to write 61 | //to specifying the port 62 | //htons : conversion functions 63 | 64 | server_address.sin_port = htons(9002); 65 | //structure within structure A.B.c 66 | server_address.sin_addr.s_addr = INADDR_ANY; //INADDR_ANY is for connection with 0000 67 | //connect returns us a response that connection is establlised or not 68 | int connect_status = connect(network_socket,(struct sockaddr *) &server_address, sizeof(server_address)); 69 | //check for the error with the connection 70 | 71 | if (connect_status == -1){ 72 | printf("There was an error making a connection to socket\n" ); 73 | } 74 | 75 | //recieve data from the server 76 | char server_response[256]; 77 | 78 | //recieve the data from the server 79 | recv(network_socket,&server_response,sizeof(server_response),0); 80 | 81 | //recieved data from the server successfully then printing the data obtained from the server 82 | 83 | printf("Ther server sent the data : %s",server_response); 84 | 85 | //closing the socket 86 | close(network_socket); 87 | return 0; 88 | } 89 | ``` 90 | 91 | ### Understanding Server Side Code 92 | 93 | ```C 94 | #include 95 | #include 96 | #include 97 | #include 98 | #include 99 | #include 100 | 101 | int main(){ 102 | 103 | char server_message[256] = "You have a missed call from server"; 104 | //create the server socket 105 | int server_socket; 106 | server_socket = socket(AF_INET,SOCK_STREAM,0); 107 | //define the server address 108 | //creating the address as same way we have created for TCPclient 109 | 110 | struct sockaddr_in server_address; 111 | server_address.sin_family = AF_INET; 112 | server_address.sin_port = htons(9002); 113 | server_address.sin_addr.s_addr = INADDR_ANY; 114 | 115 | //calling bind function to oir specified IP and port 116 | bind(server_socket,(struct sockaddr*) &server_address,sizeof(server_address)); 117 | 118 | listen(server_socket,5); 119 | 120 | //starting the accepting 121 | int client_socket; 122 | //accept(socketWeAreAccepting,structuresClientIsConnectingFrom,) 123 | client_socket = accept(server_socket,NULL,NULL); 124 | //sending data 125 | //send(toWhom,Message,SizeOfMessage,FLAG); 126 | send(client_socket,server_message,sizeof(server_message),0); 127 | 128 | //close the socket 129 | close(server_socket); 130 | return 0; 131 | } 132 | ``` -------------------------------------------------------------------------------- /03-tcp-ip-client-server/client.c: -------------------------------------------------------------------------------- 1 | /* 2 | Creating the TCP Client workflow in this program using C. 3 | 4 | * Title : TCP client 5 | * Name : Aditya Pratap Singh Rajput 6 | * Subject : Network Protocols And Programming using C 7 | Note : please consider the TYPOS in comments. 8 | Thanks. 9 | */ 10 | 11 | // adding headers 12 | #include 13 | #include 14 | //for socket and related functions 15 | #include 16 | #include 17 | //for including structures which will store information needed 18 | #include 19 | #include 20 | #define SIZE 1000 21 | 22 | //main functions 23 | int main() 24 | { 25 | int socketDescriptor = socket(AF_INET, SOCK_STREAM, 0); 26 | // server address 27 | struct sockaddr_in serverAddress; 28 | serverAddress.sin_family = AF_INET; 29 | serverAddress.sin_port = htons(9002); 30 | serverAddress.sin_addr.s_addr = INADDR_ANY; 31 | 32 | // communicates with listen 33 | connect(socketDescriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress)); 34 | 35 | char serverResponse[SIZE]; 36 | recv(socketDescriptor, &serverResponse, sizeof(serverResponse), 0); 37 | printf("Ther server sent the data : %s", serverResponse); 38 | 39 | //closing the socket 40 | close(socketDescriptor); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /03-tcp-ip-client-server/server.c: -------------------------------------------------------------------------------- 1 | /* 2 | Creating the TCP socket workflow in this program using C. 3 | 4 | * Title : TCP client 5 | * Name : Aditya Pratap Singh Rajput 6 | * Subject : Network Protocols And Programming using C 7 | Note : please consider the TYPOS in comments. 8 | Thanks. 9 | */ 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | int main(){ 18 | 19 | char serverMessage[256] = "You have a missed call from server\n"; 20 | 21 | //create the server socket 22 | int socketDescriptor = socket(AF_INET,SOCK_STREAM,0); 23 | 24 | //define the server address 25 | //creating the address as same way we have created for TCPclient 26 | struct sockaddr_in serverAddress; 27 | serverAddress.sin_family = AF_INET; 28 | serverAddress.sin_port = htons(9002); 29 | serverAddress.sin_addr.s_addr = INADDR_ANY; 30 | 31 | //calling bind function to oir specified IP and port 32 | bind(socketDescriptor,(struct sockaddr*) &serverAddress,sizeof(serverAddress)); 33 | 34 | listen(socketDescriptor,5); 35 | 36 | //starting the accepting 37 | //accept(socketWeAreAccepting,structuresClientIsConnectingFrom,) 38 | int client_socket = accept(socketDescriptor, NULL, NULL); 39 | 40 | //sending data 41 | //send(toWhom,Message,SizeOfMessage,FLAG); 42 | send(client_socket,serverMessage,sizeof(serverMessage),0); 43 | 44 | //close the socket 45 | close(socketDescriptor); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /04-udp-echo-client-server/README.md: -------------------------------------------------------------------------------- 1 | # UDP ECHO SERVER 2 | 3 | The simplest form of client-server interaction uses unreliable datagram delivery to convey messages from a client to a server and back. Consider, for example, a UDP echo server. 4 | 5 | At the server site, a UDP echo server process begins by negotiating with its operating system for permission to use the UDP port ID reserved for the echo service, the UDP echo port. 6 | 7 | Once it has obtained permission, the echo server process enters an infiite loop that has three steps: 8 | 9 | 1. wait for a datagram to amve at the echo port, 10 | 2. reverse the source and destination addresses(including source and destination IP addresses as well as UDP port ids) and 11 | 3. return the datagram to its original sender. 12 | 13 | A UDP message to the UDP echo server, and awaits the reply. 14 | The client expects to receive back exactly the same data as it sent. 15 | The UDP echo service illustrates two important points that are generally true about 16 | client-server interaction. The first concerns the difference between the lifetime of servers and clients: 17 | 18 | A server starts execution before interaction begins and (usually) continues to accept requests and send responses without ever terminating. 19 | 20 | A server waits for requests at a well-known port that has been 21 | reserved for the service it offers. A client allocates an arbitrary, 22 | unused nonreservedport for its communication. 23 | 24 | ## Who would use an echo service '?' 25 | 26 | It is not a service that the average user finds interesting. However, programmers who design, implement, measure, or modify network protocol software, or network managers who test routes and debug communication problems, often use echo servers in testing. For example, an echo service can be used 27 | to determineif it is possible to reach a remote machine. 28 | 29 | ## Learn about code 30 | 31 | * For implementation of UDP use SOCK_DGRAM 32 | 33 | ```C 34 | int serverDescriptor = socket(AF_INET, SOCK_DGRAM, 0); 35 | ``` 36 | 37 | * For sending message use sendto function 38 | 39 | ```C 40 | sendto(serverDescriptor,sendMessage,MAXLINE,0,(struct sockaddr*)&serverAddress,addressLength); 41 | ``` 42 | 43 | * For recieving message use recv 44 | 45 | ```C 46 | recvfrom(socketDescriptor,message,MAXLINE,0,(struct sockaddr*)&clientAddress,&addressLength); 47 | ``` 48 | 49 | * On server side only use ```bind()``` function -------------------------------------------------------------------------------- /04-udp-echo-client-server/client.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Title : echo client 3 | * Name : Aditya Pratap Singh Rajput 4 | * Subject : Network Protocols And Programming using C 5 | * 6 | * */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define MAXLINE 1024 16 | #define PORT 5035 17 | 18 | int main(){ 19 | // socket descriptor creation in udp mode 20 | int serverDescriptor = socket(AF_INET, SOCK_DGRAM, 0); 21 | 22 | // for storing address of address 23 | socklen_t addressLength; 24 | 25 | // preparing message 26 | char sendMessage[MAXLINE],recvMessage[MAXLINE]; 27 | printf("\nEnter message :"); 28 | fgets(sendMessage,sizeof(sendMessage),stdin); 29 | 30 | // storing address in serverAddress 31 | struct sockaddr_in serverAddress; 32 | serverAddress.sin_family = AF_INET; 33 | serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); 34 | serverAddress.sin_port = htons(PORT); 35 | 36 | // storing address size 37 | addressLength = sizeof(serverAddress); 38 | 39 | // checking connection 40 | connect(serverDescriptor,(struct sockaddr*)&serverAddress,addressLength); 41 | 42 | // sending and receiving the messages 43 | sendto(serverDescriptor,sendMessage,MAXLINE,0,(struct sockaddr*)&serverAddress,addressLength); 44 | recvfrom(serverDescriptor,recvMessage,MAXLINE,0,NULL,NULL); 45 | 46 | printf("\nServer's Echo : %s\n",recvMessage); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /04-udp-echo-client-server/server.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Title : echo server 3 | * Name : Aditya Pratap Singh Rajput 4 | * Subject : Network Protocols And Programming using C 5 | * 6 | * */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | // time 14 | 15 | #define MAXLINE 1024 16 | #define PORT 5035 17 | 18 | int main(){ 19 | 20 | int socketDescriptor = socket(AF_INET, SOCK_DGRAM, 0); 21 | int number; 22 | socklen_t addressLength; 23 | char message[MAXLINE]; 24 | 25 | struct sockaddr_in serverAddress,clientAddress; 26 | serverAddress.sin_family = AF_INET; 27 | serverAddress.sin_addr.s_addr=INADDR_ANY; 28 | serverAddress.sin_port=htons(PORT); 29 | 30 | bind(socketDescriptor,(struct sockaddr*)&serverAddress,sizeof(serverAddress)); 31 | 32 | printf("\nServer Started ...\n"); 33 | 34 | while(1){ 35 | printf("\n"); 36 | addressLength = sizeof(clientAddress); 37 | 38 | number = recvfrom(socketDescriptor,message,MAXLINE,0,(struct sockaddr*)&clientAddress,&addressLength); 39 | 40 | printf("\n Client's Message: %s ",message); 41 | 42 | if(number<6) 43 | perror("send error"); 44 | 45 | sendto(socketDescriptor,message,number,0,(struct sockaddr*)&clientAddress,addressLength); 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /05-day-time-server-tcp-ip/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | * If you are not aware of TCP functioning please check previous topics ```03-tcp-ip-client-server``` 4 | * We have inserted sending part i.e, ```send()``` in the ```while()``` loop for making server running throughout the process 5 | * Receiving current time using ```ctime(addressOfVariable)``` function and sending it to the client side -------------------------------------------------------------------------------- /05-day-time-server-tcp-ip/client.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Title : Day-Time client 3 | * Name : Aditya Pratap Singh Rajput 4 | * Subject : Network Protocols And Programming using C 5 | * 6 | * */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "string.h" 14 | 15 | #define PORT 9002 //the port users will be connecting to 16 | #define MAXLINE 30 //for buffer size 17 | 18 | int main(){ 19 | 20 | int socket_descriptor = socket(AF_INET, SOCK_STREAM, 0); 21 | 22 | char serverResponse[MAXLINE]; 23 | 24 | struct sockaddr_in serverAddress; 25 | 26 | serverAddress.sin_family = AF_INET; 27 | serverAddress.sin_addr.s_addr = INADDR_ANY; 28 | serverAddress.sin_port = htons(PORT); 29 | 30 | connect(socket_descriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress)); 31 | 32 | recv(socket_descriptor,serverResponse,MAXLINE-1,0); 33 | 34 | printf("\nTIME FROM SERVER %s\n",serverResponse); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /05-day-time-server-tcp-ip/server.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Title : Day-Time server 3 | * Name : Aditya Pratap Singh Rajput 4 | * Subject : Network Protocols And Programming using C 5 | * 6 | * */ 7 | 8 | //program for date time server 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | //port value and the max size for buffer 18 | #define PORT 9002 //the port users will be connecting to 19 | #define BACKLOG 10 //how many pending connections queue will hold 20 | 21 | int main(){ 22 | //*******************Time Setup*********************** 23 | time_t currentTime ; 24 | time(¤tTime); 25 | //**************************************************** 26 | 27 | int countClient = 0; 28 | 29 | int socket_descriptor = socket(AF_INET, SOCK_STREAM, 0); 30 | 31 | struct sockaddr_in serverAddress; 32 | serverAddress.sin_family = AF_INET; 33 | serverAddress.sin_addr.s_addr=INADDR_ANY; 34 | serverAddress.sin_port=htons(PORT); 35 | 36 | //binding address 37 | bind(socket_descriptor,(struct sockaddr*)&serverAddress,sizeof(serverAddress)); 38 | 39 | //listening to the queue 40 | listen(socket_descriptor,BACKLOG); 41 | 42 | printf("\nServer Started ..."); 43 | 44 | while(1){ 45 | countClient++; 46 | printf("\n"); 47 | 48 | int client_socket = accept(socket_descriptor, NULL, NULL); 49 | // char *string = asctime(timeinfo); 50 | 51 | printf("\nClient %d has requested for time at %s", countClient, ctime(¤tTime)); 52 | 53 | //sending time to the client side 54 | // ctime(&time_from_pc) 55 | send(client_socket,ctime(¤tTime), 30, 0); 56 | 57 | } 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /06-half-duplex-chat-tcp-ip/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | * If you don't have knowledge of basics of TCP, please check previous topics ```03-tcp-ip-client-server``` 4 | * We are preparing half duplex communication method 5 | * ***Half Duplex*** : Refers to the transmission of data in just one direction at a time. -------------------------------------------------------------------------------- /06-half-duplex-chat-tcp-ip/client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Title : Half duplex client side 3 | * Name : Aditya Pratap Singh Rajput 4 | * Subject : Network Protocols And Programming using C 5 | * 6 | Note : please consider the TYPOS in comments. 7 | Thanks. 8 | */ 9 | 10 | #include "stdio.h" 11 | #include "stdlib.h" 12 | #include "string.h" 13 | //headers for socket and related functions 14 | #include 15 | #include 16 | //for including structures which will store information needed 17 | #include 18 | #include 19 | //for gethostbyname 20 | #include "netdb.h" 21 | #include "arpa/inet.h" 22 | 23 | //defines 24 | #define h_addr h_addr_list[0] /* for backward compatibility */ 25 | 26 | #define PORT 9002 // port number 27 | #define MAX 1000 //maximum buffer size 28 | 29 | //main function 30 | int main(){ 31 | char serverResponse[MAX]; 32 | char clientResponse[MAX]; 33 | 34 | //creating a socket 35 | int socketDescriptor = socket(AF_INET, SOCK_STREAM, 0); 36 | 37 | //placeholder for the hostname and my ip address 38 | char hostname[MAX], ipaddress[MAX]; 39 | struct hostent *hostIP; //placeholder for the ip address 40 | //if the gethostname returns a name then the program will get the ip address 41 | if(gethostname(hostname,sizeof(hostname))==0){ 42 | hostIP = gethostbyname(hostname);//the netdb.h fucntion gethostbyname 43 | }else{ 44 | printf("ERROR:FCC4539 IP Address Not "); 45 | } 46 | 47 | struct sockaddr_in serverAddress; 48 | serverAddress.sin_family = AF_INET; 49 | serverAddress.sin_port = htons(PORT); 50 | serverAddress.sin_addr.s_addr = INADDR_ANY; 51 | 52 | connect(socketDescriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress)); 53 | 54 | // getting the address port and remote host 55 | printf("\nLocalhost: %s\n", inet_ntoa(*(struct in_addr*)hostIP->h_addr)); 56 | printf("Local Port: %d\n", PORT); 57 | printf("Remote Host: %s\n", inet_ntoa(serverAddress.sin_addr)); 58 | 59 | while (1) 60 | { //recieve the data from the server 61 | recv(socketDescriptor, serverResponse, sizeof(serverResponse), 0); 62 | //recieved data from the server successfully then printing the data obtained from the server 63 | printf("\nSERVER : %s", serverResponse); 64 | 65 | printf("\ntext message here... :"); 66 | scanf("%s", clientResponse); 67 | send(socketDescriptor, clientResponse, sizeof(clientResponse), 0); 68 | } 69 | 70 | //closing the socket 71 | close(socketDescriptor); 72 | return 0; 73 | } -------------------------------------------------------------------------------- /06-half-duplex-chat-tcp-ip/server.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Title : Half duplex server side 3 | * Name : Aditya Pratap Singh Rajput 4 | * Subject : Network Protocols And Programming using C 5 | * 6 | Note : please consider the TYPOS in comments. 7 | Thanks. 8 | */ 9 | 10 | #include "stdio.h" 11 | #include "stdlib.h" 12 | #include "string.h" 13 | //headers for socket and related functions 14 | #include 15 | #include 16 | //for including structures which will store information needed 17 | #include 18 | #include 19 | //for gethostbyname 20 | #include "netdb.h" 21 | #include "arpa/inet.h" 22 | #define MAX 1000 23 | #define BACKLOG 5 // how many pending connections queue will hold 24 | int main() 25 | { 26 | char serverMessage[MAX]; 27 | char clientMessage[MAX]; 28 | //create the server socket 29 | int socketDescriptor = socket(AF_INET, SOCK_STREAM, 0); 30 | 31 | 32 | struct sockaddr_in serverAddress; 33 | serverAddress.sin_family = AF_INET; 34 | serverAddress.sin_port = htons(9002); 35 | serverAddress.sin_addr.s_addr = INADDR_ANY; 36 | 37 | //calling bind function to oir specified IP and port 38 | bind(socketDescriptor, (struct sockaddr*)&serverAddress, sizeof(serverAddress)); 39 | 40 | listen(socketDescriptor, BACKLOG); 41 | 42 | //starting the accepting 43 | int clientSocketDescriptor = accept(socketDescriptor, NULL, NULL); 44 | 45 | while (1) 46 | { 47 | printf("\ntext message here .. :"); 48 | scanf("%s", serverMessage); 49 | send(clientSocketDescriptor, serverMessage, sizeof(serverMessage) , 0); 50 | //recieve the data from the server 51 | recv(clientSocketDescriptor, &clientMessage, sizeof(clientMessage), 0) ; 52 | //recieved data from the server successfully then printing the data obtained from the server 53 | printf("\nCLIENT: %s", clientMessage); 54 | 55 | } 56 | //close the socket 57 | close(socketDescriptor); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /07-full-duplex-chat-tcp-ip/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | * If you don't have knowledge of basics of TCP, please check previous topics ```03-tcp-ip-client-server``` 4 | * We are preparing full duplex communication method 5 | * ***Full Duplex*** : Refers to the transmission of data in both direction at any time. 6 | * We are using ```fork()``` function for making a child process and then switching between the recv and send mode. -------------------------------------------------------------------------------- /07-full-duplex-chat-tcp-ip/client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Title : Full duplex client side 3 | * Name : Aditya Pratap Singh Rajput 4 | * Subject : Network Protocols And Programming using C 5 | Note : please consider the TYPOS in comments. 6 | Thanks. 7 | */ 8 | 9 | #include "stdio.h" 10 | #include "stdlib.h" 11 | #include "string.h" 12 | //headers for socket and related functions 13 | #include 14 | #include 15 | //for including structures which will store information needed 16 | #include 17 | #include 18 | //for gethostbyname 19 | #include "netdb.h" 20 | #include "arpa/inet.h" 21 | 22 | int main() 23 | { 24 | int socketDescriptor; 25 | 26 | struct sockaddr_in serverAddress; 27 | char sendBuffer[1000],recvBuffer[1000]; 28 | 29 | pid_t cpid; 30 | 31 | bzero(&serverAddress,sizeof(serverAddress)); 32 | 33 | serverAddress.sin_family=AF_INET; 34 | serverAddress.sin_addr.s_addr=inet_addr("127.0.0.1"); 35 | serverAddress.sin_port=htons(5500); 36 | 37 | /*Creating a socket, assigning IP address and port number for that socket*/ 38 | socketDescriptor=socket(AF_INET,SOCK_STREAM,0); 39 | 40 | /*Connect establishes connection with the server using server IP address*/ 41 | connect(socketDescriptor,(struct sockaddr*)&serverAddress,sizeof(serverAddress)); 42 | 43 | /*Fork is used to create a new process*/ 44 | cpid=fork(); 45 | if(cpid==0) 46 | { 47 | while(1) 48 | { 49 | bzero(&sendBuffer,sizeof(sendBuffer)); 50 | printf("\nType a message here ... "); 51 | /*This function is used to read from server*/ 52 | fgets(sendBuffer,10000,stdin); 53 | /*Send the message to server*/ 54 | send(socketDescriptor,sendBuffer,strlen(sendBuffer)+1,0); 55 | printf("\nMessage sent !\n"); 56 | } 57 | } 58 | else 59 | { 60 | while(1) 61 | { 62 | bzero(&recvBuffer,sizeof(recvBuffer)); 63 | /*Receive the message from server*/ 64 | recv(socketDescriptor,recvBuffer,sizeof(recvBuffer),0); 65 | printf("\nSERVER : %s\n",recvBuffer); 66 | } 67 | } 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /07-full-duplex-chat-tcp-ip/server.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Title : Full duplex server side 4 | * Name : Aditya Pratap Singh Rajput 5 | * Subject : Network Protocols And Programming using C 6 | * 7 | Note : please consider the TYPOS in comments. 8 | Thanks. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | int main(int argc,char *argv[]) 21 | { 22 | int clientSocketDescriptor,socketDescriptor; 23 | 24 | struct sockaddr_in serverAddress,clientAddress; 25 | socklen_t clientLength; 26 | 27 | char recvBuffer[1000],sendBuffer[1000]; 28 | pid_t cpid; 29 | bzero(&serverAddress,sizeof(serverAddress)); 30 | /*Socket address structure*/ 31 | serverAddress.sin_family=AF_INET; 32 | serverAddress.sin_addr.s_addr=htonl(INADDR_ANY); 33 | serverAddress.sin_port=htons(5500); 34 | /*TCP socket is created, an Internet socket address structure is filled with 35 | wildcard address & server’s well known port*/ 36 | socketDescriptor=socket(AF_INET,SOCK_STREAM,0); 37 | /*Bind function assigns a local protocol address to the socket*/ 38 | bind(socketDescriptor,(struct sockaddr*)&serverAddress,sizeof(serverAddress)); 39 | /*Listen function specifies the maximum number of connections that kernel should queue 40 | for this socket*/ 41 | listen(socketDescriptor,5); 42 | printf("%s\n","Server is running ..."); 43 | /*The server to return the next completed connection from the front of the 44 | completed connection Queue calls it*/ 45 | clientSocketDescriptor=accept(socketDescriptor,(struct sockaddr*)&clientAddress,&clientLength); 46 | /*Fork system call is used to create a new process*/ 47 | cpid=fork(); 48 | 49 | if(cpid==0) 50 | { 51 | while(1) 52 | { 53 | bzero(&recvBuffer,sizeof(recvBuffer)); 54 | /*Receiving the request from client*/ 55 | recv(clientSocketDescriptor,recvBuffer,sizeof(recvBuffer),0); 56 | printf("\nCLIENT : %s\n",recvBuffer); 57 | } 58 | } 59 | else 60 | { 61 | while(1) 62 | { 63 | 64 | bzero(&sendBuffer,sizeof(sendBuffer)); 65 | printf("\nType a message here ... "); 66 | /*Read the message from client*/ 67 | fgets(sendBuffer,10000,stdin); 68 | /*Sends the message to client*/ 69 | send(clientSocketDescriptor,sendBuffer,strlen(sendBuffer)+1,0); 70 | printf("\nMessage sent !\n"); 71 | } 72 | } 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /08-file-transfer-protocol/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | * If you don't have knowledge of basics of TCP, please check previous topics ```03-tcp-ip-client-server``` 4 | * We are preparing to implement sample FTP program. 5 | * ***FTP (File Transfer Protocol)*** : FTP is an acronym for File Transfer Protocol. As the name suggests, FTP is used to transfer files between computers on a network. -------------------------------------------------------------------------------- /08-file-transfer-protocol/client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Title : File Transfer Protocol 3 | * Name : Aditya Pratap Singh Rajput 4 | * Subject : Network Protocols And Programming using C 5 | * 6 | Note : Please consider the TYPOS in comments. 7 | Thanks. 8 | */ 9 | 10 | #include "stdio.h" 11 | #include "stdlib.h" 12 | #include "string.h" 13 | //headers for socket and related functions 14 | #include 15 | #include 16 | #include 17 | //for including structures which will store information needed 18 | #include 19 | #include 20 | //for gethostbyname 21 | #include "netdb.h" 22 | #include "arpa/inet.h" 23 | 24 | // defining constants 25 | #define PORT 9002 26 | 27 | int main() 28 | { 29 | 30 | int serverDescriptor = socket(AF_INET, SOCK_STREAM, 0); 31 | struct sockaddr_in serverAddress; 32 | 33 | char buffer[100], file[1000]; 34 | 35 | bzero(&serverAddress, sizeof(serverAddress)); 36 | serverAddress.sin_family = AF_INET; 37 | serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); 38 | serverAddress.sin_port = htons(PORT); 39 | 40 | connect(serverDescriptor,(struct sockaddr*)&serverAddress,sizeof(serverAddress)); 41 | 42 | while (1){ 43 | printf("File name : "); 44 | scanf("%s",buffer); 45 | send(serverDescriptor,buffer,strlen(buffer)+1,0); 46 | printf("%s\n","File Output : "); 47 | recv(serverDescriptor,&file,sizeof(file),0); 48 | printf("%s",file); 49 | } 50 | return 0; 51 | } -------------------------------------------------------------------------------- /08-file-transfer-protocol/server.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Title : File Transfer Protocol 3 | * Name : Aditya Pratap Singh Rajput 4 | * Subject : Network Protocols And Programming using C 5 | * 6 | Note : Please consider the TYPOS in comments. 7 | Thanks. 8 | */ 9 | 10 | #include "stdio.h" 11 | #include "stdlib.h" 12 | #include "string.h" 13 | //headers for socket and related functions 14 | #include 15 | #include 16 | #include 17 | //for including structures which will store information needed 18 | #include 19 | #include 20 | //for gethostbyname 21 | #include "netdb.h" 22 | #include "arpa/inet.h" 23 | 24 | // defining constants 25 | #define PORT 9002 26 | #define BACKLOG 5 27 | int main() 28 | { 29 | int size; 30 | int socketDescriptor = socket(AF_INET, SOCK_STREAM, 0); 31 | struct sockaddr_in serverAddress, clientAddress; 32 | 33 | socklen_t clientLength; 34 | 35 | struct stat statVariable; 36 | 37 | char buffer[100], file[1000]; 38 | 39 | FILE *filePointer; 40 | bzero(&serverAddress, sizeof(serverAddress)); 41 | 42 | serverAddress.sin_family = AF_INET; 43 | serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); 44 | serverAddress.sin_port = htons(PORT); 45 | 46 | bind(socketDescriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress)); 47 | 48 | listen(socketDescriptor,BACKLOG); 49 | 50 | printf("Server has started working ..."); 51 | int clientDescriptor = accept(socketDescriptor,(struct sockaddr*)&clientAddress,&clientLength); 52 | 53 | while(1){ 54 | bzero(buffer,sizeof(buffer)); 55 | bzero(file,sizeof(file)); 56 | recv(clientDescriptor,buffer,sizeof(buffer),0); 57 | filePointer = fopen(buffer,"r"); 58 | stat(buffer,&statVariable); 59 | size=statVariable.st_size; 60 | fread(file,sizeof(file),1,filePointer); 61 | send(clientDescriptor,file,sizeof(file),0); 62 | } 63 | return 0; 64 | } -------------------------------------------------------------------------------- /08-file-transfer-protocol/text.txt: -------------------------------------------------------------------------------- 1 | this is coming from txt file. 2 | . 3 | -------------------------------------------------------------------------------- /09-remote-command-execution-udp/README.md: -------------------------------------------------------------------------------- 1 | # Instructions 2 | 3 | * If you don't have knowledge of basics of UDP, please check previous topics ```04-udp-echo-client-server``` 4 | * We are preparing to implement remote command execution. 5 | * ***Remote command execution*** : Handling command from one side and accessing it on other side (server side); 6 | * We have used ```system(buffer)``` for execution of the command line input, also remember to use ```include "stdlib.h"``` header for this function. -------------------------------------------------------------------------------- /09-remote-command-execution-udp/client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Title : Remote command execution using UDP 3 | * Name : Aditya Pratap Singh Rajput 4 | * Subject : Network Protocols And Programming using C 5 | * 6 | Note : please consider the TYPOS in comments. 7 | Thanks. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #define MAX 1000 19 | 20 | int main() 21 | { 22 | int serverDescriptor = socket(AF_INET, SOCK_DGRAM, 0); 23 | char buffer[MAX], message[MAX]; 24 | struct sockaddr_in cliaddr, serverAddress; 25 | socklen_t serverLength = sizeof(serverAddress); 26 | 27 | bzero(&serverAddress, sizeof(serverAddress)); 28 | serverAddress.sin_family = AF_INET; 29 | serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); 30 | serverAddress.sin_port = htons(9976); 31 | 32 | bind(serverDescriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress)); 33 | 34 | while (1) 35 | { 36 | printf("\nCOMMAND FOR EXECUTION ... "); 37 | fgets(buffer, sizeof(buffer), stdin); 38 | sendto(serverDescriptor, buffer, sizeof(buffer), 0, (struct sockaddr *)&serverAddress, serverLength); 39 | printf("\nData Sent !"); 40 | recvfrom(serverDescriptor, message, sizeof(message), 0, (struct sockaddr *)&serverAddress, &serverLength); 41 | printf("UDP SERVER : %s", message); 42 | } 43 | return 0; 44 | } -------------------------------------------------------------------------------- /09-remote-command-execution-udp/server.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Title : Remote command execution using UDP 3 | * Name : Aditya Pratap Singh Rajput 4 | * Subject : Network Protocols And Programming using C 5 | * 6 | Note : please consider the TYPOS in comments. 7 | Thanks. 8 | */ 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #define MAX 1000 20 | int main() 21 | { 22 | 23 | int serverDescriptor = socket(AF_INET, SOCK_DGRAM, 0); 24 | int size; 25 | char buffer[MAX], message[] = "Command Successfully executed !"; 26 | struct sockaddr_in clientAddress, serverAddress; 27 | 28 | socklen_t clientLength = sizeof(clientAddress); 29 | 30 | bzero(&serverAddress, sizeof(serverAddress)); 31 | serverAddress.sin_family = AF_INET; 32 | serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); 33 | serverAddress.sin_port = htons(9976); 34 | 35 | bind(serverDescriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress)); 36 | while (1) 37 | { 38 | bzero(buffer, sizeof(buffer)); 39 | recvfrom(serverDescriptor, buffer, sizeof(buffer), 0, (struct sockaddr *)&clientAddress, &clientLength); 40 | system(buffer); 41 | printf("Command Executed ... %s ", buffer); 42 | sendto(serverDescriptor, message, sizeof(message), 0, (struct sockaddr *)&clientAddress, clientLength); 43 | } 44 | close(serverDescriptor); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /10-ARP-implementation-using-UDP/README.md: -------------------------------------------------------------------------------- 1 | ## ARP implementation using UDP 2 | 3 | ARP is a request-response or request-reply protocol in which one device sends a request to another device asking for some information, to which the other device will reply with the required information. It is a message exchange pattern. ARP packets are encapsulated by link layer and are distributed only in a particular network. -------------------------------------------------------------------------------- /10-ARP-implementation-using-UDP/arp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | int main() 16 | { 17 | struct sockaddr_in sin={0}; 18 | struct arpreq myarp={{0}}; 19 | unsigned char *ptr; 20 | int sd; 21 | sin.sin_family=AF_INET; 22 | printf("Enter IP address: "); 23 | char ip[20]; 24 | scanf("%s", ip); if(inet_pton(AF_INET,ip,&sin.sin_addr)==0) { 25 | printf("IP address Entered '%s' is not valid \n",ip); 26 | exit(0); 27 | } 28 | memcpy(&myarp.arp_pa,&sin,sizeof(myarp.arp_pa)); 29 | strcpy(myarp.arp_dev,"echo"); 30 | sd=socket(AF_INET,SOCK_DGRAM,0); 31 | printf("\nSend ARP request\n"); 32 | if(ioctl(sd,SIOCGARP,&myarp)==1) 33 | { 34 | printf("No Entry in ARP cache for '%s'\n",ip); 35 | exit(0); 36 | } 37 | ptr=&myarp.arp_pa.sa_data[0]; 38 | printf("Received ARP Reply\n"); 39 | printf("\nMAC Address for '%s' : ",ip); 40 | printf("%p:%p:%p:%p:%p:%p\n",ptr,(ptr+1),(ptr+2),(ptr+3),(ptr+4),(ptr+5)); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Aditya Pratap Singh Rajput 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Packet-tracer-experiments/12-NAT-packet-tracer/EX 12 NAT.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apsrcreatix/Socket-Programming-With-C/d2ba29453841e0ba5926c057d7cd5fba7e58eb67/Packet-tracer-experiments/12-NAT-packet-tracer/EX 12 NAT.docx -------------------------------------------------------------------------------- /Packet-tracer-experiments/12-NAT-packet-tracer/NAT.pkt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apsrcreatix/Socket-Programming-With-C/d2ba29453841e0ba5926c057d7cd5fba7e58eb67/Packet-tracer-experiments/12-NAT-packet-tracer/NAT.pkt -------------------------------------------------------------------------------- /Packet-tracer-experiments/12-NAT-packet-tracer/README.md: -------------------------------------------------------------------------------- 1 | ## NAT demonstration using packet tracer 2 | 3 | Network address translation (NAT) is a method of mapping an IP address space into another by modifying network address information in the IP header of packets while they are in transit across a traffic routing device. -------------------------------------------------------------------------------- /Packet-tracer-experiments/13-VPN-packet-tacer/README.md: -------------------------------------------------------------------------------- 1 | ## VPN demonstration using Packet tracer 2 | 3 | A virtual private network extends a private network across a public network and enables users to send and receive data across shared or public networks as if their computing devices were directly connected to the private network -------------------------------------------------------------------------------- /Packet-tracer-experiments/13-VPN-packet-tacer/VPN.pkt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apsrcreatix/Socket-Programming-With-C/d2ba29453841e0ba5926c057d7cd5fba7e58eb67/Packet-tracer-experiments/13-VPN-packet-tacer/VPN.pkt -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Learn Network Protocol and Programming Using **C** 2 | 3 | ## Prerequisite 4 | 5 | * Basics of computer networks 6 | * Intermediate in C language 7 | 8 | ## Purpose 9 | 10 | The Purpose of learning this course is to understand the various network layer, transport layer and application layer protocols and it also helps to design and implement the protocols using socking programming. 11 | 12 | ### List Of Experiment 13 | 14 | 1. Study of necessary header files with respect to socket programming. 15 | 2. Study of Basic Functions of Socket Programming. 16 | 3. Simple TCP/IP Client Server Communication. 17 | 4. UDP Echo Client Server Communication. 18 | 5. Concurrent TCP/IP Day-Time Server. 19 | 6. Half Duplex Chat Using TCP/IP. 20 | 7. Full Duplex Chat Using TCP/IP. 21 | 8. Implementation of File Transfer Protocol. 22 | 9. Remote Command Execution Using UDP. 23 | 10. Arp Implementation Using UDP. 24 | 25 | ### Tips for using the repository 26 | 27 | * Use ```make``` . Example : ```make fileName``` (without extention C) 28 | * It will make server and client file for you. 29 | * Test using ```./server``` in a terminal separately and ```./client``` in a different terminal. 30 | 31 | ### Reference 32 | 33 | [Reference for socket](https://linux.die.net/man/7/socket) 34 | --------------------------------------------------------------------------------