diff --git a/03-tcp-ip-client-server/README.md b/03-tcp-ip-client-server/README.md new file mode 100644 index 0000000..bfe40c7 --- /dev/null +++ b/03-tcp-ip-client-server/README.md @@ -0,0 +1,132 @@ +# Socket in C + +_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._ + +## Client Socket Workflow + +**socket() +| +connect() +| +recv()** + +* 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. + +* 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. + +## Server Socket Workflow + +* 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. + +**socket() +| +bind() +| +listen() +| +accept()** + +### Understanding Client Side Code + +```C +//adding includes +#include +#include +//for socket and related functions +#include +#include +//for including structures which will store information needed +#include +#include + +//main functions +int main(){ + +//creating a socket +int network_socket; +//calling socket function and storing the result in the variable +//socket(domainOfTheSocket,TypeOfTheSocket,FlagForProtocol{0 for default protocol i.e, TCP}) +//AF_INET = constant defined in the header file for us +//TCP vs UDP --- SOCK_STREAM for TCP +// flag 0 for TCP (default protocol) +network_socket = socket(AF_INET,SOCK_STREAM,0); +//creating network connection +//in order to connect to the other side of the socket we need to declare connect +//with specifying address where we want to connect to +//already defined struct sockaddr_in +struct sockaddr_in server_address; +//what type of address we are woking with +server_address.sin_family = AF_INET; +//for taking the port number and htons converts the port # to the appropriate data type we want to write +//to specifying the port +//htons : conversion functions + +server_address.sin_port = htons(9002); +//structure within structure A.B.c +server_address.sin_addr.s_addr = INADDR_ANY; //INADDR_ANY is for connection with 0000 +//connect returns us a response that connection is establlised or not +int connect_status = connect(network_socket,(struct sockaddr *) &server_address, sizeof(server_address)); +//check for the error with the connection + +if (connect_status == -1){ +printf("There was an error making a connection to socket\n" ); +} + +//recieve data from the server +char server_response[256]; + +//recieve the data from the server +recv(network_socket,&server_response,sizeof(server_response),0); + +//recieved data from the server successfully then printing the data obtained from the server + +printf("Ther server sent the data : %s",server_response); + +//closing the socket +close(network_socket); +return 0; +} +``` + +### Understanding Server Side Code + +```C +#include +#include +#include +#include +#include +#include + +int main(){ + +char server_message[256] = "You have a missed call from server"; +//create the server socket +int server_socket; +server_socket = socket(AF_INET,SOCK_STREAM,0); +//define the server address +//creating the address as same way we have created for TCPclient + +struct sockaddr_in server_address; +server_address.sin_family = AF_INET; +server_address.sin_port = htons(9002); +server_address.sin_addr.s_addr = INADDR_ANY; + +//calling bind function to oir specified IP and port +bind(server_socket,(struct sockaddr*) &server_address,sizeof(server_address)); + +listen(server_socket,5); + +//starting the accepting +int client_socket; +//accept(socketWeAreAccepting,structuresClientIsConnectingFrom,) +client_socket = accept(server_socket,NULL,NULL); +//sending data +//send(toWhom,Message,SizeOfMessage,FLAG); +send(client_socket,server_message,sizeof(server_message),0); + +//close the socket +close(server_socket); +return 0; +} +``` \ No newline at end of file diff --git a/03-tcp-ip-client-server/client.c b/03-tcp-ip-client-server/client.c new file mode 100644 index 0000000..b684844 --- /dev/null +++ b/03-tcp-ip-client-server/client.c @@ -0,0 +1,42 @@ +/* +Creating the TCP Client workflow in this program using C. + + * Title : TCP client + * Name : Aditya Pratap Singh Rajput + * Subject : Network Protocols And Programming using C +Note : please consider the TYPOS in comments. +Thanks. +*/ + +// adding headers +#include +#include +//for socket and related functions +#include +#include +//for including structures which will store information needed +#include +#include +#define SIZE 1000 + +//main functions +int main() +{ + int socketDescriptor = socket(AF_INET, SOCK_STREAM, 0); +// server address + struct sockaddr_in serverAddress; + serverAddress.sin_family = AF_INET; + serverAddress.sin_port = htons(9002); + serverAddress.sin_addr.s_addr = INADDR_ANY; + +// communicates with listen + connect(socketDescriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress)); + + char serverResponse[SIZE]; + recv(socketDescriptor, &serverResponse, sizeof(serverResponse), 0); + printf("Ther server sent the data : %s", serverResponse); + + //closing the socket + close(socketDescriptor); + return 0; +} diff --git a/03-tcp-ip-client-server/server.c b/03-tcp-ip-client-server/server.c new file mode 100644 index 0000000..0110bf4 --- /dev/null +++ b/03-tcp-ip-client-server/server.c @@ -0,0 +1,47 @@ +/* +Creating the TCP socket workflow in this program using C. + + * Title : TCP client + * Name : Aditya Pratap Singh Rajput + * Subject : Network Protocols And Programming using C +Note : please consider the TYPOS in comments. +Thanks. +*/ +#include +#include +#include +#include +#include +#include + +int main(){ + + char serverMessage[256] = "You have a missed call from server\n"; + + //create the server socket + int socketDescriptor = socket(AF_INET,SOCK_STREAM,0); + + //define the server address + //creating the address as same way we have created for TCPclient + struct sockaddr_in serverAddress; + serverAddress.sin_family = AF_INET; + serverAddress.sin_port = htons(9002); + serverAddress.sin_addr.s_addr = INADDR_ANY; + + //calling bind function to oir specified IP and port + bind(socketDescriptor,(struct sockaddr*) &serverAddress,sizeof(serverAddress)); + + listen(socketDescriptor,5); + + //starting the accepting + //accept(socketWeAreAccepting,structuresClientIsConnectingFrom,) + int client_socket = accept(socketDescriptor, NULL, NULL); + + //sending data + //send(toWhom,Message,SizeOfMessage,FLAG); + send(client_socket,serverMessage,sizeof(serverMessage),0); + + //close the socket + close(socketDescriptor); + return 0; +} diff --git a/04-udp-echo-client-server/README.md b/04-udp-echo-client-server/README.md new file mode 100644 index 0000000..45ded11 --- /dev/null +++ b/04-udp-echo-client-server/README.md @@ -0,0 +1,49 @@ +# UDP ECHO SERVER + +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. + +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. + +Once it has obtained permission, the echo server process enters an infiite loop that has three steps: + +1. wait for a datagram to amve at the echo port, +2. reverse the source and destination addresses(including source and destination IP addresses as well as UDP port ids) and +3. return the datagram to its original sender. + +A UDP message to the UDP echo server, and awaits the reply. +The client expects to receive back exactly the same data as it sent. +The UDP echo service illustrates two important points that are generally true about +client-server interaction. The first concerns the difference between the lifetime of servers and clients: + +A server starts execution before interaction begins and (usually) continues to accept requests and send responses without ever terminating. + +A server waits for requests at a well-known port that has been +reserved for the service it offers. A client allocates an arbitrary, +unused nonreservedport for its communication. + +## Who would use an echo service '?' + +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 +to determineif it is possible to reach a remote machine. + +## Learn about code + +* For implementation of UDP use SOCK_DGRAM + +```C + int serverDescriptor = socket(AF_INET, SOCK_DGRAM, 0); +``` + +* For sending message use sendto function + +```C + sendto(serverDescriptor,sendMessage,MAXLINE,0,(struct sockaddr*)&serverAddress,addressLength); +``` + +* For recieving message use recv + +```C + recvfrom(socketDescriptor,message,MAXLINE,0,(struct sockaddr*)&clientAddress,&addressLength); +``` + +* On server side only use ```bind()``` function \ No newline at end of file diff --git a/04-udp-echo-client-server/client.c b/04-udp-echo-client-server/client.c new file mode 100644 index 0000000..1e3adc1 --- /dev/null +++ b/04-udp-echo-client-server/client.c @@ -0,0 +1,49 @@ +/** + * Title : echo client + * Name : Aditya Pratap Singh Rajput + * Subject : Network Protocols And Programming using C + * + * */ + +#include +#include +#include +#include +#include +#include + +#define MAXLINE 1024 +#define PORT 5035 + +int main(){ + // socket descriptor creation in udp mode + int serverDescriptor = socket(AF_INET, SOCK_DGRAM, 0); + + // for storing address of address + socklen_t addressLength; + + // preparing message + char sendMessage[MAXLINE],recvMessage[MAXLINE]; + printf("\nEnter message :"); + fgets(sendMessage,sizeof(sendMessage),stdin); + + // storing address in serverAddress + struct sockaddr_in serverAddress; + serverAddress.sin_family = AF_INET; + serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); + serverAddress.sin_port = htons(PORT); + + // storing address size + addressLength = sizeof(serverAddress); + + // checking connection + connect(serverDescriptor,(struct sockaddr*)&serverAddress,addressLength); + + // sending and receiving the messages + sendto(serverDescriptor,sendMessage,MAXLINE,0,(struct sockaddr*)&serverAddress,addressLength); + recvfrom(serverDescriptor,recvMessage,MAXLINE,0,NULL,NULL); + + printf("\nServer's Echo : %s\n",recvMessage); + + return 0; +} diff --git a/04-udp-echo-client-server/server.c b/04-udp-echo-client-server/server.c new file mode 100644 index 0000000..248c098 --- /dev/null +++ b/04-udp-echo-client-server/server.c @@ -0,0 +1,48 @@ +/** + * Title : echo server + * Name : Aditya Pratap Singh Rajput + * Subject : Network Protocols And Programming using C + * + * */ +#include +#include +#include +#include +#include +#include +// time + +#define MAXLINE 1024 +#define PORT 5035 + +int main(){ + + int socketDescriptor = socket(AF_INET, SOCK_DGRAM, 0); + int number; + socklen_t addressLength; + char message[MAXLINE]; + + struct sockaddr_in serverAddress,clientAddress; + serverAddress.sin_family = AF_INET; + serverAddress.sin_addr.s_addr=INADDR_ANY; + serverAddress.sin_port=htons(PORT); + + bind(socketDescriptor,(struct sockaddr*)&serverAddress,sizeof(serverAddress)); + + printf("\nServer Started ...\n"); + + while(1){ + printf("\n"); + addressLength = sizeof(clientAddress); + + number = recvfrom(socketDescriptor,message,MAXLINE,0,(struct sockaddr*)&clientAddress,&addressLength); + + printf("\n Client's Message: %s ",message); + + if(number<6) + perror("send error"); + + sendto(socketDescriptor,message,number,0,(struct sockaddr*)&clientAddress,addressLength); + } + return 0; +} diff --git a/05-day-time-server-tcp-ip/README.md b/05-day-time-server-tcp-ip/README.md new file mode 100644 index 0000000..fc56be9 --- /dev/null +++ b/05-day-time-server-tcp-ip/README.md @@ -0,0 +1,5 @@ +# Instructions + +* If you are not aware of TCP functioning please check previous topics ```03-tcp-ip-client-server``` +* We have inserted sending part i.e, ```send()``` in the ```while()``` loop for making server running throughout the process +* Receiving current time using ```ctime(addressOfVariable)``` function and sending it to the client side \ No newline at end of file diff --git a/05-day-time-server-tcp-ip/client.c b/05-day-time-server-tcp-ip/client.c new file mode 100644 index 0000000..1b53520 --- /dev/null +++ b/05-day-time-server-tcp-ip/client.c @@ -0,0 +1,37 @@ +/** + * Title : Day-Time client + * Name : Aditya Pratap Singh Rajput + * Subject : Network Protocols And Programming using C + * + * */ + +#include +#include +#include +#include +#include +#include "string.h" + +#define PORT 9002 //the port users will be connecting to +#define MAXLINE 30 //for buffer size + +int main(){ + + int socket_descriptor = socket(AF_INET, SOCK_STREAM, 0); + + char serverResponse[MAXLINE]; + + struct sockaddr_in serverAddress; + + serverAddress.sin_family = AF_INET; + serverAddress.sin_addr.s_addr = INADDR_ANY; + serverAddress.sin_port = htons(PORT); + +connect(socket_descriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress)); + +recv(socket_descriptor,serverResponse,MAXLINE-1,0); + + printf("\nTIME FROM SERVER %s\n",serverResponse); + + return 0; +} diff --git a/05-day-time-server-tcp-ip/server.c b/05-day-time-server-tcp-ip/server.c new file mode 100644 index 0000000..04014cb --- /dev/null +++ b/05-day-time-server-tcp-ip/server.c @@ -0,0 +1,59 @@ +/** + * Title : Day-Time server + * Name : Aditya Pratap Singh Rajput + * Subject : Network Protocols And Programming using C + * + * */ + +//program for date time server +#include +#include +#include +#include +#include +#include +#include + +//port value and the max size for buffer +#define PORT 9002 //the port users will be connecting to +#define BACKLOG 10 //how many pending connections queue will hold + +int main(){ + //*******************Time Setup*********************** + time_t currentTime ; + time(¤tTime); + //**************************************************** + + int countClient = 0; + + int socket_descriptor = socket(AF_INET, SOCK_STREAM, 0); + + struct sockaddr_in serverAddress; + serverAddress.sin_family = AF_INET; + serverAddress.sin_addr.s_addr=INADDR_ANY; + serverAddress.sin_port=htons(PORT); + +//binding address +bind(socket_descriptor,(struct sockaddr*)&serverAddress,sizeof(serverAddress)); + +//listening to the queue + listen(socket_descriptor,BACKLOG); + + printf("\nServer Started ..."); + +while(1){ + countClient++; + printf("\n"); + + int client_socket = accept(socket_descriptor, NULL, NULL); + // char *string = asctime(timeinfo); + + printf("\nClient %d has requested for time at %s", countClient, ctime(¤tTime)); + + //sending time to the client side + // ctime(&time_from_pc) + send(client_socket,ctime(¤tTime), 30, 0); + + } + return 0; +} diff --git a/06-half-duplex-chat-tcp-ip/README.md b/06-half-duplex-chat-tcp-ip/README.md new file mode 100644 index 0000000..83f9955 --- /dev/null +++ b/06-half-duplex-chat-tcp-ip/README.md @@ -0,0 +1,5 @@ +# Instructions + +* If you don't have knowledge of basics of TCP, please check previous topics ```03-tcp-ip-client-server``` +* We are preparing half duplex communication method +* ***Half Duplex*** : Refers to the transmission of data in just one direction at a time. \ No newline at end of file diff --git a/06-half-duplex-chat-tcp-ip/client.c b/06-half-duplex-chat-tcp-ip/client.c new file mode 100644 index 0000000..17abb52 --- /dev/null +++ b/06-half-duplex-chat-tcp-ip/client.c @@ -0,0 +1,73 @@ +/* + * Title : Half duplex client side + * Name : Aditya Pratap Singh Rajput + * Subject : Network Protocols And Programming using C + * +Note : please consider the TYPOS in comments. +Thanks. +*/ + +#include "stdio.h" +#include "stdlib.h" +#include "string.h" +//headers for socket and related functions +#include +#include +//for including structures which will store information needed +#include +#include +//for gethostbyname +#include "netdb.h" +#include "arpa/inet.h" + +//defines +#define h_addr h_addr_list[0] /* for backward compatibility */ + +#define PORT 9002 // port number +#define MAX 1000 //maximum buffer size + +//main function +int main(){ + char serverResponse[MAX]; + char clientResponse[MAX]; + + //creating a socket + int socketDescriptor = socket(AF_INET, SOCK_STREAM, 0); + + //placeholder for the hostname and my ip address + char hostname[MAX], ipaddress[MAX]; +struct hostent *hostIP; //placeholder for the ip address +//if the gethostname returns a name then the program will get the ip address +if(gethostname(hostname,sizeof(hostname))==0){ + hostIP = gethostbyname(hostname);//the netdb.h fucntion gethostbyname +}else{ +printf("ERROR:FCC4539 IP Address Not "); +} + +struct sockaddr_in serverAddress; +serverAddress.sin_family = AF_INET; +serverAddress.sin_port = htons(PORT); +serverAddress.sin_addr.s_addr = INADDR_ANY; + +connect(socketDescriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress)); + +// getting the address port and remote host + printf("\nLocalhost: %s\n", inet_ntoa(*(struct in_addr*)hostIP->h_addr)); + printf("Local Port: %d\n", PORT); + printf("Remote Host: %s\n", inet_ntoa(serverAddress.sin_addr)); + + while (1) + { //recieve the data from the server + recv(socketDescriptor, serverResponse, sizeof(serverResponse), 0); + //recieved data from the server successfully then printing the data obtained from the server + printf("\nSERVER : %s", serverResponse); + + printf("\ntext message here... :"); + scanf("%s", clientResponse); + send(socketDescriptor, clientResponse, sizeof(clientResponse), 0); + } + + //closing the socket + close(socketDescriptor); + return 0; +} \ No newline at end of file diff --git a/06-half-duplex-chat-tcp-ip/server.c b/06-half-duplex-chat-tcp-ip/server.c new file mode 100644 index 0000000..c2d1d2e --- /dev/null +++ b/06-half-duplex-chat-tcp-ip/server.c @@ -0,0 +1,59 @@ +/* + * Title : Half duplex server side + * Name : Aditya Pratap Singh Rajput + * Subject : Network Protocols And Programming using C + * +Note : please consider the TYPOS in comments. +Thanks. +*/ + +#include "stdio.h" +#include "stdlib.h" +#include "string.h" +//headers for socket and related functions +#include +#include +//for including structures which will store information needed +#include +#include +//for gethostbyname +#include "netdb.h" +#include "arpa/inet.h" +#define MAX 1000 +#define BACKLOG 5 // how many pending connections queue will hold +int main() +{ + char serverMessage[MAX]; + char clientMessage[MAX]; + //create the server socket + int socketDescriptor = socket(AF_INET, SOCK_STREAM, 0); + + + struct sockaddr_in serverAddress; + serverAddress.sin_family = AF_INET; + serverAddress.sin_port = htons(9002); + serverAddress.sin_addr.s_addr = INADDR_ANY; + + //calling bind function to oir specified IP and port + bind(socketDescriptor, (struct sockaddr*)&serverAddress, sizeof(serverAddress)); + + listen(socketDescriptor, BACKLOG); + + //starting the accepting + int clientSocketDescriptor = accept(socketDescriptor, NULL, NULL); + + while (1) + { + printf("\ntext message here .. :"); + scanf("%s", serverMessage); + send(clientSocketDescriptor, serverMessage, sizeof(serverMessage) , 0); + //recieve the data from the server + recv(clientSocketDescriptor, &clientMessage, sizeof(clientMessage), 0) ; + //recieved data from the server successfully then printing the data obtained from the server + printf("\nCLIENT: %s", clientMessage); + + } + //close the socket + close(socketDescriptor); + return 0; +} diff --git a/07-full-duplex-chat-tcp-ip/README.md b/07-full-duplex-chat-tcp-ip/README.md new file mode 100644 index 0000000..ad221d1 --- /dev/null +++ b/07-full-duplex-chat-tcp-ip/README.md @@ -0,0 +1,6 @@ +# Instructions + +* If you don't have knowledge of basics of TCP, please check previous topics ```03-tcp-ip-client-server``` +* We are preparing full duplex communication method +* ***Full Duplex*** : Refers to the transmission of data in both direction at any time. +* We are using ```fork()``` function for making a child process and then switching between the recv and send mode. \ No newline at end of file diff --git a/07-full-duplex-chat-tcp-ip/client.c b/07-full-duplex-chat-tcp-ip/client.c new file mode 100644 index 0000000..e7431df --- /dev/null +++ b/07-full-duplex-chat-tcp-ip/client.c @@ -0,0 +1,69 @@ +/* + * Title : Full duplex client side + * Name : Aditya Pratap Singh Rajput + * Subject : Network Protocols And Programming using C +Note : please consider the TYPOS in comments. +Thanks. +*/ + +#include "stdio.h" +#include "stdlib.h" +#include "string.h" +//headers for socket and related functions +#include +#include +//for including structures which will store information needed +#include +#include +//for gethostbyname +#include "netdb.h" +#include "arpa/inet.h" + +int main() +{ +int socketDescriptor; + +struct sockaddr_in serverAddress; +char sendBuffer[1000],recvBuffer[1000]; + +pid_t cpid; + +bzero(&serverAddress,sizeof(serverAddress)); + +serverAddress.sin_family=AF_INET; +serverAddress.sin_addr.s_addr=inet_addr("127.0.0.1"); +serverAddress.sin_port=htons(5500); + +/*Creating a socket, assigning IP address and port number for that socket*/ +socketDescriptor=socket(AF_INET,SOCK_STREAM,0); + +/*Connect establishes connection with the server using server IP address*/ +connect(socketDescriptor,(struct sockaddr*)&serverAddress,sizeof(serverAddress)); + +/*Fork is used to create a new process*/ +cpid=fork(); +if(cpid==0) +{ +while(1) +{ +bzero(&sendBuffer,sizeof(sendBuffer)); +printf("\nType a message here ... "); +/*This function is used to read from server*/ +fgets(sendBuffer,10000,stdin); +/*Send the message to server*/ +send(socketDescriptor,sendBuffer,strlen(sendBuffer)+1,0); +printf("\nMessage sent !\n"); +} +} +else +{ +while(1) +{ +bzero(&recvBuffer,sizeof(recvBuffer)); +/*Receive the message from server*/ +recv(socketDescriptor,recvBuffer,sizeof(recvBuffer),0); +printf("\nSERVER : %s\n",recvBuffer); +} +} +return 0; +} diff --git a/07-full-duplex-chat-tcp-ip/server.c b/07-full-duplex-chat-tcp-ip/server.c new file mode 100644 index 0000000..0329e68 --- /dev/null +++ b/07-full-duplex-chat-tcp-ip/server.c @@ -0,0 +1,74 @@ + +/* + * Title : Full duplex server side + * Name : Aditya Pratap Singh Rajput + * Subject : Network Protocols And Programming using C + * +Note : please consider the TYPOS in comments. +Thanks. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc,char *argv[]) +{ +int clientSocketDescriptor,socketDescriptor; + +struct sockaddr_in serverAddress,clientAddress; +socklen_t clientLength; + +char recvBuffer[1000],sendBuffer[1000]; +pid_t cpid; +bzero(&serverAddress,sizeof(serverAddress)); +/*Socket address structure*/ +serverAddress.sin_family=AF_INET; +serverAddress.sin_addr.s_addr=htonl(INADDR_ANY); +serverAddress.sin_port=htons(5500); +/*TCP socket is created, an Internet socket address structure is filled with +wildcard address & server’s well known port*/ +socketDescriptor=socket(AF_INET,SOCK_STREAM,0); +/*Bind function assigns a local protocol address to the socket*/ +bind(socketDescriptor,(struct sockaddr*)&serverAddress,sizeof(serverAddress)); +/*Listen function specifies the maximum number of connections that kernel should queue +for this socket*/ +listen(socketDescriptor,5); +printf("%s\n","Server is running ..."); +/*The server to return the next completed connection from the front of the +completed connection Queue calls it*/ +clientSocketDescriptor=accept(socketDescriptor,(struct sockaddr*)&clientAddress,&clientLength); +/*Fork system call is used to create a new process*/ +cpid=fork(); + +if(cpid==0) +{ +while(1) +{ +bzero(&recvBuffer,sizeof(recvBuffer)); +/*Receiving the request from client*/ +recv(clientSocketDescriptor,recvBuffer,sizeof(recvBuffer),0); +printf("\nCLIENT : %s\n",recvBuffer); +} +} +else +{ +while(1) +{ + +bzero(&sendBuffer,sizeof(sendBuffer)); +printf("\nType a message here ... "); +/*Read the message from client*/ +fgets(sendBuffer,10000,stdin); +/*Sends the message to client*/ +send(clientSocketDescriptor,sendBuffer,strlen(sendBuffer)+1,0); +printf("\nMessage sent !\n"); +} +} +return 0; +} diff --git a/08-file-transfer-protocol/README.md b/08-file-transfer-protocol/README.md new file mode 100644 index 0000000..35cd65b --- /dev/null +++ b/08-file-transfer-protocol/README.md @@ -0,0 +1,5 @@ +# Instructions + +* If you don't have knowledge of basics of TCP, please check previous topics ```03-tcp-ip-client-server``` +* We are preparing to implement sample FTP program. +* ***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. \ No newline at end of file diff --git a/08-file-transfer-protocol/client.c b/08-file-transfer-protocol/client.c new file mode 100644 index 0000000..12edf08 --- /dev/null +++ b/08-file-transfer-protocol/client.c @@ -0,0 +1,51 @@ +/* + * Title : File Transfer Protocol + * Name : Aditya Pratap Singh Rajput + * Subject : Network Protocols And Programming using C + * +Note : Please consider the TYPOS in comments. +Thanks. +*/ + +#include "stdio.h" +#include "stdlib.h" +#include "string.h" +//headers for socket and related functions +#include +#include +#include +//for including structures which will store information needed +#include +#include +//for gethostbyname +#include "netdb.h" +#include "arpa/inet.h" + +// defining constants +#define PORT 9002 + +int main() +{ + + int serverDescriptor = socket(AF_INET, SOCK_STREAM, 0); + struct sockaddr_in serverAddress; + + char buffer[100], file[1000]; + + bzero(&serverAddress, sizeof(serverAddress)); + serverAddress.sin_family = AF_INET; + serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); + serverAddress.sin_port = htons(PORT); + + connect(serverDescriptor,(struct sockaddr*)&serverAddress,sizeof(serverAddress)); + + while (1){ + printf("File name : "); + scanf("%s",buffer); + send(serverDescriptor,buffer,strlen(buffer)+1,0); + printf("%s\n","File Output : "); + recv(serverDescriptor,&file,sizeof(file),0); + printf("%s",file); + } + return 0; +} \ No newline at end of file diff --git a/08-file-transfer-protocol/server.c b/08-file-transfer-protocol/server.c new file mode 100644 index 0000000..ce23f57 --- /dev/null +++ b/08-file-transfer-protocol/server.c @@ -0,0 +1,64 @@ +/* + * Title : File Transfer Protocol + * Name : Aditya Pratap Singh Rajput + * Subject : Network Protocols And Programming using C + * +Note : Please consider the TYPOS in comments. +Thanks. +*/ + +#include "stdio.h" +#include "stdlib.h" +#include "string.h" +//headers for socket and related functions +#include +#include +#include +//for including structures which will store information needed +#include +#include +//for gethostbyname +#include "netdb.h" +#include "arpa/inet.h" + +// defining constants +#define PORT 9002 +#define BACKLOG 5 +int main() +{ + int size; + int socketDescriptor = socket(AF_INET, SOCK_STREAM, 0); + struct sockaddr_in serverAddress, clientAddress; + + socklen_t clientLength; + + struct stat statVariable; + + char buffer[100], file[1000]; + + FILE *filePointer; + bzero(&serverAddress, sizeof(serverAddress)); + + serverAddress.sin_family = AF_INET; + serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); + serverAddress.sin_port = htons(PORT); + + bind(socketDescriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress)); + + listen(socketDescriptor,BACKLOG); + + printf("Server has started working ..."); + int clientDescriptor = accept(socketDescriptor,(struct sockaddr*)&clientAddress,&clientLength); + + while(1){ + bzero(buffer,sizeof(buffer)); + bzero(file,sizeof(file)); + recv(clientDescriptor,buffer,sizeof(buffer),0); + filePointer = fopen(buffer,"r"); + stat(buffer,&statVariable); + size=statVariable.st_size; + fread(file,sizeof(file),1,filePointer); + send(clientDescriptor,file,sizeof(file),0); + } + return 0; +} \ No newline at end of file diff --git a/08-file-transfer-protocol/text.txt b/08-file-transfer-protocol/text.txt new file mode 100644 index 0000000..9ee2738 --- /dev/null +++ b/08-file-transfer-protocol/text.txt @@ -0,0 +1,2 @@ +this is coming from txt file. +. diff --git a/09-remote-command-execution-udp/README.md b/09-remote-command-execution-udp/README.md new file mode 100644 index 0000000..66f85c2 --- /dev/null +++ b/09-remote-command-execution-udp/README.md @@ -0,0 +1,6 @@ +# Instructions + +* If you don't have knowledge of basics of UDP, please check previous topics ```04-udp-echo-client-server``` +* We are preparing to implement remote command execution. +* ***Remote command execution*** : Handling command from one side and accessing it on other side (server side); +* We have used ```system(buffer)``` for execution of the command line input, also remember to use ```include "stdlib.h"``` header for this function. \ No newline at end of file diff --git a/09-remote-command-execution-udp/client.c b/09-remote-command-execution-udp/client.c new file mode 100644 index 0000000..12cfbec --- /dev/null +++ b/09-remote-command-execution-udp/client.c @@ -0,0 +1,44 @@ +/* + * Title : Remote command execution using UDP + * Name : Aditya Pratap Singh Rajput + * Subject : Network Protocols And Programming using C + * +Note : please consider the TYPOS in comments. +Thanks. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#define MAX 1000 + +int main() +{ + int serverDescriptor = socket(AF_INET, SOCK_DGRAM, 0); + char buffer[MAX], message[MAX]; + struct sockaddr_in cliaddr, serverAddress; + socklen_t serverLength = sizeof(serverAddress); + + bzero(&serverAddress, sizeof(serverAddress)); + serverAddress.sin_family = AF_INET; + serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1"); + serverAddress.sin_port = htons(9976); + + bind(serverDescriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress)); + + while (1) + { + printf("\nCOMMAND FOR EXECUTION ... "); + fgets(buffer, sizeof(buffer), stdin); + sendto(serverDescriptor, buffer, sizeof(buffer), 0, (struct sockaddr *)&serverAddress, serverLength); + printf("\nData Sent !"); + recvfrom(serverDescriptor, message, sizeof(message), 0, (struct sockaddr *)&serverAddress, &serverLength); + printf("UDP SERVER : %s", message); + } + return 0; +} \ No newline at end of file diff --git a/09-remote-command-execution-udp/server.c b/09-remote-command-execution-udp/server.c new file mode 100644 index 0000000..2daa061 --- /dev/null +++ b/09-remote-command-execution-udp/server.c @@ -0,0 +1,46 @@ +/* + * Title : Remote command execution using UDP + * Name : Aditya Pratap Singh Rajput + * Subject : Network Protocols And Programming using C + * +Note : please consider the TYPOS in comments. +Thanks. +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define MAX 1000 +int main() +{ + + int serverDescriptor = socket(AF_INET, SOCK_DGRAM, 0); + int size; + char buffer[MAX], message[] = "Command Successfully executed !"; + struct sockaddr_in clientAddress, serverAddress; + + socklen_t clientLength = sizeof(clientAddress); + + bzero(&serverAddress, sizeof(serverAddress)); + serverAddress.sin_family = AF_INET; + serverAddress.sin_addr.s_addr = htonl(INADDR_ANY); + serverAddress.sin_port = htons(9976); + + bind(serverDescriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress)); + while (1) + { + bzero(buffer, sizeof(buffer)); + recvfrom(serverDescriptor, buffer, sizeof(buffer), 0, (struct sockaddr *)&clientAddress, &clientLength); + system(buffer); + printf("Command Executed ... %s ", buffer); + sendto(serverDescriptor, message, sizeof(message), 0, (struct sockaddr *)&clientAddress, clientLength); + } + close(serverDescriptor); + return 0; +} \ No newline at end of file diff --git a/10-ARP-implementation-using-UDP/README.md b/10-ARP-implementation-using-UDP/README.md new file mode 100644 index 0000000..179b544 --- /dev/null +++ b/10-ARP-implementation-using-UDP/README.md @@ -0,0 +1,3 @@ +## ARP implementation using UDP + +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. \ No newline at end of file diff --git a/10-ARP-implementation-using-UDP/arp.c b/10-ARP-implementation-using-UDP/arp.c new file mode 100644 index 0000000..a2be2fc --- /dev/null +++ b/10-ARP-implementation-using-UDP/arp.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +int main() +{ + struct sockaddr_in sin={0}; + struct arpreq myarp={{0}}; + unsigned char *ptr; + int sd; + sin.sin_family=AF_INET; + printf("Enter IP address: "); + char ip[20]; + scanf("%s", ip); if(inet_pton(AF_INET,ip,&sin.sin_addr)==0) { + printf("IP address Entered '%s' is not valid \n",ip); + exit(0); + } + memcpy(&myarp.arp_pa,&sin,sizeof(myarp.arp_pa)); + strcpy(myarp.arp_dev,"echo"); + sd=socket(AF_INET,SOCK_DGRAM,0); + printf("\nSend ARP request\n"); + if(ioctl(sd,SIOCGARP,&myarp)==1) + { + printf("No Entry in ARP cache for '%s'\n",ip); + exit(0); + } + ptr=&myarp.arp_pa.sa_data[0]; + printf("Received ARP Reply\n"); + printf("\nMAC Address for '%s' : ",ip); + printf("%p:%p:%p:%p:%p:%p\n",ptr,(ptr+1),(ptr+2),(ptr+3),(ptr+4),(ptr+5)); + return 0; +} \ No newline at end of file diff --git a/Packet-tracer-experiments/12-NAT-packet-tracer/EX 12 NAT.docx b/Packet-tracer-experiments/12-NAT-packet-tracer/EX 12 NAT.docx new file mode 100644 index 0000000..52d5294 Binary files /dev/null and b/Packet-tracer-experiments/12-NAT-packet-tracer/EX 12 NAT.docx differ diff --git a/Packet-tracer-experiments/12-NAT-packet-tracer/NAT.pkt b/Packet-tracer-experiments/12-NAT-packet-tracer/NAT.pkt new file mode 100644 index 0000000..392b634 Binary files /dev/null and b/Packet-tracer-experiments/12-NAT-packet-tracer/NAT.pkt differ diff --git a/Packet-tracer-experiments/12-NAT-packet-tracer/README.md b/Packet-tracer-experiments/12-NAT-packet-tracer/README.md new file mode 100644 index 0000000..f1c3787 --- /dev/null +++ b/Packet-tracer-experiments/12-NAT-packet-tracer/README.md @@ -0,0 +1,3 @@ +## NAT demonstration using packet tracer + +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. \ No newline at end of file diff --git a/Packet-tracer-experiments/13-VPN-packet-tacer/README.md b/Packet-tracer-experiments/13-VPN-packet-tacer/README.md new file mode 100644 index 0000000..ff4f7a9 --- /dev/null +++ b/Packet-tracer-experiments/13-VPN-packet-tacer/README.md @@ -0,0 +1,3 @@ +## VPN demonstration using Packet tracer + +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 \ No newline at end of file diff --git a/Packet-tracer-experiments/13-VPN-packet-tacer/VPN.pkt b/Packet-tracer-experiments/13-VPN-packet-tacer/VPN.pkt new file mode 100644 index 0000000..4fa953c Binary files /dev/null and b/Packet-tracer-experiments/13-VPN-packet-tacer/VPN.pkt differ diff --git a/README.md b/README.md new file mode 100644 index 0000000..a04b772 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +### List Of Experiment + +1. Study of necessary header files with respect to socket programming. +2. Study of Basic Functions of Socket Programming. +3. Simple TCP/IP Client Server Communication. +4. UDP Echo Client Server Communication. +5. Concurrent TCP/IP Day-Time Server. +6. Half Duplex Chat Using TCP/IP. +7. Full Duplex Chat Using TCP/IP. +8. Implementation of File Transfer Protocol. +9. Remote Command Execution Using UDP. +10. Arp Implementation Using UDP. + +### Tips for using the repository + +- Test using `./server` in a terminal separately and `./client` in a different terminal. + +### Reference + +[Reference for socket](https://linux.die.net/man/7/socket)