Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitsalla committed Nov 16, 2023
0 parents commit 530ee81
Show file tree
Hide file tree
Showing 30 changed files with 1,043 additions and 0 deletions.
132 changes: 132 additions & 0 deletions 03-tcp-ip-client-server/README.md
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
//for socket and related functions
#include <sys/types.h>
#include <sys/socket.h>
//for including structures which will store information needed
#include <netinet/in.h>
#include <unistd.h>

//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<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <unistd.h>
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;
}
```
42 changes: 42 additions & 0 deletions 03-tcp-ip-client-server/client.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
//for socket and related functions
#include <sys/types.h>
#include <sys/socket.h>
//for including structures which will store information needed
#include <netinet/in.h>
#include <unistd.h>
#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;
}
47 changes: 47 additions & 0 deletions 03-tcp-ip-client-server/server.c
Original file line number Diff line number Diff line change
@@ -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<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <unistd.h>

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;
}
49 changes: 49 additions & 0 deletions 04-udp-echo-client-server/README.md
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions 04-udp-echo-client-server/client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Title : echo client
* Name : Aditya Pratap Singh Rajput
* Subject : Network Protocols And Programming using C
*
* */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#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;
}
48 changes: 48 additions & 0 deletions 04-udp-echo-client-server/server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Title : echo server
* Name : Aditya Pratap Singh Rajput
* Subject : Network Protocols And Programming using C
*
* */
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
// 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;
}
5 changes: 5 additions & 0 deletions 05-day-time-server-tcp-ip/README.md
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 530ee81

Please sign in to comment.