-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: xXIamNoOneXx <143564810+xXIamNoOneXx@users.noreply.github.com>
- Loading branch information
1 parent
091bed9
commit 35d6714
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <iostream> | ||
#include <cstring> | ||
#include <unistd.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
|
||
int main() { | ||
int server_socket, client_socket; | ||
struct sockaddr_in server_addr, client_addr; | ||
|
||
// Create a socket | ||
server_socket = socket(AF_INET, SOCK_STREAM, 0); | ||
|
||
// Configure the server address structure | ||
server_addr.sin_family = AF_INET; | ||
server_addr.sin_port = htons(12345); // Choose a port | ||
server_addr.sin_addr.s_addr = INADDR_ANY; | ||
|
||
// Bind the socket to the server address | ||
bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)); | ||
|
||
// Listen for incoming connections | ||
listen(server_socket, 5); // Number of queued connections | ||
|
||
std::cout << "Server listening on port 12345..." << std::endl; | ||
|
||
// Accept an incoming connection | ||
socklen_t client_addr_len = sizeof(client_addr); | ||
client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len); | ||
|
||
std::cout << "Accepted connection from " << inet_ntoa(client_addr.sin_addr) << std::endl; | ||
|
||
// Send a welcome message to the client | ||
const char* welcome_message = "Welcome to the server!\n"; | ||
send(client_socket, welcome_message, strlen(welcome_message), 0); | ||
|
||
// Close the sockets | ||
close(client_socket); | ||
close(server_socket); | ||
|
||
return 0; | ||
} |