Socket, client (help-me)

This is a discussion on Socket, client (help-me) within the C Programming forums, part of the General Programming Boards category; I am beginning in C, I made a server in delphi and one client in C with some examples that ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    13

    Socket, client (help-me)

    I am beginning in C, I made a server in delphi and one client in C with some examples that I found in net... I obtained to make the two if to communicate, however I am with some problems in client... one of them is the BUFFER_SIZE that I have that to define a fixed value... and has commands that they return texts bigger that the buffer, this to make a menu in client, I have that to pressure to enter to receive the remain from the messages.... what of it has made a mistake with my code and what can be improved?

    thanks!!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <winsock.h>
    
    
    #define BUFFER_SIZE 256
    #define EXIT_CALL_STRING "FECHACON"
    
    int remote_socket = 0;
    int message_length = 0;
    
    unsigned short remote_port = 0;
    
    char remote_ip[32];
    char message[BUFFER_SIZE];
    
    
    struct sockaddr_in remote_address;
    
    WSADATA wsa_data;
    
    
    /* Exibe uma mensagem de erro e termina o programa */
    void msg_err_exit(char *msg)
    {
        fprintf(stderr, msg);
        system("PAUSE");
        exit(EXIT_FAILURE);
    }
    
    
    int main(int argc, char **argv)
    {
        if (WSAStartup(MAKEWORD(2, 0), &wsa_data) != 0)
            msg_err_exit("WSAStartup() failed\n");
    
        printf("IP do servidor: ");
        scanf("%s", remote_ip);
        fflush(stdin);
    
        printf("Porta do servidor: ");
        scanf("%d", &remote_port);
        fflush(stdin);
    
        remote_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (remote_socket == INVALID_SOCKET)
        {
            WSACleanup();
            msg_err_exit("socket() failed\n");
        }
    
        // preenchendo o remote_address (servidor)
        memset(&remote_address, 0, sizeof(remote_address));
        remote_address.sin_family = AF_INET;
        remote_address.sin_addr.s_addr = inet_addr(remote_ip);
        remote_address.sin_port = htons(remote_port);
    
    
        printf("conectando ao servidor %s...\n", remote_ip);
        if (connect(remote_socket, (struct sockaddr *) &remote_address, sizeof(remote_address)) == SOCKET_ERROR)
        {
            WSACleanup();
            msg_err_exit("connect() failed\n");
        }
    
    
        printf("digite as mensagens\n");
        do
        {
            // limpa o buffer
            memset(&message, 0, BUFFER_SIZE);
    
            printf("msg: ");
            gets(message);
            fflush(stdin);
    
            message_length = strlen(message);
    
            // envia a mensagem para o servidor
            if (send(remote_socket, message, message_length, 0) == SOCKET_ERROR)
            {
                WSACleanup();
                closesocket(remote_socket);
                msg_err_exit("send() failed\n");
            }
            
            //RECEBE MENSAGEM DO SERVIDOR
            // limpa o buffer
            memset(&message, 0, BUFFER_SIZE);
            // recebe a mensagem do cliente
            if (select(0,remote_socket,NULL,remote_socket,0)) {
              do{
                message_length = recv(remote_socket, message, BUFFER_SIZE, 0);
                if(message_length == SOCKET_ERROR)
                 {
                  WSACleanup();
                  closesocket(remote_socket);
                  msg_err_exit("recv() failed\n");
                 }
               printf("%s\n tamanho da msg = %d", message, (int)message_length);
               }while(select(0,remote_socket,NULL,remote_socket,0) == 0);
              }
            
            
        }
        while(strcmp(message, EXIT_CALL_STRING)); // sai quando enviar um "#quit" para o servidor
    
    
        printf("encerrando\n");
        WSACleanup();
        closesocket(remote_socket);
    
    
        system("PAUSE");
        return 0;
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    memset(&message, 0, BUFFER_SIZE);
    memset is expecting just the base pointer of your string, not pointer to pointer of your string. Just replace &message to message. Perhaps you will have to change that in quite a few places in your code.

    And please avoid using gets, fflush, system (Have a look at our FAQ). There are reply dangerous function. Have you enables your warning flag yet. If not do that first

    Code:
    gcc -Wall -W <filename>
    Which compiler are u using?

    ssharish

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    I use the DEV-C++,

    you obtained to understand my problem? it is everything functioning, I only have problems when receiving the message because of the size from the buffer!

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I'm not sure I totally understand your problem. But is it possible to have your server send a prior message indicating the size of the buffer needed? This will allow you to dynamically allocate storage for the actual message that is causing the storage issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 06:17 PM
  2. Socket Client Detect Disconnected Equip.
    By sergioms in forum C# Programming
    Replies: 2
    Last Post: 01-25-2009, 06:49 AM
  3. Socket Programming Problem!!!!
    By bobthebullet990 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-21-2008, 07:36 PM
  4. socket web client
    By Abila in forum C Programming
    Replies: 0
    Last Post: 06-28-2003, 05:42 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 12:48 PM

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