Decir que son solo las funciones de la connexion de sockets, hay algunas funciones que no estas ^^
Gracias de antemano.
SERVIDOR
Código:
*/
int process_msg(int sock, struct _DNSTable *dnsTable)
{
unsigned short op_code;
char buffer[MAX_BUFF_SIZE];
int done = 0;
int size;
//TODO: ....
op_code = ldshort(buffer);
switch(op_code)
{
case MSG_HELLO_RQ:
printf("HELLO\n");
bzero(buffer,sizeof(buffer)); // NECESARIO Poner el buffer a 0
stshort(2,buffer);//fiquem el codigo del mensaje
strcpy(buffer, "Hello World\0");//|02|Hello World|0|
size=14;
break;
break;
case MSG_LIST_RQ:
process_LIST_RQ_msg(sock, dnsTable);
break;
case MSG_FINISH:
//TODO
done = 1;
break;
default:
perror("Message code does not exist.\n");
}
if(send(sock, buffer, size, 0) < 0)
{
perror("SEND FAIL");
close(sock);
exit(1);
}
return done;
}
int main (int argc, char *argv[])
{
struct _DNSTable *dnsTable;
int port;
char dns_file[MAX_FILE_NAME_SIZE];
char buffer[MAX_BUFF_SIZE];
short opcode;
getProgramOptions(argc, argv, dns_file, &port);
dnsTable = loadDNSTableFromFile(dns_file);
printDNSTable(dnsTable);
int s;
int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); // crea el socket del servidor
//LOCAL SERVER CODE
struct sockaddr_in local, client;//struct del host local
local.sin_family=AF_INET;
local.sin_port= htons(DEFAULT_PORT); // cambiar por port
local.sin_addr.s_addr=INADDR_ANY;
//CALL BIND
if( bind(sock, (struct sockaddr *) &local, sizeof(local)) < 0)
{
perror("BIND FAIL");
}
//CALL LISTEN
if( listen(sock, 5) == -1 )
{
perror("LISTEN FAIL");
}
while(1)
{
//Dentro del bucle infitino para que este el servidor siempre a la espera
s = accept(sock, (struct sockaddr *) &client, sizeof(client));
if(s == -1)
{
perror("ACCEPT FAIL");
}else{
process_msg(sock, &dnsTable);
}
}
return 0;
}
CLIENTE
Código:
void holamundo(int sock)
{
// esto es lo que se le enviara al servidor
int Opcode = 1;
int sizeop = 2; // Tamaño del Opcode de envio de la comanda holamundo
char buffer[125]; //LA operacion holamundo segun la tabla de opcode recibira 14 bytes (1 char -> 1 byte)
printf("HELLORQ\n");
stshort(Opcode, buffer); //stshort(01, buffer); stshort(HELLORQ, buffer);
printf("%d\n", Opcode);
if(send(sock, buffer, sizeof(buffer), 0) < 0)
{
perror("SEND IN CLIENT FAIL");
}
recv(sock, buffer, sizeof(buffer), 0);
printf("%s\n", buffer);
}
/*
* Function that process the menu option set by the user by calling
* the function related to the menu option.
* @param s The communications socket
* @param option the menu option specified by the user.
*/
void process_menu_option(int s, int option)
{
switch(option){
// Opció HELLO
case MENU_OP_HELLO:
holamundo(s);
break;
case MENU_OP_LIST:
process_list_operation(s);
break;
case MENU_OP_FINISH:
//TODO:
break;
default:
printf("Invalid menu option\n");
}
}
int main(int argc, char *argv[])
{
int port; // variable per al port inicialitzada al valor DEFAULT_PORT (veure common.h)
char host[MAX_HOST_SIZE]; // variable per copiar el nom del host des de l'optarg
int option = 0; // variable de control del menu d'opcions
int ctrl_options;
int sock;
char buffer[MAX_BUFF_SIZE];
int sizeop;
int Opcode;
int s = socket(AF_INET, SOCK_DGRAM,0); // He aqui el socket
//Control de error sobre los parametros recividos por referencia
ctrl_options = getProgramOptions(argc, argv, host, &port);
printf("que vale host aqui%s\n", host);
if(ctrl_options<0)
{
perror("No s'ha especificat el nom del servidor o el port\n\n");
close(s);
return -1;
}
struct sockaddr_in client;//struct in_client_from_server
client.sin_family=AF_INET;
client.sin_port=htonl(port);
client.sin_addr.s_addr=INADDR_ANY;
struct sockaddr_in server; //struct in_client_to_server
server.sin_family=AF_INET;
server.sin_port=htonl(DEFAULT_PORT);
setaddrbyname(&server,argv[2]);
bind()
//CLIENT MODE
//TODO: setting up the socket for communication
if(connect(s, (struct sockaddr *) &server, sizeof(server)) < 0)// CONECTING SOCKET
{
perror("CONNECT FAIL");
close(s);
exit(1);
}
//send(s, "HOLA", sizeof("HOLA"), 0);
//MENU & START
do{
printa_menu();
// getting the user input.
scanf("%d",&option);
printf("\n\n");
process_menu_option(s, option); // De forma original le enviava una variable s... si es el socket yo lo llamo sock
}while(option != MENU_OP_FINISH); //end while(opcio)
return 0;
}
Muestro tambien los resultados que me da el strace
SERVER
Código:
socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 3
bind(3, {sa_family=AF_INET, sin_port=htons(2222), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
listen(3, 5) = 0
accept(3,
CLIENT
Código:
socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 3
bind(3, {sa_family=AF_INET, sin_port=htons(2222), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
listen(3, 5) = 0
accept(3,