True Brendan
True Brendan

Reputation: 9

C programming TCP server and client connection errors

I want to make a TCP connection between my Virtual private server and my host machine using a TCP socket connection in C programming.

The serverside code is good and runs flawlessly. Its the client side that only returns the string that the server is supposed to send out on the FIRST attempt of running it. After that the code doesnt work anymore and i have to restart my terminal and recompile the code for it to work again.

am i doing it right? did i call the IP of my vps right in my client.c?

This is my host machines client.c code

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>


int main()
{
    // create a socket
    int mySocket;
    mySocket = socket(AF_INET, SOCK_STREAM, 0);

    //specify an address structure for the socket

    struct sockaddr_in server_address;
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(666);
    server_address.sin_addr.s_addr = inet_addr("IP OF MY VPS");


    int connection_status = connect(mySocket, (struct sockaddr *) &server_address, sizeof(server_address));
    //check for error with the connection
    if (connection_status == -1) {
    printf("There was an error making a connection to the remote socket \n\n");
    exit(1);
    }
// recieve data from the server
    char server_response[256];
    recv(mySocket, &server_response, sizeof(server_response), 0);



    // pritn out the server's response
    printf("The server sent the data: %s\n \n",server_response);

    close(mySocket);

    return 0;
}

Now here is the code for my VPS's server.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
int main()
{
char server_message[256] = "client has connected";

int server_socket;
server_socket = socket(AF_INET, SOCK_STREAM, 0);

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

bind(server_socket, (stuct sockaddr*) &server_address, 
sizeof(server_address));

listen(server_socekt, 5);
int client_socket;
client_socket = accept(server_socket, NULL, NULL);

send(client_socket, server_message,sizeof(server_message), 0);
close(server_socket);
return 0;
}

note: this code works some times but then most of the time it doesnt

Upvotes: 0

Views: 2107

Answers (1)

Mathieu
Mathieu

Reputation: 9619

You have no processing loop in the server: each time a client connects, after sending it a message, the server stops listening and terminates.

You can correct the problem in the server:

/* listen for new clients */ 
listen(server_socket, 5);

while (1)
{
    int client_socket;
    /* wait for a new client */
    client_socket = accept(server_socket, NULL, NULL);

    /* send the message */
    send(client_socket, server_message,sizeof(server_message), 0);

    /* and close only the client socket, not the listening one*/
    close(client_socket);
}

/* Once the while loop is finished, you can stop listen (up to you to
   change the while loop condition)*/
close(server_socket);

Another thing: you should use perror function to display errors messages, for instance,

int connection_status = connect(mySocket, (struct sockaddr *) &server_address, sizeof(server_address));
//check for error with the connection
if (connection_status == -1) {
    perror("connect");
    exit(1);
}

will give you this kind of message on error:

connect: Connection refused

Upvotes: 1

Related Questions