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

  • 2
    Compare your code with https://www.geeksforgeeks.org/socket-programming-cc/. Note that their code has a lot more error handling than yours. – Robert Harvey Sep 01 '18 at 20:04
  • What is not working? The server or the client or both? You sometimes ignore return values (`bind()`, `listen()` and others return success or failure, check them out and check the `errno` if they indicate failure). – Roman Hocke Sep 01 '18 at 20:04
  • 2
    Could you add some details on what you mean by "doesn't work most of the time"? – user2653663 Sep 01 '18 at 20:06
  • The server works flawlessly with no problems. Its the client. When i run it, it returns what the server sends out to all clients on the first time i run the client. but after that it never works again and i have to restart my terminal to do it, and even after i do that most of the time it still doesnt work – True Brendan Sep 01 '18 at 20:10
  • Am i connecting it to my VPS server properly? am i calling the IP of my server the right way – True Brendan Sep 01 '18 at 20:11
  • 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. – melpomene Sep 01 '18 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. – Will Bickford Sep 01 '18 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 [mcve]. – melpomene Sep 01 '18 at 20:44
  • 1
    To allow server side socket reuse, in the server, you may want to add: `int enable = 1; setsockopt(server_socket, SOL_SOCKET, (SO_REUSEPORT | SO_REUSEADDR), &enable, sizeof(enable));` after `socket` and _before_ the `bind` to allow multiple clients to connect (or to ctrl-c your client and rerun it) – Craig Estey Sep 01 '18 at 20:44
  • See my answer here: https://stackoverflow.com/questions/37497556 – Craig Estey Sep 01 '18 at 20:51
  • @melpomene what do you not understand about the sentence "The server side works fine, its the client that is messing up"? – True Brendan Sep 02 '18 at 00:35
  • 1
    the posted server code does not compile, so it NEVER works fine – user3629249 Sep 02 '18 at 01:30
  • the posted server code is (at least) missing the statement:: `#include ` – user3629249 Sep 02 '18 at 01:32
  • @TrueBrendan What do you not understand about the sentence "*note: this code works some times but then most of the time it doesnt*" on the server code? – melpomene Sep 02 '18 at 09:22

1 Answers1

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

Mathieu
  • 8,840
  • 7
  • 32
  • 45