0% found this document useful (0 votes)
7 views2 pages

code2pdf_67305b662c11c

This document is a C++ program that implements a simple TCP server. It listens for incoming client connections on port 12345, sends a greeting message, and receives a file from the client, saving it as 'received_file'. The program handles socket creation, binding, listening, accepting connections, and file writing.

Uploaded by

anujaw.aidsioe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

code2pdf_67305b662c11c

This document is a C++ program that implements a simple TCP server. It listens for incoming client connections on port 12345, sends a greeting message, and receives a file from the client, saving it as 'received_file'. The program handles socket creation, binding, listening, accepting connections, and file writing.

Uploaded by

anujaw.aidsioe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <fstream>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 12345


#define BUF_SIZE 1024

using namespace std;

int main() {
int server_sock, client_sock;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
char buffer[BUF_SIZE];

// Create a TCP socket


server_sock = socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0) {
perror("Socket creation failed");
return -1;
}

memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY; // Listen on all interfaces
server_addr.sin_port = htons(PORT);

// Bind the socket to the address and port


if (bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed");
close(server_sock);
return -1;
}

// Listen for incoming connections


if (listen(server_sock, 5) < 0) {
perror("Listen failed");
close(server_sock);
return -1;
}

cout << "Server listening on port " << PORT << "..." << endl;

// Accept incoming connection


client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &client_len);
if (client_sock < 0) {
perror("Accept failed");
close(server_sock);
return -1;
}

cout << "Connected to client!" << endl;

// Send a greeting message to the client


const char *greeting = "Hello, client! I am the server.";
send(client_sock, greeting, strlen(greeting), 0);
cout << "Sent greeting to client." << endl;

// Receive the file from the client


ofstream out_file("received_file", ios::binary);
if (!out_file) {
cerr << "Error opening file for writing" << endl;
close(client_sock);
close(server_sock);
return -1;
}

// Receive the file in chunks and write to the output file


while (true) {
int bytes_received = recv(client_sock, buffer, BUF_SIZE, 0);
if (bytes_received <= 0) {
break; // No more data or error
}
out_file.write(buffer, bytes_received);
}

cout << "File received successfully." << endl;

out_file.close();
close(client_sock);
close(server_sock);

return 0;
}

You might also like