Well, my problem is this:
When trying to compile this code in linux (gcc server.cpp -o server ):
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
using namespace std;
const int TAM_COLA = 10;
const int TAM_TEXTO = 80;
const int ESPERASEG = 0;
const int ESPERAMILISEG = 1;
/*
int EnterPulsado();
int CrearSocket(unsigned short puerto);
void CerrarSockets(int sock, int cola, int num);
void CerrarSocket(int sock, int num);
int AceptarConexion(int sock, int cola, int num);
int Leer(int sock, int cola, int* num, char buffer);
int Escribir(int sock, int cola, int num, char buffer, int cont);
*/
int EnterPulsado()
{
fd_set conjunto;
struct timeval t;
FD_ZERO(&conjunto);
FD_SET(0,&conjunto); /* 0 es el identificador del socket de stdin */
t.tv_sec=0;
t.tv_usec=1000; /* 1 milisegundo */
return select(1,&conjunto,NULL,NULL,&t); /* En caso de error devuelve 1 y
saldremos del programa. */
}
int CrearSocket(unsigned short puerto)
{
int sock;
struct sockaddr_in dir;
//Comprobación socket
if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
perror("socket");
exit(0);
}
//Asignacion de puerto al socket
dir.sin_family=PF_INET;
dir.sin_port=htons(puerto);
dir.sin_addr.s_addr=htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr *)&dir,sizeof(struct sockaddr_in))!=0)
{
perror("bind");
exit(0);
}
}
void CerrarSocket(int sock, int num)
{
if (close(sock)<0)
{
perror("socket");
exit(0);
}
else
num--;
}
void CerrarSockets(int sock, int *cola, int num)
{
int aux = 0;
for(int i = 0; i < num; i++)
{
aux = cola[i];
CerrarSocket(aux, num);
}
num++;
CerrarSocket(sock, num);
}
int AceptarConexion(int sock, int *cola, int num)
{
int accept(int s, struct sockaddr *addr, int *addrlen);
int sock_conectado;
struct sockaddr_in s;
int aux = TAM_COLA;
if ((sock_conectado = accept(sock,(struct sockaddr *)&s,&aux)) < 0)
{
perror("accept");
exit(0);
}
else
num++;
return num;
}
int Leer(int sock, int *cola, int* num, char* buffer) ///< Si se queja, dejar buffer como char
{
int n, s;
fd_set conjunto;
struct timeval timeout;
//select
FD_ZERO(&conjunto);
FD_SET(sock, &conjunto);
timeout.tv_sec = ESPERASEG;
timeout.tv_usec = ESPERAMILISEG;
if ((n = select(s+1, &conjunto, NULL, NULL, &timeout)) < 0)
{
perror("select");
exit(0);
}
//Leer
if ((n = read(s, buffer, TAM_COLA)) < 0)
{
perror("read");
exit(0);
}
return n;
}
int Escribir(int sock, int *cola, int num, char* buffer, int cont)
{
int write(int fd, void *buf, int num);
int n, s;
fd_set conjunto;
struct timeval timeout;
//select
FD_ZERO(&conjunto);
FD_SET(s, &conjunto);
timeout.tv_sec = ESPERASEG;
timeout.tv_usec = ESPERAMILISEG;
if ((n = select(s+1, &conjunto, NULL, NULL, &timeout)) < 0)
{
perror("select");
exit(0);
}
//write
if (write(s, buffer, strlen(buffer))==-1)
{
perror("write");
exit(0);
}
}
int main(int argc,char *argv[])
{
char* buffer[TAM_COLA][TAM_TEXTO+1];
int sock,cola[TAM_COLA],num,cont;
/*if (argc!=2)
Uso(argv[0]);*/
sock=CrearSocket((unsigned short)atoi(argv[1])); /* Creamos el socket. */
num = 0;
while (EnterPulsado()==0) /* Mientras no pulsemos la tecla [Enter]. */
{
num = AceptarConexion(sock,cola,num); /* Miramos si hay conexiones nuevas. */
if ((cont = Leer(sock,cola,&num,&buffer)) > 0) /* Miramos si tenemos datos para leer. */
Escribir(sock,cola,num,&buffer,cont); /* Enviamos los datos leidos. */
else
CerrarSocket(sock, num);
}
CerrarSockets(sock,cola,num); /* Cerramos todas las conexiones. */
return 0;
}
I get the following error:
server.cpp: En la función ‘int main(int, char**)’:
server.cpp:192:52: error: no se puede convertir ‘char* (*)[10][81]’ a ‘char*’ para el argumento ‘4’ para ‘int Leer(int, int*, int*, char*)’
if ((cont = Leer(sock,cola,&num,&buffer)) > 0) /* Miramos si tenemos datos para leer. */
^
server.cpp:193:52: error: no se puede convertir ‘char* (*)[10][81]’ a ‘char*’ para el argumento ‘4’ para ‘int Escribir(int, int*, int, char*, int)’
Escribir(sock,cola,num,&buffer,cont); /* Enviamos los datos leidos. */
How can I correct it?