119k views
0 votes
A haiku is a three-line poem in which the first line contains five syllables, the second line contains seven syllables, and the third line contains five syllables. Write a haiku server that listens to port 5755. When a client connects to this port the server responds with a haiku; written in C language. Should only be a few lines of code.

User THTP
by
4.4k points

2 Answers

3 votes

Final answer:

To write a haiku server that listens to port 5755 and responds with a haiku, you can use socket programming in C. Here is an example of how you can implement this in a few lines of code.

Step-by-step explanation:

To write a haiku server that listens to port 5755 and responds with a haiku written in C language, you can use socket programming in C. Here is an example of how you can implement this:



#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<arpa/inet.h>

#define PORT 5755
#define BUFFER_SIZE 1024

int main(){
int server_socket, client_socket;
struct sockaddr_in server_address, client_address;
char* haiku = "Autumn moonlight,\\The sound of the bell\\As it floats across fields.";

// Create socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
printf("Failed to create socket.\\");
exit(EXIT_FAILURE);
}

// Set server address
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(PORT);

// Bind the socket and address
if (bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {
printf("Failed to bind the socket.\\");
exit(EXIT_FAILURE);
}

// Listen for incoming connections
if (listen(server_socket, 3) < 0) {
printf("Failed to listen for connections.\\");
exit(EXIT_FAILURE);
}

printf("Server listening on port %d.\\", PORT);

// Accept incoming connections
int client_address_size = sizeof(client_address);
client_socket = accept(server_socket, (struct sockaddr *)&client_address, (socklen_t *)&client_address_size);
if (client_socket < 0) {
printf("Failed to accept connection.\\");
exit(EXIT_FAILURE);
}

printf("Client connected. Sending haiku...\\");

// Send the haiku to the client
if (send(client_socket, haiku, strlen(haiku), 0) < 0) {
printf("Failed to send haiku.\\");
exit(EXIT_FAILURE);
}

// Close the sockets
close(client_socket);
close(server_socket);

return 0;
}
User Fukanchik
by
4.7k points
4 votes

Answer:

Files :

server.c

client.c

Haiku msg : ( source internet )

Birds fly through the wind

The wind blows through the blue sky

Winds of change grow close

Step-by-step explanation:

server.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <sys/socket.h>

#include <netinet/in.h>

// Here we've defined the PORT

#define PORT 5755

// This a out haiku_msg

const char haiku_msg[] = "Birds fly through the wind\\The wind blows through the blue sky\\Winds of change grow close";

int main()

{

int server_fd, new_socket;

struct sockaddr_in address;

int addrlen = sizeof(address);

// Creating socket file descriptor

if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {

perror("socket failed");

exit(EXIT_FAILURE);

}

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");

exit(EXIT_FAILURE);

}

printf("haiku server started...\\\\");

if (listen(server_fd, 5) < 0) {

perror("listen");

exit(EXIT_FAILURE);

}

if ((new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen)) < 0) {

perror("accept");

exit(EXIT_FAILURE);

}

printf("\\Client connected to haiku server.\\sending msg.\\");

send(new_socket, haiku_msg, strlen(haiku_msg), 0);

printf("haiku sent to client.\\\\");

return 0;

}

client.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <arpa/inet.h>

#include <sys/socket.h>

#include <netinet/in.h>

// Here we have defined the PORT

#define PORT 5755

int main()

{

char buffer[1024] = "";

struct sockaddr_in address;

struct sockaddr_in serv_addr;

int sock = 0;

if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)

{

printf("\\ Socket creation error \\");

return -1;

}

memset(&serv_addr, '0', sizeof(serv_addr));

serv_addr.sin_family = AF_INET;

serv_addr.sin_port = htons(PORT);

// Convert IPv4 and IPv6 addresses from text to binary form

if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)

{

printf("\\Invalid address/ Address not supported \\");

return -1;

}

if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)

{

printf("\\Connection Failed \\");

return -1;

}

read( sock , buffer, 1024);

printf("\\Message from server : \\%s\\\\",buffer );

return 0;

}

User Ismael Ghalimi
by
5.0k points