草庐IT

c - 在一台服务器上接收多个客户端数据包

coder 2023-09-19 原文

我有 3 个应用程序、一个服务器、2 个客户端。我正在尝试对此进行编程,以便我可以将我的客户端的登录请求发送到服务器并请求查看有关另一个客户端的信息。

问题:我对所有 3 个应用程序使用相同的 ip 和端口号(这都是在我计算机上的 3 个 visual studio 应用程序上完成的)否则我认为它无法连接到服务器。这样可以吗?

当我尝试在服务器的 while 循环之外使用 listen 和 accept 时,它适用于一个客户端,但不适用于另一个客户端。当我尝试将 listen 和 accept 放在 while 循环中时,它对两个客户端都有效,但不会响应多个 accept。我的问题是什么?

服务器

struct Users {

int message;
char userName[50];
char ipAddress[50];
int PortNumber;
bool online;

};

int main () {

//Create users
Users client[2];

client[0].PortNumber = 20000;
client[0].online = false;
sprintf(&client[0].userName[0], "Albert");
sprintf(&client[0].ipAddress[0], "127.0.0.1");

client[1].PortNumber = 20000;
client[1].online = false;
sprintf(&client[1].userName[0], "Monique");
sprintf(&client[1].ipAddress[0], "127.0.0.1");


//Set up all the connection stuff
struct sockaddr_in SvrAddr;
SOCKET WelcomeSocket, ConnectionSocket;
int PortNumber = 20000;
int result;
char IPAddress[] = "127.0.0.1";
char RxBuffer[128];
char TxBuffer[128];


WORD wVersionRequested; 
WSADATA wsaData; 

wVersionRequested = MAKEWORD(2,2); //For Server 


if(WSAStartup(wVersionRequested, &wsaData) != 0) 
    return -1;


//Setting up the welcome socket
WelcomeSocket = socket(AF_INET, SOCK_STREAM, 0);

//Setting up the sockaddr svraddr structure
SvrAddr.sin_family = AF_INET;
SvrAddr.sin_addr.s_addr = inet_addr(IPAddress);
SvrAddr.sin_port = htons(PortNumber);


//Bind
bind(WelcomeSocket, (sockaddr*)&SvrAddr, sizeof(SvrAddr));

//listening
//listen(WelcomeSocket, 5);

//temp users
Users temp;
//ConnectionSocket = accept(WelcomeSocket, NULL, NULL);
ConnectionSocket = SOCKET_ERROR;

//while loop for the connection 
while (1) {

    listen(WelcomeSocket, 5);

    if ((ConnectionSocket = accept(WelcomeSocket, NULL, NULL)) == SOCKET_ERROR)
    {
        return -1;
    }

    else {

    int n = recv(ConnectionSocket, RxBuffer, sizeof(RxBuffer), 0);

    if (n == 0)
        break;


    memcpy(&temp, RxBuffer, sizeof(struct Users));

    //cout << temp.message << temp.userName << endl << endl;

    //check which message type is being sent
    switch(temp.message) {

    //if message type 1
    case 1 :
        for (int i = 0; i < 2; i++) {

            //if receieved username matches with any username in the database
            if (strcmp(temp.userName, client[i].userName) == 0) {

                //assign the recieved users information to the matched one in database
                strcpy(client[i].userName, temp.userName);
                client[i].online = true;
                client[i].message = 2;

                cout << "Username: " << client[i].userName << endl << "Online status: " << client[i].online << endl << endl;

                //send the acknowledgement packet
                send(ConnectionSocket, (char *)&client[i], sizeof(struct Users), 0);
                //closesocket(ConnectionSocket);
            }

        }
        break;

    //if message type 3
    case 3 :

        cout << "User being searched for: " << temp.userName << endl << endl;
        for (int i = 0; i < 2; i++) {

            //if receieved username matches with any username in the database
            if (strcmp(temp.userName, client[i].userName) == 0) {
                client[i].message = 4;
                //send the acknowledgement packet
                send(ConnectionSocket, (char *)&client[i], sizeof(struct Users), 0);
                //closesocket(ConnectionSocket);
            }

        }
        break;

    default :
        break;

    }

}

}

closesocket(ConnectionSocket);
WSACleanup();
}

客户端(两者相同)

struct Users {

int message;
char userName[50];
char ipAddress[50];
int PortNumber;
bool online;

};


int main () {

struct sockaddr_in SvrAddr;
SOCKET ClientSocket;
int PortNumber = 20000;
char IPAddress[] = "127.0.0.1";
//char message[] = "Hello this is the client.";
char RxBuffer[128];

WORD wVersionRequested; 
WSADATA wsaData; 

wVersionRequested = MAKEWORD(2,3); //For Client 

if(WSAStartup(wVersionRequested, &wsaData) != 0) 
    return -1; 


ClientSocket = socket(AF_INET, SOCK_STREAM, 0);

SvrAddr.sin_family = AF_INET;
SvrAddr.sin_addr.s_addr = inet_addr(IPAddress);
SvrAddr.sin_port = htons(PortNumber);

connect(ClientSocket, (sockaddr*)&SvrAddr, sizeof(SvrAddr));



//cout << "Name: ";
//cin >> login;

//Send request to login
int log;
char * name = new char[128];
char * request = new char[128];
Users client;
Users talkto;


do {

    cout << "To login press (1) to end press (2). ";
    cin >> log;
    flushall();

    if (log == 1) {

        cout << "Username : ";
        cin.getline(name, 128, '\n');
        cout << endl;
        flushall();

        //Set client login info
        strcpy(client.userName, name);
        client.message = 1;



        send(ClientSocket, (char *)&client, sizeof(struct Users), 0);


        //Recieve acknowledgement
        recv(ClientSocket, RxBuffer, sizeof(RxBuffer), 0);
        //create temp users
        Users temp;

        memcpy(&temp, RxBuffer, sizeof(struct Users));

        //If logged in and received a message of type 2 (acknowledgement)
        if (temp.message == 2) {

            cout << "You have logged in." << endl << endl;

            cout << "Enter user to request user information: ";
            cin.getline(talkto.userName, 128, '\n');
            flushall();
            cout << endl;

            talkto.message = 3;

            //send request for user information packet
            send(ClientSocket, (char *)&talkto, sizeof(struct Users), 0);

            recv(ClientSocket, RxBuffer, sizeof(RxBuffer), 0);

            memcpy(&temp, RxBuffer, sizeof(struct Users));

            //if message received is of type 4 (acknowledgement of user request info)
            if (temp.message == 4) {

                cout << "Requested User: " << temp.userName << endl << "Online status: " << temp.online << endl << endl;
            }
        }

        //cout << temp.userName << endl << temp.online << endl << temp.message;
    } 

} while (log != 2);


closesocket(ClientSocket);


WSACleanup();



} 

最佳答案

您不要在循环内使用 listen()。只有 accept()。此外,您不使用非阻塞套接字,但这是您的偏好。另一件事,每次完成 accept 时都必须使用 closesocket。此外,您的 recv() 必须在循环中处理,因为与发送不同,它可能会在接收到所有数据之前返回。

服务器的基本结构:

struct sockaddr_in inAddr; //incoming address. Use it to distinguise between incoming clients
struct sockaddr_in address;
unsigned int inLen;
int recvMsgSize;
unsigned short port = 20000;
Socket socket_handle, acceptSocket;

socket_handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(socket_handle == INVALID_SOCKET){
    //error
}

memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY); //All incoming addresses
address.sin_port = htons(port); //port must be unsigned short not int!
if(bind(socket_handle, (struct sockaddr *)&address, sizeof(address)) < 0){
    //error
}

if(listen(socket_handle, MAXPENDING) < 0){
    //error
}

while(true){
    acceptSocket = accept(socket_handle, (struct sockaddr *) &inAddr, &inLen));
    if(acceptSocket == INVALID_SOCKET){
        //error
    }

    while(true){
        recvMsgSize = recv(acceptSocket, received_data, strlen(received_data), 0);
        if(recvMsgSize < 0){/*error*/}
        else if(recvMsgSize == 0){break;}
        else{/*receive your data*/}
    }

    (do other stuff...)
    closesocket(acceptSocket);
}

closesocket(socket_handle);
WSACleanup();

瓦尔特

关于c - 在一台服务器上接收多个客户端数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22122066/

有关c - 在一台服务器上接收多个客户端数据包的更多相关文章

  1. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  2. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  3. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  4. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  5. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  6. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  7. ruby-on-rails - 启动 Rails 服务器时 ImageMagick 的警告 - 2

    最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru

  8. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

  9. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  10. ruby-on-rails - RSpec:避免使用允许接收的任何实例 - 2

    我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_

随机推荐