3

在 Qt 框架上工作 ..,需要获得给定要求的解决方案

我已经将一台服务器和 2 个客户端作为我的 PC:IP1、IP2、IP3 是端口。

我的服务器正在侦听 IP1,端口号 9999 我想将数据(数据报)发送到服务器,然后服务器需要响应我的客户端。所以我可以知道客户端IP地址和客户端端口号。

客户端 IP 地址和端口如何知道服务器?这是将响应返回给客户端所必需的。

注意:我在同一台 PC 上运行服务器、客户端 1 和客户端 2。

4

3 回答 3

4
How client IP address and port knows to the server ?, 
     Ans: When datagram is recieved( readyRead signal is emited),  you can use readDatagram API 
     socketServer.readDatagram(buffer.data(),buffer.size(),&sender,&port);
     sender = IPaddress of client
     por  = portNumber of client.

这是使用 Qt 的 QUDPSocket, 1 UDP Sever 的解决方案,在同一个本地主机上有多个客户端,测试它工作正常。必要时注释代码

有两个控制台应用程序( UDPServer 和 UDPClient )

How to test
    1. Run UDPServer, it will listen on port 9999
    2. Run UDPClient(First instance )
    3. Run UDPClient(second instance )

Result:
    Please check below screenshot

在此处输入图像描述

UDP服务器代码

> 主要.cpp

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    UDPServer server;
    return a.exec();
}

> UDPServer.h

  #ifndef UDPSERVER_H
    #define UDPSERVER_H
    #include <QObject>
    #include <QUdpSocket>

    class UDPServer : public QObject
    {
        Q_OBJECT
    public:
        explicit UDPServer(QObject *parent = 0);
        void WriteData( const QString& );

    public slots:
        void readReady();
    private:
        QUdpSocket socketServer;

    };
    #endif // UDPSERVER_H

> UDPServer.cpp

#include "UDPServer.h"
UDPServer::UDPServer(QObject *parent) :QObject(parent),socketServer(this)
{
    qDebug()<<"I am UDP server, listening on 9999";
    // Listen to 9999 port server
    socketServer.bind(QHostAddress::LocalHost,9999 );
    connect(&socketServer,SIGNAL(readyRead()),this,SLOT(readReady()));

}

void UDPServer::readReady()
{
    QByteArray buffer;
    buffer.resize(socketServer.pendingDatagramSize());
    QHostAddress sender;
    quint16 port;
    socketServer.readDatagram(buffer.data(),buffer.size(),&sender,&port);
    qDebug()<<"Datagram Recieved From";
    qDebug()<<"Client IP" << sender.toString();
    qDebug()<<"Client Port Number " << port;
    qDebug()<<"\n\n";

    // Write to the client,need to specify the client port number.
    QByteArray clientData;
    clientData.append( "data");
    socketServer.writeDatagram( clientData, QHostAddress::LocalHost, port );
}

UDP客户端代码

> 主要.cpp

#include <QtCore/QCoreApplication>
#include "udpclient.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    UDPClient client;
    client.WriteData("What is my IP");    
    return a.exec();
}

> udpclient.h

#ifndef UDPCLIENT_H
#define UDPCLIENT_H

#include <QObject>
#include <QUdpSocket>

class UDPClient : public QObject
{
    Q_OBJECT
public:
    explicit UDPClient(QObject *parent = 0);
    void WriteData( const QString& );
public slots:
    void readReady();

private:
    QUdpSocket clientSocket;

};

#endif // UDPCLIENT_H

> udpclient.cpp

#include "udpclient.h"


UDPClient::UDPClient(QObject *parent) :QObject(parent), clientSocket(this)
{    
    qDebug()<<"I am your client";
    connect(&clientSocket,SIGNAL(readyRead()),this,SLOT(readReady()));
}

void UDPClient::WriteData(const QString& data)
{
    QByteArray clientData;
    clientData.append( data);    
    // write to the port, listening by the server.
    qDebug()<<"Writing datagram to 9999 port";
    clientSocket.writeDatagram(clientData, QHostAddress::LocalHost, 9999 );  

}

void UDPClient::readReady()
{
    // got response from server, so clientSoclet port number can get.
    qDebug()<< "Reacieved response from server through my port(Client port No):" << clientSocket.localPort();  

    QByteArray buffer;    
    buffer.resize(clientSocket.pendingDatagramSize());        

    QHostAddress sender;    
    quint16 port;  
    clientSocket.readDatagram(buffer.data(),buffer.size(),&sender,&port);    
    // To read server IP
    qDebug()<< "Server IP Responded" << sender.toString();
    qDebug()<< "Server Port Number" << port;    
}
于 2013-08-17T13:23:43.727 回答
0

您将让服务器在您的 localhost 127.0.0.1 上进行侦听,然后每个客户端将连接到服务器正在侦听的端口上的 localhost(127.0.0.1)。在这里,我将展示它的外观。

服务器代码可能如下所示:

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>

int main() {
    struct addrinfo hints, *res;
    int sockfd;
    int byte_count;
    socklen_t fromlen;
    struct sockaddr_storage addr;
    char buf[512];
    char ipstr[INET6_ADDRSTRLEN];

    // get host info, make socket, bind it to port 9999
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_PASSIVE;
    getaddrinfo(NULL, "9999", &hints, &res);
    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    bind(sockfd, res->ai_addr, res->ai_addrlen);

    // no need to accept(), just recvfrom():
    fromlen = sizeof addr;
    byte_count = recvfrom(sockfd, buf, sizeof buf, 0, (sockaddr*)&addr, &fromlen);

    printf("recv()'d %d bytes of data in buf\n", byte_count);
    printf("from IP address %s\n",
            inet_ntop(addr.ss_family,
            (const void*)(&((struct sockaddr_in *) &addr)->sin_addr), ipstr, sizeof ipstr));

    return 0;
}

您可以尝试使用终端中的 -vu 选项通过 nc 监听服务器:

me@computer:~# nc -vu localhost 9999

并且应该像这样从服务器获取输出:

recv()'d 29 bytes of data in buf
from IP address 127.0.0.1

客户端代码:

#define DEST_PORT 9999
#define DEST_IP "127.0.0.1"

int main(int argc, char** argv) {
    char *secret_message = "The Cheese is in The Toaster";

    int dgram_socket;
    struct sockaddr_in dest_addr;

    // now with UDP datagram sockets:
    // datagram sockets and recvfrom()
    dest_addr.sin_family = AF_INET;
    /* short, network byte order */
    dest_addr.sin_port = htons(DEST_PORT);
    dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);

    /* zero the rest of the struct */
    memset(&(dest_addr.sin_zero), 0, 8);

    dgram_socket = socket(dest_addr.sin_family, SOCK_DGRAM, 0);


    // send secret message normally:
    sendto(dgram_socket, secret_message, strlen(secret_message) + 1, 0,
            (struct sockaddr*) &dest_addr, sizeof dest_addr);

    return 0;
}

同样,当被解雇时,您应该从服务器获得类似于以下内容的输出:

recv()'d 29 bytes of data in buf
from IP address 127.0.0.1
于 2013-08-16T21:16:25.967 回答
0

不确定 Qt 框架,但使用普通的套接字命令可以bind访问特定的 IP 和端口。

handle = socket( AF_INET, SOCK_DGRAM, 0 );

// my local computer
struct sockaddr_in addr_loc;
memset( &addr_loc, 0, sizeof(addr_loc) );
addr_loc.sin_family = AF_INET;
addr_loc.sin_port = htons( localPort );
addr_loc.sin_addr.s_addr = htonl( localAddr );
bind( handle, (struct sockaddr*)&addr_loc, sizeof(addr_loc) );

// the remote location
struct sockaddr_in addr_rem;
memset( &addr_rem, 0, sizeof(addr_rem) );
addr_rem.sin_family = AF_INET;
addr_rem.sin_port = htons( remotePort );
addr_rem.sin_addr.s_addr = htonl( remoteAddr );
sendto( handle, packetData, packetLength, 0, (struct sockaddr *)&addr_rem, sizeof(addr_rem) );
于 2013-08-16T20:19:52.407 回答