Question: How can I update the code, so 2 people on different computers ( not terminals ) can communicate with each other? Thank you! client.c #include

How can I update the code, so 2 people on different computers (not terminals) can communicate with each other? Thank you!
client.c
#include
#include
#include
#include
#include
#define PORT 8080
#define BUFFER_SIZE 1024
int main(){
int sock =0;
struct sockaddr_in serv_addr;
char buffer[BUFFER_SIZE]={0};
// Create socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0))<0){
printf("
Socket creation error
");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IP address to binary format
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0){
printf("
Invalid address/ Address not supported
");
return -1;
}
// Connect to server
if (connect(sock,(struct sockaddr *)&serv_addr, sizeof(serv_addr))<0){
printf("
Connection Failed
");
return -1;
}
printf("Connected to server!
");
while (1){
// Send message to server
printf("You: ");
fgets(buffer, BUFFER_SIZE, stdin);
send(sock, buffer, strlen(buffer),0);
// Receive message from server
memset(buffer,0, BUFFER_SIZE);
int valread = read(sock, buffer, BUFFER_SIZE);
if (valread <=0) break;
printf("Server: %s
", buffer);
}
close(sock);
return 0;
}
server.c
#include
#include
#include
#include
#include
#define PORT 8080
#define BUFFER_SIZE 1024
int main(){
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE]={0};
// Create socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0))==0){
perror("Socket failed");
exit(EXIT_FAILURE);
}
// Bind the socket to the network address and port
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd,(struct sockaddr *)&address, sizeof(address))<0){
perror("Bind failed");
close(server_fd);
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(server_fd,3)<0){
perror("Listen failed");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Waiting for a connection...
");
// Accept a connection
if ((new_socket = accept(server_fd,(struct sockaddr *)&address, (socklen_t*)&addrlen))<0){
perror("Accept failed");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Connected to client!
");
while (1){
// Receive message from client
memset(buffer,0, BUFFER_SIZE);
int valread = read(new_socket, buffer, BUFFER_SIZE);
if (valread <=0) break;
printf("Client: %s
", buffer);
// Send message to client
printf("You: ");
fgets(buffer, BUFFER_SIZE, stdin);
send(new_socket, buffer, strlen(buffer),0);
}
close(new_socket);
close(server_fd);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!