Assignment 6
Assignment 6
#define PORT 8080 // Port number where the server will listen for connections
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
const char *hello = "Hello from server";
// Set options for the socket to reuse the port and address
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt,
sizeof(opt))) {
perror("Setsockopt failed");
exit(EXIT_FAILURE);
}
return 0;
}
Client code:-
#include <stdio.h> // For standard input/output functions
#include <stdlib.h> // For standard library functions (e.g., exit)
#include <string.h> // For string manipulation functions
#include <unistd.h> // For close function
#include <arpa/inet.h> // For socket-related functions and definitions
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
const char *hello = "Hello from client";
char buffer[1024] = {0};
// Creating a socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\nSocket creation error\n");
return -1;
}
return 0;
}
SERVER TERMINAL:-
Server is listening on port 8080
Message from client: Hello from client
Hello message sent
CLIENT TERMINAL:-
Hello message sent to server
Message from server: Hello from server