简体   繁体   English

C编程TCP服务器和客户端连接错误

[英]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. 我想使用C编程中的TCP套接字连接在虚拟专用服务器和主机之间建立TCP连接。

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? 我是否在client.c中直接调用了vps的IP?

This is my host machines client.c code 这是我的主机client.c代码

#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 现在这是我的VPS服务器的代码。

#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 注意:此代码有时会起作用,但大多数情况下不会

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, 另一件事:您应该使用perror函数显示错误消息,例如,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM