Problem with POSIX sockets

This is a discussion on Problem with POSIX sockets within the C Programming forums, part of the General Programming Boards category; First, thats my first post in this forum so hello everybody Well, I'm trying to write my first C application. ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    5

    Question Problem with POSIX sockets

    First, thats my first post in this forum so hello everybody

    Well, I'm trying to write my first C application. For now I want to do a primitive chat that allows to save the conversations in a txt file.

    The chat will be for POSIX systems and it will use AF_INET / SOCK_STREAM sockets (TCP), it code compiles fine with gcc 4.1.1, the problem comes when I run the program, because the server only receives the first message, after that the client runs out.

    The code is very simple but Im unable to find the bug, please try to give me a hand.

    Regards & Thanks, here is the code:

    PD: Yes, I'm spanish

    _____________
    programilla_commons.h
    ________________

    Code:
    #ifndef __PROGRAMILLA_COMMONS__
    
    #define __PROGRAMILLA_COMMONS__
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    #define ERROR(numerror,errno) { printf("Error: %s // %s \n", errores[numerror-1], strerror (errno)); }
    #define SERVER_IP "127.0.0.1"
    #define SERVER_PORT 6969
    #define MAX 255
    #endif
    
    char errores[10][45] = {"1, Apertura del fichero",
                           "2, Escritura del fichero",
                           "3, Cerradura del fichero",
                           "4, Lectura del mensaje en el socket",
                           "5, Escritura del mensaje en el socket",
                           "6, Apertura del socket del server",
                           "7, Publicidad del socket del server",
                           "8, Acceptar la conexion del cliente",
                           "9, Apertura del socket del cliente",
                           "10, Peticion de conexion del cliente"};
    
    const char nombre_txt[20] = "mitxt";
    ________________
    programilla-server.c
    ________________

    Code:
    #include "programilla-commons.h"
    
    leer_mensaje (int new_socket_fd, char* mensaje)
    {
           int errno;
           int nbytes;
    
           if (( nbytes = read (new_socket_fd, mensaje, MAX)) == -1)
                   ERROR(4,errno);
           printf(mensaje);
    }
    
    int main()
    {
           int errno;
           int pid;
           int socket_fd, new_socket_fd;
           char *mensaje = (char *) malloc (255);
    
           struct sockaddr_in server_addr, client_addr;
    
           int client_addr_length;
    
           if ((socket_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
                   ERROR(6,errno);
    
           server_addr.sin_family = AF_INET;
           server_addr.sin_addr.s_addr = inet_addr (SERVER_IP);
           server_addr.sin_port = htons (SERVER_PORT);
    
           if ((bind (socket_fd, (struct sockaddr *) &server_addr, sizeof (server_addr))) == -1)
                   ERROR(7,errno);
    
           listen (socket_fd, 5);
    
           for (;;) {
                   client_addr_length = sizeof (client_addr);
    
                   if ((new_socket_fd = accept (socket_fd, (struct sockaddr *) &client_addr, &client_addr_length)) == -1)
                           ERROR(8,errno);
    
                   if ((pid = fork ()) == -1)
                   {
                           ERROR(15,errno)
                   }
                   else if (pid == 0) {
                           close (socket_fd);
                           leer_mensaje (new_socket_fd, mensaje);
                           close (new_socket_fd);
                           exit (0);
                   }
                           close (new_socket_fd);
           }
    
    
    return 0;
    
    }
    ________________
    programilla-cliente.c
    ________________

    Code:
    #include "programilla-commons.h"
    
    escribir_mensaje (int* socket_fd, char* mensaje)
    {
           int errno;
           int nbytes;
    
           if ((nbytes = write (*socket_fd, "aaa", MAX)) == -1)
                   ERROR(5,errno);
           printf("Enviado!");
    }
    
    int main()
    {
           int errno;
           bool fin = false;
           FILE *txt;
           int socket_fd;
           char *mensaje = (char *) malloc (255);
    
           struct sockaddr_in server_addr;
    
           if ((socket_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
                   ERROR(9,errno);
    
           server_addr.sin_family = AF_INET;
           server_addr.sin_addr.s_addr = inet_addr (SERVER_IP);
           server_addr.sin_port = htons (SERVER_PORT);
    
           if (connect (socket_fd, (struct sockaddr *) &server_addr, sizeof (server_addr)) == -1)
                   ERROR(10,errno);
    
           if ((txt = fopen (nombre_txt, "w")) == NULL)
                   ERROR(1,errno);
    
           printf ("Introduce una linea (END para acabar)\n"); fflush (stdout);
           do {
                   fgets (mensaje, MAX, stdin);
    
                   if ((strcmp (mensaje, "END\n")) == 0)
                   {
                           fin = true;
                           break;
                   }
    
                   if ((fprintf (txt, mensaje)) < 0)
                   ERROR(2,errno);
    
                   escribir_mensaje (&socket_fd, mensaje);
    
           }
           while (fin != true);
    
           if ((fclose (txt)) != 0)
                   ERROR(3,errno);
    
           close (socket_fd);
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Hey, I don't have unix installed so I can't compile that to test it out. What do you mean the client runs out?

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    Quote Originally Posted by Brian
    Hey, I don't have unix installed so I can't compile that to test it out. What do you mean the client runs out?
    Well, the server only receives the first message, after that I send 2 more messages and the client exits without errors. The server stills alive.

    In fact I don't understand why only one message (the first one) is received.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Sockets - recv() problem
    By Mercurius in forum Networking/Device Communication
    Replies: 3
    Last Post: 05-15-2006, 08:49 AM
  2. Sockets and UDP problem
    By ally153 in forum C Programming
    Replies: 5
    Last Post: 03-20-2006, 05:19 AM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21