Eric Garcia
Eric Garcia

Reputation: 87

Socket Programming: Client printing garbage string

I am new to Network Programming and the first program we were supposed to do in our lab was a program where two systems, client and the server send messages to each other. My program has no compiler errors, but whenever a message is sent over the network, it is not being displayed properly on the other end. Instead an unreadable box is being displayed (I think a garbage string is being printed but don't know why).

View the screenshot here

The codes:

client.c

int main()
{
    char server_message[256]="You have reached the server!!\n";
    //create a socket
    int nw_socket;
    nw_socket=socket(AF_INET,SOCK_STREAM,0);

    //specify address for socket

    struct sockaddr_in server_address;
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(9999);
    server_address.sin_addr.s_addr = INADDR_ANY;

    //establish a connection
    int connection_status = connect(nw_socket, (struct sockaddr *) & server_address, sizeof(server_address));

    //check for errors

    if(connection_status==-1)
    {
        perror("There was a problem initiating the connection.\n");
    }
    else
    {
        printf("The connection was successful.\n");
    }

    int send_val = send(nw_socket,server_message,sizeof(server_message),0);
    if(send_val==-1)
        perror("\nError while sending: ");
    //receive data

    char data[256];
    int recv_val=recv(nw_socket,data,sizeof(data),0);
    if(recv_val==-1)
        perror("\nError while receiving: ");

    printf("The server's response was: %s\n",data );

    //close the socket

    close(nw_socket);

    return 0;
}

server.c

int main()
{
    char server_message[256]="You have reached the server!!\n";

    //create a new socket
    int server_socket=socket(AF_INET,SOCK_STREAM,0);

    struct sockaddr_in server_connection;
    server_connection.sin_family=AF_INET;
    server_connection.sin_port=htons(9999);
    server_connection.sin_addr.s_addr=INADDR_ANY;

    int bind_val=bind(server_socket, (struct sockaddr *) & server_connection, sizeof(server_connection));
    if(bind_val==-1)
        perror("\nError while binding: ");
    listen(server_socket,6);

    int client_socket=accept(server_socket, NULL, NULL);
    if(client_socket==-1)
        perror("\nError while accepting: ");

    int send_val=   send(server_socket,server_message,sizeof(server_message),0);
    if(send_val==-1)
        perror("\nError while sending: ");

    char data[256];
    recv(server_socket,data,sizeof(data),0);
    close(server_socket);

    return 0;
}

How do I fix this? Thanks in advance.

Upvotes: 3

Views: 294

Answers (1)

Klas Lindbäck
Klas Lindbäck

Reputation: 33283

You are using the listening socket when sending a message in the server:

int send_val=   send(server_socket,server_message,sizeof(server_message),0);

You should be using the socket connected to the client:

int send_val = send(client_socket,server_message,sizeof(server_message),0);

Upvotes: 7

Related Questions