0% found this document useful (0 votes)
18 views3 pages

Experiment 3 TCP Using Socket

The document outlines the algorithms for creating a TCP server and client in C. It details the steps for setting up sockets, binding, listening, accepting connections, sending and receiving data, and handling disconnections. Sample code is provided for both the server and client implementations.

Uploaded by

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

Experiment 3 TCP Using Socket

The document outlines the algorithms for creating a TCP server and client in C. It details the steps for setting up sockets, binding, listening, accepting connections, sending and receiving data, and handling disconnections. Sample code is provided for both the server and client implementations.

Uploaded by

redbadff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

TCP Server Algorithm

Step 1: Create a TCP Server Socket


1. Create a TCP server socket using socket(AF_INET, SOCK_STREAM, 0).
2. Check if the socket creation was successful. If not, exit the program.

Step 2: Set Server Address


1. Set the server address using struct sockaddr_in.
2. Specify the address family (AF_INET), port number, and IP address.

Step 3: Bind the Socket


1. Bind the server socket to the specified address and port using bind().
2. Check if the binding was successful. If not, exit the program.

Step 4: Listen for Incoming Connections


1. Listen for incoming connections using listen().
2. Specify the maximum number of pending connections (10 in this case).

Step 5: Accept and Handle Client Connections


1. Accept an incoming client connection using accept().
2. Create a new socket for communication with the client.
3. Receive data from the client using recv().
4. Process the received data (print it to the console in this case).
5. Send a response back to the client using send().
6. Close the client socket using close().

Step 6: Repeat the Process


1. Loop indefinitely to accept and handle multiple client connections.

ts.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<arpa/inet.h>
void main() {
char *ip="127.0.0.1";
int port=5555;
int server_sock,client_sock;
struct sockaddr_in server_addr,client_addr;
socklen_t addr_size;
char buffer[1024];
int n;
server_sock = socket( AF_INET , SOCK_STREAM ,0);
if(server_sock<0) {
perror("[ -] Socket error ");
exit(1);
}
printf(" [+] TCP server socket created .\n ");
memset(&server_addr ,'\0',sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = port;
server_addr.sin_addr.s_addr = inet_addr(ip);
n = bind(server_sock,( struct sockaddr *)&server_addr,sizeof(server_addr));
if(n<0)
{
perror("[ -] Bind error ");
exit(1);
}
printf("[+] Bind to the port number :% d \n",port);
listen(server_sock ,10);
printf(" Listening ...\n ");
while(1) {
addr_size = sizeof(client_addr);
client_sock = accept( server_sock ,( struct sockaddr *) & client_addr ,& addr_size );
printf("[+] Cl i en tc on n ec te d .\n " );
bzero(buffer ,1024);
recv(client_sock,buffer,sizeof(buffer),0);
printf("Client : %s \n ",buffer);
bzero(buffer,1024);
strcpy(buffer," HELLO , THIS IS SERVER ");
printf(" Server : %s \n" , buffer);
send(client_sock ,buffer,strlen ( buffer) ,0) ;
close(client_sock ) ;
printf(" [+] Client disconnected .\n \n ") ;
}
}

TCP Client Algorithm


Step 1: Create a TCP Client Socket
1. Create a TCP client socket using socket(AF_INET, SOCK_STREAM, 0).
2. Check if the socket creation was successful. If not, exit the program.

Step 2: Set Server Address


1. Set the server address using struct sockaddr_in.
2. Specify the address family (AF_INET), port number, and IP address.

Step 3: Connect to the Server


1. Connect to the server using connect().
2. Pass the client socket, server address, and address size as arguments.

Step 4: Send Data to the Server


1. Send data to the server using send().
2. Pass the client socket, data buffer, data length, and flags as arguments.

Step 5: Receive Data from the Server


1. Receive data from the server using recv().
2. Pass the client socket, data buffer, buffer size, and flags as arguments.

Step 6: Close the Client Socket


1. Close the client socket using close().
2. End the connection with the server.
Step 7: Print Connection Status
1. Print connection status messages to the console.
2. Indicate when connected, disconnected, and data sent/received.

tc.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<arpa/inet.h>
void main() {
char *ip = "127.0.0.1";
int port =5555;
int sock;
struct sockaddr_in addr;
socklen_t addr_size;
char buffer [1024];
int n;
sock = socket (AF_INET, SOCK_STREAM,0);
if(sock<0)
{
perror("[ -] Socket error ") ;
exit (1);
}
printf("[+] TCP server socket created .\n " );
memset(&addr,'\0',sizeof( addr ));
addr.sin_family = AF_INET;
addr.sin_port = port;
addr.sin_addr.s_addr = inet_addr(ip);
connect(sock,( struct sockaddr *)&addr,sizeof(addr));
printf("Connected to the server .\n ");
bzero(buffer,1024);
strcpy(buffer," HELLO FROM THE CLIENT ");
printf("Client : %s\n ",buffer);
send(sock,buffer,strlen(buffer),0);
bzero(buffer,1024);
recv(sock,buffer,sizeof(buffer),0);
printf("Server : %s\n",buffer );
close(sock) ;
printf(" Disconnected from the server .\n ");
}

You might also like