Skip to content

Instantly share code, notes, and snippets.

@D3f0
Created March 27, 2018 15:05
Show Gist options
  • Save D3f0/a6259043846896205675c10c1aec56e4 to your computer and use it in GitHub Desktop.
Save D3f0/a6259043846896205675c10c1aec56e4 to your computer and use it in GitHub Desktop.
TP1
/* 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