Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please locate the incorrect error. We have made many ports, but all are still the same. The output that comes out is error connection refused

Please locate the incorrect error. We have made many ports, but all are still the same. The output that comes out is "error connection refused (ip address are correct and port are changed but still error)" Server C #include #include #include #include #include #include #include #define PORT 8080 #define MAX_LEN 1024 int main() { int server_socket, client_socket, pid; struct sockaddr_in server_address, client_address; socklen_t client_len = sizeof(client_address); char buffer[MAX_LEN]; // Create a socket if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket creation failed"); exit(EXIT_FAILURE); } // Bind socket to a specific address and port server_address.sin_family = AF_INET; server_address.sin_port = htons(PORT); server_address.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address)) < 0) { perror("bind failed"); exit(EXIT_FAILURE); } // Listen for incoming connections if (listen(server_socket, 5) < 0) { perror("listen failed"); exit(EXIT_FAILURE); } // Loop to handle incoming connections while (1) { // Accept incoming connection if ((client_socket = accept(server_socket, (struct sockaddr*) &client_address, &client_len)) < 0) { perror("accept failed"); continue; } // Use fork to handle incoming connection pid = fork(); if (pid == 0) { // Child process close(server_socket); // Read incoming data from client memset(buffer, 0, MAX_LEN); if (read(client_socket, buffer, MAX_LEN) < 0) { perror("read failed"); exit(EXIT_FAILURE); } // Combine incoming data with string "from server xyz" strcat(buffer, " from server xyz"); // Write combined data back to client if (write(client_socket, buffer, strlen(buffer)) < 0) { perror("write failed"); exit(EXIT_FAILURE); } close(client_socket); exit(EXIT_SUCCESS); } else if (pid > 0) { // Parent process close(client_socket); } else { perror("fork failed"); } } return 0; } ======================= Client Python ======================= import socket c = socket.socket(socket.AF_INET, socket.SOCK_STREAM) c.connect(("172.104.177.86", 8080)) #Compose a string from user input request=input("Enter a message to send to the server: ") #send user input string to the server c.send(request.encode()) #print incoming input string to the server response = c.recv(1024).decode() print(f"Server reply: {response}") c.close()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

How To Build A Million Dollar Database

Authors: Michelle Bergquist

1st Edition

0615246842, 978-0615246840

More Books

Students also viewed these Databases questions

Question

What are the functions of top management?

Answered: 1 week ago

Question

Bring out the limitations of planning.

Answered: 1 week ago

Question

Why should a business be socially responsible?

Answered: 1 week ago

Question

Discuss the general principles of management given by Henri Fayol

Answered: 1 week ago

Question

5. Describe the relationship between history and identity.

Answered: 1 week ago