0

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

14
  • 2
    Compare your code with geeksforgeeks.org/socket-programming-cc. Note that their code has a lot more error handling than yours. Commented Sep 1, 2018 at 20:04
  • 2
    Could you add some details on what you mean by "doesn't work most of the time"? Commented Sep 1, 2018 at 20:06
  • 1
    First you say "The serverside code is good and runs flawlessly", but then "this code works some times but then most of the time it doesnt". And there's no clear problem description. Please show exactly what you're doing, what happens as a result, and what you expected to happen isntead. Commented Sep 1, 2018 at 20:42
  • 1
    Try using a debugger on both ends of the session. Your server will need to loop, waiting for incoming requests. It looks like you wait for one request and exit right now. Commented Sep 1, 2018 at 20:43
  • 2
    This isn't even real code (just from looking at it I count at least three errors that prevent compilation). Please show the actual code you're using. See minimal reproducible example. Commented Sep 1, 2018 at 20:44

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

After applying these changes the client no longer receives the server message even on the first try.

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.