Created
March 27, 2018 15:05
-
-
Save D3f0/a6259043846896205675c10c1aec56e4 to your computer and use it in GitHub Desktop.
TP1
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
/* PrimerServidorTCP.c | |
Servicio: Las cadenas de texto recibidas de un Cliente son enviadas a la salida estándar. | |
Nota: Por simplicidad del código no se realiza ningún tipo de control de errores. No obstante el servidor es totalmente funcional. | |
*/ | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#define PORTNUMBER 12345 | |
int main(void){ | |
char buf[10]; | |
int s, n, ns, len; | |
struct sockaddr_in direcc; | |
s = socket(AF_INET, SOCK_STREAM, 0); | |
bzero((char *) &direcc, sizeof(direcc)); | |
direcc.sin_family = AF_INET; | |
direcc.sin_port = htons(PORTNUMBER); | |
direcc.sin_addr.s_addr = htonl(INADDR_ANY); | |
len = sizeof(struct sockaddr_in); | |
bind(s, (struct sockaddr *) &direcc, len); | |
listen(s, 5); | |
ns = accept(s, (struct sockaddr *) &direcc, &len); | |
while ((n = recv(ns, buf, sizeof(buf), 0)) > 0) | |
write(1, buf, n); | |
close(ns); close(s); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment