首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何正确获取访问web浏览器的c++服务器代码?

如何正确获取访问web浏览器的c++服务器代码?
EN

Stack Overflow用户
提问于 2021-03-01 02:19:03
回答 2查看 55关注 0票数 0

以下是我对消息执行的代码,但由于某种原因,我一直收到一条消息:“连接到套接字时出现问题!对不起!”然后是GET消息:

代码语言:javascript
运行
复制
Server says:
GET / HTTP/1.1
Host: 006010fb-d2cb-4a56-9e26-21bf35548134.id.replitusercontent.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36
Accept: text/html,application/xhtml+xml,apP�s� 

谁能检查一下这个,告诉我我遗漏了什么/做错了什么?这是我第一次做服务器/客户端编码。

代码语言:javascript
运行
复制
/**
 * @brief Main function of the program. It is called like this "program <port> <encryptedMessage>",
 * where <port> means the address of the webserver and <encryptedMessage> is the message to show
 * 
 * @param argc number of arguments
 * @param argv pointer to arguments
 * @return int 0 if everything is ok
 */
 
//Basic TCP Client: socket() creation > connect() > receive() > display > close

#include <stdio.h>          //Standard library
#include <stdlib.h>         //Standard library
#include <sys/socket.h>     //API and definitions for the sockets
#include <sys/types.h>      //more definitions
#include <netinet/in.h>     //Structures to store address information
#include <unistd.h>         //close function
#include <string.h>  
#define PORT 8080

int main()
{
    char tcp_server_message[256] = " Hello, I am the TCP Server you successfully connected to!! \n\n               Bye Bye!!!\n\n";
    //create the server socket
    int tcp_server_socket;                                  //variable for the socket descriptor
    tcp_server_socket = socket(AF_INET, SOCK_STREAM, 0);    //calling the socket function. Params: Domain of the socket (Internet in this case), type of socket stream (TCP), Protocol (default, 0 for TCP)

    //creating the TCP socket
    int tcp_client_socket;                                    //Socket descriptor
    tcp_client_socket = socket(AF_INET, SOCK_STREAM, 0);      //Calling the socket function - args: socket domain, socket stream type, TCP protocol (default)

    //specify address and port of the remote socket
    struct sockaddr_in tcp_server_address;             //declaring a structure for the address
    tcp_server_address.sin_family = AF_INET;           //Structure Fields' definition: Sets the address family of the address the client would connect to
    tcp_server_address.sin_port = htons(PORT);        //Specify and pass the port number to connect - converting in right network byte order
    tcp_server_address.sin_addr.s_addr = INADDR_ANY;   //Connecting to 0.0.0.0
    
    // binding the socket to the IP address and port
    bind(tcp_server_socket, (struct sockaddr *) &tcp_server_address, sizeof(tcp_server_address));  //Params: which socket, cast for server address, its size

    //listen for simultaneous connections
    listen(tcp_server_socket, 5);  //which socket, how many connections
    // server socket to interact with client, structure like before - if you know - else NULL for local connection
    tcp_client_socket = accept(tcp_server_socket, NULL, NULL);
    
    //send data stream
    send(tcp_client_socket, tcp_server_message, strlen(tcp_server_message), 0);  // send where, what, how much, flags (optional)
    //connecting to the remote socket
    int connection_status = connect(tcp_client_socket, (struct sockaddr *) &tcp_server_address, sizeof(tcp_server_address)); //params: which socket, cast for address to the specific structure type, size of address
    if (connection_status == -1)
    {   //return value of 0 means all okay, -1 means a problem
        printf(" Problem connecting to the socket! Sorry!! \n");
    }
    char tcp_server_response[256];
    recv(tcp_client_socket, &tcp_server_response, sizeof(tcp_server_response), 0);   // params: where (socket), what (string), how much - size of the server response, flags (0)

    //Output, as received from Server
    printf("\n\n Server says: %s \n", tcp_server_response);

    //closing the socket
    close(tcp_client_socket);

    return 0;
}

run.bash

代码语言:javascript
运行
复制
#!/bin/bash
# DO NOT MODIFY THIS FILE

# This script compiles and runs the webserver.

c++ -o src/serveMessage src/main.cpp

src/serveMessage 8080 "We will meet at midnight"
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-01 05:08:25

您需要将程序分为服务器部分和客户端部分。首先执行服务器程序,并使其侦听客户端的访问。然后执行客户端程序。这是服务器部分(Server.cpp)。

代码语言:javascript
运行
复制
    :
    :
int main()
{
    char tcp_server_message[256] = " Hello, I am the TCP Server you successfully connected to!! \n\n               Bye Bye!!!\n\n";
    
    //create the server socket
    int tcp_server_socket;                                  //variable for the socket descriptor
    tcp_server_socket = socket(AF_INET, SOCK_STREAM, 0);    //calling the socket function. Params: Domain of the socket (Internet in this case), type of socket stream (TCP), Protocol (default, 0 for TCP)

    //creating the TCP socket
    int tcp_client_socket;                                    //Socket descriptor
    //tcp_client_socket = socket(AF_INET, SOCK_STREAM, 0);      //Calling the socket function - args: socket domain, socket stream type, TCP protocol (default)

    //specify address and port of the remote socket
    struct sockaddr_in tcp_server_address;             //declaring a structure for the address
    tcp_server_address.sin_family = AF_INET;           //Structure Fields' definition: Sets the address family of the address the client would connect to
    tcp_server_address.sin_port = htons(PORT);        //Specify and pass the port number to connect - converting in right network byte order
    tcp_server_address.sin_addr.s_addr = INADDR_ANY;   //Connecting to 0.0.0.0
             

    // binding the socket to the IP address and 
    //Params: which socket, cast for server address, its size        
    if (bind(tcp_server_socket, (struct sockaddr *) &tcp_server_address, sizeof(tcp_server_address)) < 0)
    {
        printf("Error binding\n");
    } 
    else
    {
        printf("Bind successfully!\n");
    }

    //listen for simultaneous connections
    if(listen(tcp_server_socket, 5) < 0) {      // listen for incoming connections
        printf("Error listening\n");
    }    
    else
    {
        printf("Success in listening\n");
    }

    // server socket to interact with client, structure like before - if you know - else NULL for local connection
    if((tcp_client_socket=accept(tcp_server_socket, NULL, NULL)) < 0) { // accept one
        printf("Error accepting\n");
    }    
    else
    {
        printf("Success in accepting\n");
        //send data stream
        send(tcp_client_socket, tcp_server_message, strlen(tcp_server_message), 0);  // send where, what, how much, flags (optional)            
    }
 
    //closing the socket
    close(tcp_server_socket);

    return 0;
}

这是客户端部分(Client.cpp)。

代码语言:javascript
运行
复制
:
:

int main()
{
    //creating the TCP socket
    int tcp_client_socket;                                    //Socket descriptor
    tcp_client_socket = socket(AF_INET, SOCK_STREAM, 0);      //Calling the socket function - args: socket domain, socket stream type, TCP protocol (default)

    //specify address and port of the remote socket
    struct sockaddr_in tcp_server_address;             //declaring a structure for the address
    tcp_server_address.sin_family = AF_INET;           //Structure Fields' definition: Sets the address family of the address the client would connect to
    tcp_server_address.sin_port = htons(PORT);        //Specify and pass the port number to connect - converting in right network byte order
    tcp_server_address.sin_addr.s_addr = INADDR_ANY;   //Connecting to 0.0.0.0
             
    //connecting to the remote socket
    int connection_status = connect(tcp_client_socket, (struct sockaddr *) &tcp_server_address, sizeof(tcp_server_address)); //params: which socket, cast for address to the specific structure type, size of address
    if (connection_status == -1)
    {   //return value of 0 means all okay, -1 means a problem
        printf(" Problem connecting to the socket! Sorry!! \n");
    }
    else
    {
        char tcp_server_response[256];
        recv(tcp_client_socket, &tcp_server_response, sizeof(tcp_server_response), 0);   // params: where (socket), what (string), how much - size of the server response, flags (0)

        //Output, as received from Server
        printf("\n\n Server says: %s \n", tcp_server_response);
    }    
 
    //closing the socket
    close(tcp_client_socket);

    return 0;
}
票数 1
EN

Stack Overflow用户

发布于 2021-03-01 03:32:22

您忘记绑定您的客户端套接字。tcp_client_socket已用socket()初始化,但尚未绑定。通常情况下,您绑定到0 IP和0端口用于出站,而HTTP不会另行说明,因此您需要这样做。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66412427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档