Ayuda para enviar mensajes en c unix
La tarea trata sobre el desarrollo de un conjunto de peque帽as aplicaciones, que permitan simular la ejecuci贸n de un sistema distribuido y el uso de relojes vectoriales, para monitorear sus acciones. Para ello se deben crear dos aplicaciones. Se deben crear dos procesos mas de cliente, cada uno envia sus mensajes, el archivo en el que se envian mensajes es un texto plano.
CLIENTE
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <ctype.h>
char buffer[256];
int main(int argc, char *argv[])
{
int conexion, largo_leido;
struct sockaddr_in client;
struct hostent *host_ip, *gethostbyname();
bzero (&client, sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons(atoi(argv[2]));
if (isdigit(argv[1][0]))
client.sin_addr.s_addr = inet_addr(argv[1]);
else
{
host_ip = gethostbyname(argv[1]);
if (host_ip == NULL)
{
perror ((\"ERROR: No se pudo resolver nombre %s.\", argv[1]));
exit (1);
}
bcopy(host_ip->h_addr, &client.sin_addr, host_ip->h_length);
}
conexion = socket(AF_INET, SOCK_STREAM, 0);
if (conexion < 0)
{
perror (\"ERROR: No es posible crear socket\");
exit (1);
}
//***********************************************************
/*
* Esta es la parte en donde se debe trabajar !!!
*/
connect(conexion, (struct sockaddr *)&client, sizeof(client));
printf (\"Esperando datos de teclado...\\n\");
largo_leido = read(0, buffer, sizeof(buffer));
while (largo_leido > 0)
{
write(conexion, buffer, largo_leido);
largo_leido = read(0, buffer, sizeof(buffer));
}
close(conexion);
//***********************************************************
return 0;
}
SERVIDOR
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char buffer[256];
int main(int argc, char *argv[])
{
struct sockaddr_in server;
int largo_leido, largo_socket, nuestro_socket, conexion;
nuestro_socket = socket(AF_INET, SOCK_STREAM, 0);
if (nuestro_socket < 0)
{
perror (\"ERROR: No se puede crear socket.\");
exit (1);
}
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(atoi(argv[1]));
if (bind(nuestro_socket, (struct sockaddr *)&server, sizeof(server)) < 0)
{
close(nuestro_socket);
perror(\"ERROR: No se puede hacer bind del socket.\");
exit(1);
}
if (listen(nuestro_socket, 5) < 0)
{
perror (\"ERROR: No se puede escuchar.\");
exit (1);
}
largo_socket = sizeof (struct sockaddr_in);
//****************************************************
/*
* Esta es la parte en donde se debe trabajar!!!
*/
printf(\"Servidor activo, esperando conexion...\\n\");
conexion = accept(nuestro_socket, (struct sockaddr *)&server, &largo_socket);
printf(\"Conexi贸n aceptada desde: %s\\n\", inet_ntoa(server.sin_addr));
largo_leido = read(conexion, buffer, sizeof(buffer));
while (largo_leido>0)
{
write(1, buffer, largo_leido);
largo_leido = read(conexion, buffer, sizeof(buffer));
}
printf(\"Cierra conexion\\n\");
close(conexion);
close(nuestro_socket);
//****************************************************
return 0;
}
CLIENTE
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <ctype.h>
char buffer[256];
int main(int argc, char *argv[])
{
int conexion, largo_leido;
struct sockaddr_in client;
struct hostent *host_ip, *gethostbyname();
bzero (&client, sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons(atoi(argv[2]));
if (isdigit(argv[1][0]))
client.sin_addr.s_addr = inet_addr(argv[1]);
else
{
host_ip = gethostbyname(argv[1]);
if (host_ip == NULL)
{
perror ((\"ERROR: No se pudo resolver nombre %s.\", argv[1]));
exit (1);
}
bcopy(host_ip->h_addr, &client.sin_addr, host_ip->h_length);
}
conexion = socket(AF_INET, SOCK_STREAM, 0);
if (conexion < 0)
{
perror (\"ERROR: No es posible crear socket\");
exit (1);
}
//***********************************************************
/*
* Esta es la parte en donde se debe trabajar !!!
*/
connect(conexion, (struct sockaddr *)&client, sizeof(client));
printf (\"Esperando datos de teclado...\\n\");
largo_leido = read(0, buffer, sizeof(buffer));
while (largo_leido > 0)
{
write(conexion, buffer, largo_leido);
largo_leido = read(0, buffer, sizeof(buffer));
}
close(conexion);
//***********************************************************
return 0;
}
SERVIDOR
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char buffer[256];
int main(int argc, char *argv[])
{
struct sockaddr_in server;
int largo_leido, largo_socket, nuestro_socket, conexion;
nuestro_socket = socket(AF_INET, SOCK_STREAM, 0);
if (nuestro_socket < 0)
{
perror (\"ERROR: No se puede crear socket.\");
exit (1);
}
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(atoi(argv[1]));
if (bind(nuestro_socket, (struct sockaddr *)&server, sizeof(server)) < 0)
{
close(nuestro_socket);
perror(\"ERROR: No se puede hacer bind del socket.\");
exit(1);
}
if (listen(nuestro_socket, 5) < 0)
{
perror (\"ERROR: No se puede escuchar.\");
exit (1);
}
largo_socket = sizeof (struct sockaddr_in);
//****************************************************
/*
* Esta es la parte en donde se debe trabajar!!!
*/
printf(\"Servidor activo, esperando conexion...\\n\");
conexion = accept(nuestro_socket, (struct sockaddr *)&server, &largo_socket);
printf(\"Conexi贸n aceptada desde: %s\\n\", inet_ntoa(server.sin_addr));
largo_leido = read(conexion, buffer, sizeof(buffer));
while (largo_leido>0)
{
write(1, buffer, largo_leido);
largo_leido = read(conexion, buffer, sizeof(buffer));
}
printf(\"Cierra conexion\\n\");
close(conexion);
close(nuestro_socket);
//****************************************************
return 0;
}
