Skip to content

Commit 8168d70

Browse files
committed
added improvements
1 parent 75aec14 commit 8168d70

File tree

26 files changed

+898
-0
lines changed

26 files changed

+898
-0
lines changed

03-tcp-ip-client-server/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Socket in C
2+
3+
Sockets are low level endpoint used for processing information across a network.
4+
common networking protocols like HTTP and FTP rely on the sockets underneath to make connection.
5+
6+
## client socket workflow
7+
8+
## Client socket workflow
9+
10+
**socket()
11+
|
12+
connect()
13+
|
14+
recv()**
15+
16+
- 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.
17+
18+
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.
19+
20+
## server socket workflow
21+
22+
on the "server" end of the socket,we need to also create a socket with a socket() call,
23+
but then,we need to bind(),that socket to an IP and port where it can then listen() for
24+
connections, and then finally accept() a connection and then send() or recv() data to the
25+
other sockets it has connected to
26+
27+
socket()
28+
|
29+
bind()
30+
|
31+
listen()
32+
|
33+
accept()
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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
7+
8+
Note : please consider the TYPOS in comments.
9+
Thanks.
10+
*/
11+
//adding includes
12+
#include <stdio.h>
13+
#include <stdlib.h>
14+
//for socket and related functions
15+
#include <sys/types.h>
16+
#include <sys/socket.h>
17+
//for including structures which will store information needed
18+
#include <netinet/in.h>
19+
20+
#include <unistd.h>
21+
//main functions
22+
int main(){
23+
//creating a socket
24+
int network_socket;
25+
//calling socket function and storing the result in the variable
26+
//socket(domainOfTheSocket,TypeOfTheSocket,FlagForProtocol{0 for default protocol i.e, TCP})
27+
//AF_INET = constant defined in the header file for us
28+
//TCP vs UDP --- SOCK_STRAM for TCP
29+
// flag 0 for TCP (default protocol)
30+
network_socket = socket(AF_INET,SOCK_STREAM,0);
31+
//creating network connection
32+
//in order to connect to the other side of the socket we need to declare connect
33+
//with specifying address where we want to connect to
34+
//already defined struct sockaddr_in
35+
struct sockaddr_in server_address;
36+
//what type of address we are woking with
37+
server_address.sin_family = AF_INET;
38+
//for taking the port number and htons converts the port # to the appropriate data type we want to write
39+
//to specifying the port
40+
//htons : conversion functions
41+
server_address.sin_port = htons(9002);
42+
//structure within structure A.B.c
43+
server_address.sin_addr.s_addr = INADDR_ANY;//INADDR_ANY is for connection with 0000
44+
//connect returns us a response that connection is establlised or not
45+
int connect_status = connect(network_socket,(struct sockaddr *) &server_address, sizeof(server_address));
46+
//check for the error with the connection
47+
if (connect_status == -1){
48+
printf("There was an error making a connection to socket\n" );
49+
}
50+
//recieve data from the server
51+
char server_response[256];
52+
//recieve the data from the server
53+
recv(network_socket,&server_response,sizeof(server_response),0);
54+
//recieved data from the server successfully then printing the data obtained from the server
55+
56+
printf("Ther server sent the data : %s",server_response);
57+
//closing the socket
58+
close(network_socket);
59+
return 0;
60+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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
7+
8+
Note : please consider the TYPOS in comments.
9+
Thanks.
10+
*/
11+
#include<stdio.h>
12+
#include<stdlib.h>
13+
#include<sys/types.h>
14+
#include<sys/socket.h>
15+
#include<netinet/in.h>
16+
#include <unistd.h>
17+
18+
int main(){
19+
char server_message[256] = "You have a missed call from server";
20+
//create the server socket
21+
int server_socket;
22+
server_socket = socket(AF_INET,SOCK_STREAM,0);
23+
//define the server address
24+
//creating the address as same way we have created for TCPclient
25+
26+
struct sockaddr_in server_address;
27+
server_address.sin_family = AF_INET;
28+
server_address.sin_port = htons(9002);
29+
server_address.sin_addr.s_addr = INADDR_ANY;
30+
31+
//calling bind function to oir specified IP and port
32+
bind(server_socket,(struct sockaddr*) &server_address,sizeof(server_address));
33+
34+
listen(server_socket,5);
35+
36+
//starting the accepting
37+
int client_socket;
38+
//accept(socketWeAreAccepting,structuresClientIsConnectingFrom,)
39+
client_socket = accept(server_socket,NULL,NULL);
40+
//sending data
41+
//send(toWhom,Message,SizeOfMessage,FLAG);
42+
send(client_socket,server_message,sizeof(server_message),0);
43+
//close the socket
44+
close(server_socket);
45+
return 0;
46+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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.

04-udp-echo-client-server/client.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Title : echo client
3+
* Name : Aditya Pratap Singh Rajput
4+
* Subject : Network Protocols And Programming
5+
*/
6+
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <sys/types.h>
10+
#include <sys/socket.h>
11+
#include <netinet/in.h>
12+
13+
#define MAXLINE 1024
14+
15+
int main(){
16+
int socket_descriptor;
17+
int n;
18+
socklen_t length;
19+
char sendline[MAXLINE],recvline[MAXLINE];
20+
struct sockaddr_in servaddr;
21+
printf("\nEnter message :");
22+
scanf("%s",sendline);
23+
socket_descriptor = socket(AF_INET,SOCK_DGRAM,0);
24+
25+
servaddr.sin_family = AF_INET;
26+
servaddr.sin_addr.s_addr = INADDR_ANY;
27+
servaddr.sin_port = htons(5035);
28+
29+
int isConnect = connect(socket_descriptor,(struct sockaddr*)&servaddr,sizeof(servaddr));
30+
31+
if(isConnect==-1){
32+
printf("\nNot connected");
33+
}
34+
35+
length = sizeof(servaddr);
36+
sendto(socket_descriptor,sendline,MAXLINE,0,(struct sockaddr*)&servaddr,length);
37+
n=recvfrom(socket_descriptor,recvline,MAXLINE,0,NULL,NULL);
38+
recvline[n]=0;
39+
printf("\nServer's Echo : %s\n",recvline);
40+
41+
return 0;
42+
}

04-udp-echo-client-server/server.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Title : echo server
3+
* Name : Aditya Pratap Singh Rajput
4+
* Subject : Network Protocols And Programming
5+
*/
6+
#include<stdio.h>
7+
#include<stdlib.h>
8+
#include<sys/types.h>
9+
#include<sys/socket.h>
10+
#include<netinet/in.h>
11+
#include<unistd.h>
12+
// time
13+
14+
#define MAXLINE 1024
15+
16+
int main(){
17+
18+
int socket_descriptor;
19+
int n;
20+
socklen_t length;
21+
char msg[MAXLINE];
22+
struct sockaddr_in servaddr,cliaddr;
23+
24+
socket_descriptor = socket(AF_INET,SOCK_DGRAM,0);
25+
26+
servaddr.sin_family = AF_INET;
27+
servaddr.sin_addr.s_addr=INADDR_ANY;
28+
servaddr.sin_port=htons(5035);
29+
30+
bind(socket_descriptor,(struct sockaddr*)&servaddr,sizeof(servaddr));
31+
32+
printf("\nServer Started ...");
33+
while(1){
34+
printf("\n");
35+
length = sizeof(cliaddr);
36+
37+
n = recvfrom(socket_descriptor,msg,MAXLINE,0,(struct sockaddr*)&cliaddr,&length);
38+
39+
printf("\n Client's Message:%s ",msg);
40+
41+
if(n<6)
42+
perror("send error");
43+
44+
sendto(socket_descriptor,msg,n,0,(struct sockaddr*)&cliaddr,length);
45+
}
46+
return 0;
47+
}

05-day-time-server-tcp-ip/README.md

Whitespace-only changes.

05-day-time-server-tcp-ip/client.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Title : Day-Time client
3+
* Name : Aditya Pratap Singh Rajput
4+
* Subject : Network Protocols And Programming
5+
*/
6+
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <sys/types.h>
10+
#include <sys/socket.h>
11+
#include <netinet/in.h>
12+
#include "string.h"
13+
14+
#define PORT 9002 //the port users will be connecting to
15+
#define MAXLINE 500 //for buffer size
16+
17+
int main(){
18+
19+
//variable for socket descriptor
20+
int socket_descriptor = socket(AF_INET, SOCK_STREAM, 0);
21+
22+
struct sockaddr_in servaddr;
23+
24+
25+
servaddr.sin_family = AF_INET;
26+
servaddr.sin_addr.s_addr = INADDR_ANY;
27+
servaddr.sin_port = htons(PORT);
28+
29+
//if not connected then print the error message
30+
if (connect(socket_descriptor, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
31+
{
32+
perror("\nNot connected");
33+
exit(0);
34+
}
35+
36+
char server_response[MAXLINE];
37+
38+
39+
40+
//if not received the data it will show the error message
41+
if(recv(socket_descriptor,server_response,MAXLINE-1,0)==-1){
42+
perror("recv");
43+
exit(0);
44+
}
45+
46+
printf("\nTIME FROM SERVER %s\n",server_response);
47+
48+
return 0;
49+
}

05-day-time-server-tcp-ip/server.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Title : Day-Time server
3+
* Name : Aditya Pratap Singh Rajput
4+
* Subject : Network Protocols And Programming
5+
*/
6+
7+
//program for date time server
8+
#include<stdio.h>
9+
#include<stdlib.h>
10+
#include<sys/types.h>
11+
#include<sys/socket.h>
12+
#include<netinet/in.h>
13+
#include<unistd.h>
14+
#include<time.h>
15+
//port value and the max size for buffer
16+
#define PORT 9002 //the port users will be connecting to
17+
#define BACKLOG 10 //how many pending connections queue will hold
18+
int main(){
19+
//*******************Time Setup***********************
20+
int counter=0;
21+
//for showing the time from the server to the client
22+
time_t time_from_pc;
23+
struct tm * timeinfo;
24+
time(&time_from_pc);
25+
timeinfo = localtime (&time_from_pc);
26+
//same as echo server
27+
//****************************************************
28+
int socket_descriptor=socket(AF_INET, SOCK_STREAM, 0);
29+
30+
31+
struct sockaddr_in servaddr;
32+
33+
34+
servaddr.sin_family = AF_INET;
35+
servaddr.sin_addr.s_addr=INADDR_ANY;
36+
servaddr.sin_port=htons(PORT);
37+
//binding address
38+
if(bind(socket_descriptor,(struct sockaddr*)&servaddr,sizeof(servaddr))==-1){
39+
perror("bind");
40+
exit(0);
41+
}
42+
//listening to the queue
43+
listen(socket_descriptor,BACKLOG);
44+
45+
printf("\nServer Started ...");
46+
47+
while(1){
48+
counter++;
49+
printf("\n");
50+
51+
int client_socket = accept(socket_descriptor, NULL, NULL);
52+
char *string = asctime(timeinfo);
53+
54+
printf("\nClient %d has requested for time at %s",counter,asctime(timeinfo));
55+
//sending time to the client side
56+
if (send(client_socket, ctime(&time_from_pc), 30, 0) == -1)
57+
perror("send");
58+
59+
}
60+
return 0;
61+
}

06-half-duplex-chat-tcp-ip/README.md

Whitespace-only changes.

0 commit comments

Comments
 (0)