3

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.

1 Answer 1

7

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Another problem is that recv doesn't put a string in the buffer, just the bytes received. It's fine if the \0 of the string is amongst the bytes received, but it is not guaranteed that that the first recv will return the entire message sent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.