PLEASE USE MULTITHREADING IN THIS PROGRAM SO THAT MANY CLIENTS CONNECTED TO THE SERVER AND EACH CLIENT CAN SEND MESSAGE TO SERVER AND SERVER MESSAGE SHOWN TO ALL CLIENTS AND ONE CLIENT MESSAGE SHOWN TO ALL CLIENTS.PLEASE MAKE CHANGINGS IN THIS CODE. SERVER #include #include #include #include #include #include #include #include #include #define PORT 5500 void func(int connfd){ char buffer[1024]; int n; while(1) { bzero(buffer, 1024); read(connfd, buffer, sizeof(buffer)); printf("From client: %s\t To client : ", buffer); bzero(buffer, 1024); n = 0; while ((buffer[n++] = getchar()) != '\n'); write(connfd, buffer, sizeof(buffer)); if (strncmp("exit", buffer, 4) == 0){ printf("Server Exit...\n"); break; } } } int main(){ int sockfd, connfd, len; struct sockaddr_in serverAddr, cli; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { printf("socket creation failed...\n"); exit(0); } else{ printf("Socket successfully created..\n"); } memset(&serverAddr, '\0', sizeof(serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); serverAddr.sin_port = htons(PORT); if((bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr))) != 0) { printf("socket bind failed...\n"); exit(0); } else{ printf("Socket successfully binded..\n"); } if((listen(sockfd, 5)) != 0) { printf("Listen failed...\n"); exit(0); } else{ printf("Server listening..\n"); } len = sizeof(cli); connfd = accept(sockfd, (struct sockaddr*)&cli, &len); if (connfd < 0) { printf("server accept failed...\n"); exit(0); } else{ printf("server accept the client...\n"); } func(connfd); close(sockfd); } CLIENT #include #include #include #include #include #include #include #include #include #define PORT 5500 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[0]); send(clientSocket, buffer, strlen(buffer), 0); if(strcmp(buffer, ":exit") == 0){ close(clientSocket); printf("[-]Disconnected from server.\n"); exit(1); } if(recv(clientSocket, buffer, 1024, 0) < 0){ printf("[-]Error in receiving data.\n"); }else{ printf("Server: \t%s\n", buffer); } } return 0; }
PLEASE USE MULTITHREADING IN THIS PROGRAM SO THAT MANY CLIENTS CONNECTED TO THE SERVER AND EACH CLIENT CAN SEND MESSAGE TO SERVER AND SERVER MESSAGE SHOWN TO ALL CLIENTS AND ONE CLIENT MESSAGE SHOWN TO ALL CLIENTS.PLEASE MAKE CHANGINGS IN THIS CODE.
SERVER
#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>
#include <pthread.h>
#define PORT 5500
void func(int connfd){
char buffer[1024];
int n;
while(1) {
bzero(buffer, 1024);
read(connfd, buffer, sizeof(buffer));
printf("From client: %s\t To client : ", buffer);
bzero(buffer, 1024);
n = 0;
while ((buffer[n++] = getchar()) != '\n');
write(connfd, buffer, sizeof(buffer));
if (strncmp("exit", buffer, 4) == 0){
printf("Server Exit...\n");
break;
}
}
}
int main(){
int sockfd, connfd, len;
struct sockaddr_in serverAddr, cli;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);
}
else{
printf("Socket successfully created..\n");
}
memset(&serverAddr, '\0', sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(PORT);
if((bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr))) != 0) {
printf("socket bind failed...\n");
exit(0);
}
else{
printf("Socket successfully binded..\n");
}
if((listen(sockfd, 5)) != 0) {
printf("Listen failed...\n");
exit(0);
}
else{
printf("Server listening..\n");
}
len = sizeof(cli);
connfd = accept(sockfd, (struct sockaddr*)&cli, &len);
if (connfd < 0) {
printf("server accept failed...\n");
exit(0);
}
else{
printf("server accept the client...\n");
}
func(connfd);
close(sockfd);
}
CLIENT
#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>
#include <pthread.h>
#define PORT 5500
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[0]);
send(clientSocket, buffer, strlen(buffer), 0);
if(strcmp(buffer, ":exit") == 0){
close(clientSocket);
printf("[-]Disconnected from server.\n");
exit(1);
}
if(recv(clientSocket, buffer, 1024, 0) < 0){
printf("[-]Error in receiving data.\n");
}else{
printf("Server: \t%s\n", buffer);
}
}
return 0;
}
Step by step
Solved in 4 steps