Commit 6a5bd4cb authored by belen's avatar belen
Browse files


parent f44423b4
* Created on: 5 set. 2018
* Author: belen
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <stdio.h>
#define PORT 3490
#define MY_IP ""
#define MAX_QUEUE 10
#define MAX_MSG_SIZE 1024
int main(void)
//primitiva SOCKET
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
//primitiva BIND
struct sockaddr_in server_addr;
socklen_t server_addr_size = sizeof server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr(MY_IP);
bind(server_socket, (struct sockaddr*)&server_addr, server_addr_size); // le fijo a ese skt, el ip y puerto mio, no a donde mando, no hay conexiones de nada aun.
//se le da identidad/ configura el skt servidor
//primitiva LISTEN
listen(server_socket, MAX_QUEUE); //si hay mas de 10 encolados, se descartan las solicitudes nuevas
while (1) {
//primitiva ACCEPT
struct sockaddr_in client_addr;
socklen_t client_addr_size = sizeof client_addr;
int socket_to_client = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_size);
//abre conexion, si no hay nadie solicitando, me quedo aca bloqueado esperando
//primitiva RECEIVE
char* data = malloc(MAX_MSG_SIZE);
int data_size = MAX_MSG_SIZE;
int received_data_size = recv(socket_to_client, data, data_size, 0);
printf("Recibido del cliente (%d bytes): %s\n", received_data_size, data);
int i;
for (i = 0; i < received_data_size; i++) {
data[i] = toupper(data[i]);
//primitiva SEND
int sent_data_size = send(socket_to_client, data, received_data_size, 0);
printf("Enviado al cliente (%d bytes): %s\n", sent_data_size, data);
//primitiva CLOSE
//CLOSE del socket que espera conexiones
* Created on: 5 set. 2018
* Author: belen
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment