LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Socket problem - C/Linux (https://www.linuxquestions.org/questions/programming-9/socket-problem-c-linux-922517/)

fachamix 01-06-2012 09:20 PM

Socket problem - C/Linux
 
I have the following code.
Dont pay atention to the SPANISH outputs (I am from Argentina).

The problem is that when I run it, starts fine, and waits for a client to connect.
Once the client connects, the client can send a datablock.
Once received (datablock) the server automaically hungs and exits.

here the code...

Code:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
    printf("Iniciando servidor ...\n\r");
    int server_sockfd, client_sockfd;
    struct sockaddr_in server_dir, client_dir;
    int server_len, client_len;
    char buffer[1024];

    server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    server_dir.sin_family = AF_INET;
    server_dir.sin_addr.s_addr = inet_addr("10.0.0.4");
    server_dir.sin_port = htons(9981);

    server_len = sizeof(server_dir);
    bind(server_sockfd,(struct sockaddr*)(&server_dir),server_len);

    listen(server_sockfd,5);

    while (1)
    {
        printf("Esperando clientes ...\n\r");

        client_len = sizeof(client_dir);
        client_sockfd = accept(server_sockfd,(struct sockaddr*)&client_dir,(socklen_t*)&client_len);
        if (client_sockfd == -1)
        {
            perror("DEBUG");
            exit(EXIT_FAILURE);
        }
        printf("Cliente conectado, esperando datos ...\n\r");
        while (1)
        {
            memset(buffer,'\0',sizeof(buffer));
            int temp = read(client_sockfd,buffer,sizeof(buffer));
            buffer[strlen(buffer) - 2] = '\0';
            printf("Leidos %d \n\r",temp);
            printf("Datos recibidos [%s]\n\r",buffer);
            if (strcmp(buffer,"quit") == 0)
            {
                break;
            }
            printf("Enviando respuesta de ECHO\n\r"); //ESTO SI SE EJECUTA
            strcpy(buffer,"DATOS-RECIBIDOS"); //ESTO TAMBIEN
            write(server_sockfd,buffer,sizeof(buffer));//I BELIEVE HERE IS THE PROBLEM BUT I ITS JUST A THOUGHT, I DONT KNOW HATS WRONG.
        }
        close(client_sockfd);
        printf("Fin de conexion.\n\r");
    }
    close(server_sockfd);
    exit(EXIT_SUCCESS);
}

HOW I EXECUTE IT ?

I compile this code an run the server.

once compiled and executed, I open a new terminal, and execute "telnet <IP> <PORT>" in this case "telnet 10.0.0.4 9981" , and I can succesfully connect, but I can only send 1 packet and then the server terminates.

Please, need some advice.

NevemTeve 01-07-2012 01:25 AM

Code:

old: write(server_sockfd,buffer,sizeof(buffer));
new: write(client_sockfd,buffer,strlen(buffer));


fachamix 01-07-2012 06:27 AM

thanks very very much my friend.

it works perfectly now.


I should not being programming hard at night next time :D

thanks.


good bye

paulsm4 01-07-2012 01:02 PM

One additional suggestion:

Code:

write(client_sockfd,buffer,strlen(buffer)+1);

fachamix 01-07-2012 04:42 PM

thanks you too for the sug.


All times are GMT -5. The time now is 08:10 PM.