Created
July 30, 2024 10:11
-
-
Save alonsoir/b1242d2a75d1497408225cf78f1613f5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#define BUF_SIZE 1024 | |
#define L_BACKLOG 5 | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <unistd.h> | |
void handle_error(const char *message) { | |
perror(message); | |
exit(EXIT_FAILURE); | |
} | |
int main(int argc, char *argv[]) { | |
if (argc != 2) { | |
printf("Usage: %s <port>", argv[0]); | |
return 1; | |
} | |
int port = atoi(argv[1]); | |
if (port <= 0 || port > 65535) { | |
fprintf(stderr, "Invalid port number"); | |
return 1; | |
} | |
int sfd, cfd; | |
struct sockaddr_in sock_addr, peer_addr; | |
char buffer[BUF_SIZE]; | |
socklen_t peer_addrlen; | |
sfd = socket(AF_INET, SOCK_STREAM, 0); | |
if (sfd == -1) | |
handle_error("socket"); | |
memset(&sock_addr, 0, sizeof(sock_addr)); | |
sock_addr.sin_family = AF_INET; | |
sock_addr.sin_port = htons(port); | |
sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
if (bind(sfd, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) == -1) | |
handle_error("bind"); | |
if (listen(sfd, L_BACKLOG) == -1) | |
handle_error("listen"); | |
printf("Server is listening on port %d...", port); | |
peer_addrlen = sizeof(peer_addr); | |
cfd = accept(sfd, (struct sockaddr*)&peer_addr, &peer_addrlen); | |
if (cfd == -1) | |
handle_error("accept"); | |
recv(cfd, buffer, BUF_SIZE, 0); | |
printf("Received message: %s", buffer); | |
close(cfd); | |
close(sfd); | |
return 0; | |
} |
This file contains hidden or 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
if (argc + 2) { | |
printf("usage: %s PORT:int | |
", argv[0]); | |
exit (EXIT_FAILURE) ; | |
} | |
int port = atoi(argv[1]); | |
if (port < 0 || port = 65535) { | |
fprintf(stderr, "invalid port number"); | |
exit (EXIT_FAILURE) ; | |
} | |
int sfd, cfd; // socket and client file descriptors | |
socklen_t peer_addrlen; // size of the peer address struct | |
struct sockaddr_in sock_addr, peer_addr; // socket and client address structs | |
char buffer[BUF_SIZE]; | |
sfd = socket(AF_INET, SOCK_STREAM, 0); | |
if (sfd = -1) | |
handle_error("socket"); | |
memset(&sock_addr, 0, sizeof (sock_addr)); | |
sock_addr.sin_family = AF_INET; | |
sock_addr.sin_port = htons(port); | |
sock_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
if (bind(sfd, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) = -1) | |
handle_error("bind"); | |
if (listen(sfd, L_BACKLOG) = -1) | |
handle_error("listen"); | |
// we are now primed to receive incoming connections | |
peer_addrlen = sizeof (peer_addr) ; | |
cfd = accept(sfd, (struct sockaddr *)&peer_addr, &peer_addrlen); | |
if (cfd = -1) | |
handle_error("accept"); | |
// the client is now connected. operations primed. | |
recv(cfd, buffer, 255, 0); | |
printf("%s | |
", buffer); | |
// closing the socket. setting the SO_REUSEADDR to 1 will allow quick rebinding to | |
the same address and port. | |
int opt = | |
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) | |
handle_error("setsockopt"); | |
"webserver.c" 75L, 2055B written |
This file contains hidden or 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
#define BUF_SIZE 1024 | |
#define L_BACKLOG 5 | |
#include <signal.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <unistd.h> | |
volatile sig_atomic_t keep_running = 1; | |
void handle_error(const char *message) { | |
perror(message); | |
exit(EXIT_FAILURE); | |
} | |
void sigterm_handler(int sig) { | |
keep_running = 0; | |
} | |
int main(int argc, char *argv[]) { | |
if (argc != 2) { | |
printf("Usage: %s <port>", argv[0]); | |
return 1; | |
} | |
int port = atoi(argv[1]); | |
if (port <= 0 || port > 65535) { | |
fprintf(stderr, "Invalid port number"); | |
return 1; | |
} | |
int sfd, cfd; | |
struct sockaddr_in sock_addr, peer_addr; | |
char buffer[BUF_SIZE]; | |
socklen_t peer_addrlen; | |
signal(SIGTERM, sigterm_handler); | |
sfd = socket(AF_INET, SOCK_STREAM, 0); | |
if (sfd == -1) handle_error("socket"); | |
memset(&sock_addr, 0, sizeof(sock_addr)); | |
sock_addr.sin_family = AF_INET; | |
sock_addr.sin_port = htons(port); | |
sock_addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
if (bind(sfd, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) == -1) handle_error("bind"); | |
if (listen(sfd, L_BACKLOG) == -1) handle_error("listen"); | |
printf("Server is listening on port %d...", port); | |
while (keep_running) { | |
peer_addrlen = sizeof(peer_addr); | |
cfd = accept(sfd, (struct sockaddr*)&peer_addr, &peer_addrlen); | |
if (cfd == -1) handle_error("accept"); | |
recv(cfd, buffer, BUF_SIZE, 0); | |
printf("Received message: %s", buffer); | |
close(cfd); | |
} | |
close(sfd); | |
return 0; | |
} |
This file contains hidden or 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
import socket | |
# Configura los parámetros para la conexión | |
host = 'localhost' # Ajusta esto a la dirección IP necesaria | |
port = 8080 # El puerto en el que tu servidor está escuchando | |
# Crea un objeto socket | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
s.connect((host, port)) # Conecta al servidor | |
# Enviar un mensaje de exactamente 1024 'a' | |
message = 'b' * 1024 | |
s.sendall(message.encode()) # Envía el mensaje al servidor | |
data = s.recv(1024) # Recibe la respuesta del servidor (si esperas una) | |
print('Sent and potentially received data from server') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment