0% found this document useful (0 votes)
14 views9 pages

TCP Chat Application

Uploaded by

vasuprabakar1985
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)
14 views9 pages

TCP Chat Application

Uploaded by

vasuprabakar1985
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/ 9

IMPLEMENTATION OF SOCKET

Ex. No. 10
PROGRAMMING USING TCP

AIM:
To implement the socket programming using TCP in linux c.
SOCKET PROGRAMMING:

Sockets provide the communication mechanism between two computers using TCP. A
client program creates a socket on its end of the communication and attempts to connect
that socket to a server. When the connection is made, the server creates a socket object on its end
of the communication.
CREATING A SOCKET PROGRAMMING IN LINUX C:
 Open the notepad in linux.
 Type the below server program and client program using TCP in the notepad.
 Open the terminal.
 Create a socket using below linux commands
 GCC <filename> -o <servername>
(GCC <TCPServer.c> -o <TCPserver>)
 ./servername(./TCPserver)

 GCC <filename> -o <clientname>


(GCC <TCPclient.c> -o <TCPclient>)
 ./<clientname> (./TCPclient)

TCP SERVER PROGRAM:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 4444
int main() {
int sockfd, ret;
struct sockaddr_in serverAddr;
int newSocket;
struct sockaddr_in newAddr;
OUTPUT:
socklen_t addr_size;
char buffer[1024];
OUTPUT:
pid_t childpid;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("[-]Error in connection.\n");
exit(1);
}
printf("[+]Server Socket is created.\n");

memset(&serverAddr, '\0', sizeof(serverAddr));


serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

ret = bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));


if (ret < 0) {
printf("[-]Error in binding.\n");
exit(1);
}
printf("[+]Bind to port %d\n", PORT);

if (listen(sockfd, 10) == 0) {
printf("[+]Listening....\n");
} else {
printf("[-]Error in listening.\n");
exit(1);
}

while (1) {
addr_size = sizeof(newAddr);
newSocket = accept(sockfd, (struct sockaddr*)&newAddr, &addr_size);
if (newSocket < 0) {
exit(1);
}
printf("Connection accepted from %s:%d\n", inet_ntoa(newAddr.sin_addr),
ntohs(newAddr.sin_port));

if ((childpid = fork()) == 0) {
close(sockfd);

while (1) {
memset(buffer, 0, sizeof(buffer));
int recv_size = recv(newSocket, buffer, 1024, 0);
if (recv_size <= 0) {
printf("[-]Disconnected from %s:%d\n", inet_ntoa(newAddr.sin_addr),
ntohs(newAddr.sin_port));
break;
} else if (strcmp(buffer, ":exit") == 0) {
printf("[-]Client requested to exit from %s:%d\n", inet_ntoa(newAddr.sin_addr),
ntohs(newAddr.sin_port));
break;
} else {
printf("Client: %s\n", buffer);
send(newSocket, buffer, strlen(buffer), 0);
}
}
close(newSocket);
exit(0);
}
close(newSocket);
}
close(sockfd);
return 0;
}
TCP CLIENT PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 4444
int main() {
int clientSocket, ret;
struct sockaddr_in serverAddr;
char buffer[1024];
clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket < 0) {
printf("[-]Error in connection.\n");
exit(1);
}
printf("[+]Client Socket is created.\n");
memset(&serverAddr, '\0', sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
ret = connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
if (ret < 0) {
printf("[-]Error in connection.\n");
exit(1);
}
printf("[+]Connected to Server.\n");
while (1) {
printf("Client: \t");
scanf("%s", buffer); // Corrected scanf argument
send(clientSocket, buffer, strlen(buffer), 0);
if (strcmp(buffer, ":exit") == 0) {
close(clientSocket);
printf("[-]Disconnected from server.\n");
exit(0);
}
}
memset(buffer, 0, sizeof(buffer)); // Clear buffer before receiving
if (recv(clientSocket, buffer, 1024, 0) < 0) {
printf("[-]Error in receiving data.\n");
} else {
printf("Server: \t%s\n", buffer);
}
return 0;
}
RESULT:
Thus, the socket programming using TCP has been executed successfully, and the output
has been verified.

You might also like