1

I'm writing a multithreaded C++ server and a Java client using TCP. It works fine for short strings, but when I want to send big strings (e.g. 40 characters), the server only receives a few of them, and the client is waiting for the response.

The multithreading part works fine.

Here you have the code. (Sorry for the comments and names of variables. I'm Spanish.)

Server C++

void* SocketHandler(void*);


int main(int argv, char** argc){



//Puerto en el que recibe
int host_port= 1101;

//Estructura usada para especificar una direccion local o remota a la que conectar un socket
struct sockaddr_in my_addr;

int hsock;
int * p_int ;
int errno;

socklen_t addr_size = 0;
int* csock;
sockaddr_in sadr;
pthread_t thread_id=0;

//Se inicializa socket
hsock = socket(AF_INET, SOCK_STREAM, 0);
//Para comprobar que el socket se ha inicializado correctamente
if(hsock == -1){
    printf("Error inicializando socket %d\n", errno);
    goto FINISH;
}

//Se reserva memoria
p_int = (int*)malloc(sizeof(int));
*p_int = 1;

//Se comprueba que al introducir las opciones del socket se introduzcan correctamente   
if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )||
    (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){
    printf("Error introduciendo opciones del socket %d\n", errno);
    //Se libera la memoria reservada
    free(p_int);
    goto FINISH;
}
//Se libera la memoria reservada
free(p_int);

//Se añade la familia de direcciones a la que pertenece IPV4
my_addr.sin_family = AF_INET ;
//Se añade el puerto que es
my_addr.sin_port = htons(host_port);

//Relleno de sin zero con 8 ceros
bzero((char *) &(my_addr.sin_zero), sizeof(my_addr.sin_zero));
//memset(&(my_addr.sin_zero), 0, 8);

//Se añade la dirección IP
my_addr.sin_addr.s_addr = INADDR_ANY ;

//Enlaza el socket con la dirección IP, puerto
if( bind( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == -1 ){
    fprintf(stderr,"Error enlazando el socket, asegurate de que no hay nada más escuchando en este puerto %d\n",errno);
    goto FINISH;
}
//Se pone a escuchar
if(listen( hsock, 5) == -1 ){
    fprintf(stderr, "Error listening %d\n",errno);
    goto FINISH;
}


//Ahora se pasa a hacer las cosas en el servidor


addr_size = sizeof(sockaddr_in);

while(true){
    printf("Esperando a una conexión\n");
    //Se reserva memoria 
    csock = (int*)malloc(sizeof(long double));
    //Hay que encontrar porque no manda mas de x caracteres


    //LLamada que se bloquea esperando una conexion de un cliente
    if((*csock = accept( hsock, (struct sockaddr*)&sadr, &addr_size))!= -1){
        printf("---------------------\nRecibida conexión de %s\n",inet_ntoa(sadr.sin_addr));
        //Se crea un nuevo hilo por cliente, se llama a socket handler
        pthread_create(&thread_id,0,&SocketHandler, (void*)csock );
        //El almacenamiento del hilo puede ser reclamado cuando el hilo haya terminado
        pthread_detach(thread_id);
    }
    else{
        fprintf(stderr, "Error aceptando cliente %d\n", errno);
    }
}

FINISH:
;
}

void* SocketHandler(void* lp){
int *csock = (int*)lp;

//Buffer en donde se guarda lo recibido
char buffer[8192];
//Longitud del buffer
//int buffer_len = 8192;
//Contador para saber el número de caracteres del buffer
int bytecount;

//Relleno del buffer con ceros
bzero((char *) &buffer, sizeof(buffer));
//memset(buffer, 0, sizeof(buffer));
//Se recibe la informacion del socket y se comprueba que sea valida

//recv(buffer,offset,size,socketflags) 
//buffer es un array de bytes que es la localización en donde se van a guardar los datos
//offset es la posicion en el buffer de datos desde la cual se quiere empezar a guardar
//size es el número de bytes a recibir
//socketflags es la combinacion de flas que se quieren utilizar 0 significa ninguno 
/*if((bytecount = recv(*csock, buffer, sizeof(buffer), 0))== -1){
    fprintf(stderr, "Error recibiendo los datos %d\n", errno);
    goto FINISH;
}*/


if((bytecount = read(*csock,buffer,sizeof(buffer)))== -1){
        fprintf(stderr, "Error recibiendo los datos %d\n", errno);
    goto FINISH;
}

printf("Bytes recibidos %d\nstring recibido %s\n", bytecount, buffer);
//Copia el string al buffer
//strcat(buffer, " SERVER ECHO");




//Se tienen que poner parentesis por el uso de go to de arriba
{       


    string comando = buffer;


    if(comando.compare("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz") == 0){

        strcpy(buffer,"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");


    }

}


//Envía el mensaje de vuelta con el string añadido

//send(buffer,offset,size,socketflags) 
//buffer es un array de bytes que contiene los datos a ser enviados
//offset es la posicion en el buffer de datos desde la cual se quiere empezar a enviar datos
//size es el número de bytes a enviar
//socketflags es la combinacion de flas que se quieren utilizar 0 significa ninguno

{
    //Se hace esto ya que en el cliente parece que si no le llegua salto de linea no continua, y se queda esperando
string bufferbarran =  buffer;
bufferbarran +="\n";    
strcpy(buffer,bufferbarran.c_str());
/*
if((bytecount = send(*csock, buffer, strlen(buffer), 0))== -1){
    fprintf(stderr, "Error enviando los datos %d\n", errno);
    goto FINISH;
}
*/
if((bytecount = write(*csock,buffer,strlen(buffer)))== -1){
        fprintf(stderr, "Error enviando los datos %d\n", errno);
    goto FINISH;
}
}

printf("Bytes enviados %d\n", bytecount);


FINISH:
//Se libera la memoria reservada

free(csock);

return 0;
}
mjk
  • 2,443
  • 4
  • 33
  • 33
Kydo2
  • 15
  • 3

2 Answers2

1

Instead of calling read() directly in the socket server (and client), you should use select() to know when the socket is ready to be read or written. Its considered bad practice to just call read/write without calling select(). select() will block until the socket is ready to be read/written or until it times out, if you configure a timeout.

Here is a good example where they use select(): c++ Socket select and receive problem

Community
  • 1
  • 1
Brady
  • 10,207
  • 2
  • 20
  • 59
  • It's ok to use blocking read/write calls when appropriate for the app, surely? It may not scale as well to many sockets as a reactor, but not everything needs to. – Useless May 29 '12 at 13:47
  • @Useless, It may be ok, but its not advisable. If he's learning sockets, etc why not start out correctly? – Brady May 29 '12 at 13:51
  • I add select() but, the problem was in the client, I don't know why but, when I use readLine and I introduce zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz, doesn't work fine, but when I use a string, the java client send the data perfectly. – Kydo2 May 29 '12 at 17:05
  • @Kydo2, glad to hear it works now, happy to help. You could try posting the client code too (here or in another question) and we can take a look (let me know when, and I'll try to help). Hasta pronto desde Madrid. – Brady May 29 '12 at 17:32
  • Thank you for your help! If I have another another question I will ask over here. – Kydo2 May 30 '12 at 12:32
1

TCP is a stream-oriented transport - the correspondance between calls to write and read is not 1:1 (as it would be for a datagram-oriented transport like UDP).

You need to keep reading, in a loop, until you've received one (or more) whole messages. It's up to you to encode some way of figuring out where a message ends.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • Poor choice of words describing TCP as stream-oriented. UDP is typically used for streaming audio/video. But you're correct that he needs to make sure he reads ALL of the data. Select() would help knowing there's more data :) – Brady May 29 '12 at 13:50
  • Conventional though: it has type `SOCK_STREAM`, and behaves a bit like an `iostream` (in that they're both bidirectional and byte- or character-oriented). This naming scheme precedes the popular use of datagram transport for _lossy_ multimedia streams. – Useless May 29 '12 at 14:03