0% found this document useful (0 votes)
2 views4 pages

Socket Programming Using Tcp

The document outlines an experiment on implementing client-server communication using TCP socket programming. It details the theory behind TCP, the steps for establishing a TCP socket on both the server and client sides, and provides algorithms and sample code for both. The result confirms successful implementation and verification of the communication process.

Uploaded by

Sreekutty M B
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Socket Programming Using Tcp

The document outlines an experiment on implementing client-server communication using TCP socket programming. It details the theory behind TCP, the steps for establishing a TCP socket on both the server and client sides, and provides algorithms and sample code for both. The result confirms successful implementation and verification of the communication process.

Uploaded by

Sreekutty M B
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Experiment no: 2.

1
Date: 06.03.2023

Socket programming using TCP


Aim
Implement Client-Server communication using Socket Programming and TCP as transport layer protocol.

Theory
TCP provides a connection oriented service, since it is based on connections between clients and servers.
TCP provides reliability. When a TCP client send data to the server, it requires an acknowledgement in re-
turn. If an acknowledgement is not received, TCP automatically re-transmit the data and waits for a longer
period of time. TCP is instead a byte-stream protocol, without any boundaries at all. TCP is described in
RFC 793, RFC 1323, RFC 2581 and RFC 3390.

The steps involved in establishing a TCP socket on the server side are as follows:
• Create a socket with the socket() function.
• Bind the socket to an address using the bind() function.

• Listen for connections with the listen() function.


• Accept a connection with the accept() function system call. This call typically blocks until a client
connects with the server.
• Send and receive data by means of send() and receive().

The steps involved in establishing a TCP socket on the client side are as follows:
• Create a socket using the socket() function.
• Connect the socket to the address of the server using the connect() function.
• Send and receive data by means of the read() and write() functions.

Algorithm
Server
1. Initialise IP address and port number.

2. Create a TCP socket using socket() system call.

3. If socket() returns negative integer, print error and go to step 14.

4. Print "Server socket created!".

5. Bind the local address and port using bind() system call.

6. If bind returns negative integer, print "bind error" and go to step 14.

7. Listen to client using listen() system call.

8. Accept connection from client using accept() system call.

9. Print "Client connected".

24
10. Receive message sent by client using recv() system call.

11. Print the received message.

12. Send reply using send() system call.

13. Close the connection using close() system call and go to step 8.

14. Exit.

Client
1. Initialise IP address and port number.

2. Create a socket using socket() system call.

3. Print "TCP socket created".

4. Connect to the listening socket using connect() system call.

5. Print "Connected to the server".

6. Send a message to the server using send() system call.

7. Receive reply using recv() system call.

8. Print the received message.

9. Close the connection.

10. Print "Disconnected from server".

11. Exit

Program
Server
1 # include < stdio .h >
2 # include < stdlib .h >
3 # include < string .h >
4 # include < unistd .h >
5 # include < arpa / inet .h >
6 void main () {
7 char * ip = " [Link] " ;
8 int port =5566;
9 int server_sock , client_sock ;
10 struct sockaddr_in server_addr , client_addr ;
11 socklen_t addr_size ;
12 char buffer [1024];
13 int n ;
14 server_sock = socket ( AF_INET , SOCK_STREAM ,0) ;
15 if ( server_sock <0) {
16 perror ( " [ -] Socket error " ) ;
17 exit (1) ;
18 }
19 printf ( " [+] TCP server socket created .\ n " ) ;
20 memset (& server_addr , ’ \0 ’ , sizeof ( server_addr ) ) ;
21 server_addr . sin_family = AF_INET ;
22 server_addr . sin_port = port ;
23 server_addr . sin_addr . s_addr = inet_addr ( ip ) ;
24 n = bind ( server_sock ,( struct sockaddr *) & server_addr , sizeof ( server_addr ) ) ;
25 if (n <0) {

25
26 perror ( " [ -] Bind error " ) ;
27 exit (1) ;
28 }
29 printf ( " [+] Bind to the port number :% d \ n " , port ) ;
30 listen ( server_sock ,10) ;
31 printf ( " Listening ...\ n " ) ;
32 while (1) {
33 addr_size = sizeof ( client_addr ) ;
34 client_sock = accept ( server_sock ,( struct sockaddr *) & client_addr ,& addr_size ) ;
35 printf ( " [+] Cl i en tc on n ec te d .\ n " ) ;
36 bzero ( buffer ,1024) ;
37 recv ( client_sock , buffer , sizeof ( buffer ) ,0) ;
38 printf ( " Client : % s \ n " , buffer ) ;
39 bzero ( buffer ,1024) ;
40 strcpy ( buffer , " HELLO , THIS IS SERVER " ) ;
41 printf ( " Server : % s \ n " , buffer ) ;
42 send ( client_sock , buffer , strlen ( buffer ) ,0) ;
43 close ( client_sock ) ;
44 printf ( " [+] Client disconnected .\ n \ n " ) ;
45 }
46 }

Client
1 # include < stdio .h >
2 # include < stdlib .h >
3 # include < string .h >
4 # include < unistd .h >
5 # include < arpa / inet .h >
6 void main () {
7 char * ip = " [Link] " ;
8 int port =5566;
9 int sock ;
10 struct sockaddr_in addr ;
11 socklen_t addr_size ;
12 char buffer [1024];
13 int n ;
14 sock = socket ( AF_INET , SOCK_STREAM ,0) ;
15 if ( sock <0) {
16 perror ( " [ -] Socket error " ) ;
17 exit (1) ;
18 }
19 printf ( " [+] TCP server socket created .\ n " ) ;
20 memset (& addr , ’ \0 ’ , sizeof ( addr ) ) ;
21 addr . sin_family = AF_INET ;
22 addr . sin_port = port ;
23 addr . sin_addr . s_addr = inet_addr ( ip ) ;
24 connect ( sock ,( struct sockaddr *) & addr , sizeof ( addr ) ) ;
25 printf ( " Connected to the server .\ n " ) ;
26 bzero ( buffer ,1024) ;
27 strcpy ( buffer , " HELLO FROM THE CLIENT " ) ;
28 printf ( " Client : % s \ n " , buffer ) ;
29 send ( sock , buffer , strlen ( buffer ) ,0) ;
30 bzero ( buffer ,1024) ;
31 recv ( sock , buffer , sizeof ( buffer ) ,0) ;
32 printf ( " Server : % s \ n " , buffer ) ;
33 close ( sock ) ;
34 printf ( " Disconnected from the server .\ n " ) ;
35 }

26
Output

Result
The client-server communication using socket programming and TCP as transport layer is implemented and
output is verified.

27

You might also like