C++网络编程

发布于 2024-11-03 04:03:33 字数 1702 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甜是你 2024-11-10 04:03:33

要了解套接字并正确使用它们,您需要《套接字圣经》:

W. Richard Stevens,Unix 网络编程,第 1 卷:套接字网络 API(第 3 版)

你绝对必须拥有这本书在你坐下来写一行套接字代码之前。没有它就不要离开家。真的。起价约为 35 美元,在 Amazon 上使用。

编辑:OP询问其他卷。这里还有另外两个:

  W.理查德·史蒂文斯,
UNIX 网络编程,第 2 卷:
进程间通信(第二
版)

  W.理查德·史蒂文斯,
TCP/IP 图解,卷。 1:
协议

它们具有史蒂文斯一贯的和预期的卓越品质。我不知道他整合所有这些书的计划是什么,

To understand sockets and use them right, you need The Sockets Bible:

W. Richard Stevens, Unix Network Programming, Volume 1: The Sockets Networking API (3rd Edition)

You absolutely must have this book before you sit down to write a line of sockets code. Don't leave home without it. Really. Starting around $35 used at Amazon.

EDIT: The OP asked about other volumes. Here are two others:

  W. Richard Stevens,
UNIX Network Programming, Volume 2:
Interprocess Communications (2nd
Edition)

  W. Richard Stevens,
TCP/IP Illustrated, Vol. 1: The
Protocols

They are of Stevens's usual and expected superb quality. I don't know what his plans were for integrating all these books,

趴在窗边数星星i 2024-11-10 04:03:33

提升asio(在我看来)是用现代 C++ 编写与平台无关的网络代码的事实上的标准。

boost.asio is (in my opinion) the de facto standard for writing platform independant networking code in modern C++.

林空鹿饮溪 2024-11-10 04:03:33

我的建议:

  1. 我首先使用编写速度快、功能强大的高级语言(例如 python)编写机器人。习惯使用网络工具、IRC 协议等。

  2. 了解底层的套接字和网络。对于 Unix,我建议看一下《Unix 网络编程》。

  3. 用 C++ 编写您的机器人!犯错误,改正错误,然后坚持下去。

My recommendations:

  1. I'd first write the bot in fast-to-write, powerful high-level language, such as python. Get used to working with net tools, the IRC protocol and stuff.

  2. Learn about sockets and networking at low-level. For Unix, I'd say take a look at Unix Network Programming.

  3. Write your bot in C++! Make mistakes, fix them, and keep at it.

世界和平 2024-11-10 04:03:33

学习 C/C++ 套接字编程的最佳指南必须是 Beej 网络指南到目前为止,编程。它通过示例和详细说明完成了您需要了解的所有步骤。据我所知,该站点唯一缺乏的信息是 IPv6 多播。

The best guide to learn socket programming in C/C++ must be Beej's Guide to Network Programming by far. It goes through all of the steps you need to know, both with examples and detailed description. As far as I know, the only information this site lacks is of IPv6 Multicasting.

万劫不复 2024-11-10 04:03:33

从一个简单的客户端-服务器示例开始。使用 Qt 框架非常容易。例如:

server.cpp:

#include <QTcpSocket>
#include <QTcpServer>

int main()
{
    QTcpServer *tcpServer = new QTcpServer(); //creates TCP-based server
    tcpServer->listen(QHostAddress("172.16.254.1"),5300); //listen on your IP adress, port 5300
    while ( tcpServer->isListening() )  //while server is listening
    {   
        QTcpSocket* tcpSocket; //define TCP-based socket
        tcpServer->waitForNewConnection(); //server waits for connection
        if ( (tcpSocket = tcpServer->nextPendingConnection()) ) //if there are connections to be processsed 
        { 
                tcpSocket->write("hello",6); //write "hello" to the socket, client is connected to
                tcpSocket->flush();    
        }
    }
}

client.cpp:

#include <QDebug>
#include <QTcpSocket>

int main()
{
    QTcpSocket *tcpSocket = new QTcpSocket(); //create TCP-based socket
    tcpSocket->connectToHost("172.16.254.1",5300); //connect socket to server
    tcpSocket->waitForConnected(); //wait 
    tcpSocket->waitForReadyRead(); 
    qDebug() << tcpSocket->readAll();    
}

您所需要做的就是在一个终端窗口中运行第一个程序,在另一个终端窗口中运行第二个程序。

您可以在此处找到更多 Qt 网络示例

Start with a simple client-server example. It's very easy with Qt framework. For example:

server.cpp:

#include <QTcpSocket>
#include <QTcpServer>

int main()
{
    QTcpServer *tcpServer = new QTcpServer(); //creates TCP-based server
    tcpServer->listen(QHostAddress("172.16.254.1"),5300); //listen on your IP adress, port 5300
    while ( tcpServer->isListening() )  //while server is listening
    {   
        QTcpSocket* tcpSocket; //define TCP-based socket
        tcpServer->waitForNewConnection(); //server waits for connection
        if ( (tcpSocket = tcpServer->nextPendingConnection()) ) //if there are connections to be processsed 
        { 
                tcpSocket->write("hello",6); //write "hello" to the socket, client is connected to
                tcpSocket->flush();    
        }
    }
}

client.cpp:

#include <QDebug>
#include <QTcpSocket>

int main()
{
    QTcpSocket *tcpSocket = new QTcpSocket(); //create TCP-based socket
    tcpSocket->connectToHost("172.16.254.1",5300); //connect socket to server
    tcpSocket->waitForConnected(); //wait 
    tcpSocket->waitForReadyRead(); 
    qDebug() << tcpSocket->readAll();    
}

All you need to do is to run the first program in one terminal window, and the second in the other.

You will find more Qt network examples here

白云不回头 2024-11-10 04:03:33

我知道这本书很老了,有一本叫做

“Beej's Guide to Network Programming using Internet Socket”

的书Beej 提供的所有内容都是 100% 免费的,可以访问这里的网站来学习网络编程的基础知识。

https://beej.us/guide/bgnet/

这里提供的书籍我仍然建议您获取因为它们提供了有关套接字和 TCP/IP 协议的非常可靠的信息。

  • Unix 网络编程:套接字网络 Api Unix 网络编程:套接字网络 Api - 作者:W. Richard Stevens

  • TCP/IP 插图,卷。 1:协议(Addison-Wesley 专业计算系列)TCP/IP 图解,卷。 1:W. Richard Stevens 的协议(Addison-Wesley 专业计算系列)

  • UNIX 网络编程,第 2 卷:进程间通信,第二版UNIX 网络编程,卷 2:进程间通信,第二版,作者:W. Richard Stevens

  • TCP/IP 插图,第 1 卷:协议(Addison-Wesley 专业计算系列)第二版版
    作者:Kevin Fall(作者)、W. Stevens(作者)

  • TCP/IP 指南:全面、图解的 Internet 协议参考,第一版
    作者:查尔斯·M·科齐罗克


我不是网络程序员或软件开发人员,我唯一的兴趣是仅用于游戏开发的虚幻引擎的网络和复制。请不要给我发私信或询问有关网络的问题。

I know this is old there's book called

"Beej's Guide to Network Programming using Internet Socket"

Everything what Beej provides is 100% free to access here's the website to go and learn the basics of Network Programming.

https://beej.us/guide/bgnet/

The books provided on here I still recommend getting because they offer a pretty solid information on sockets and TCP/IP protocols.

  • Unix Network Programming: The Sockets Networking Api Unix Network Programming: The Sockets Networking Api - by W. Richard Stevens

  • TCP/IP Illustrated, Vol. 1: The Protocols (Addison-Wesley Professional Computing Series)TCP/IP Illustrated, Vol. 1: The Protocols (Addison-Wesley Professional Computing Series) by W. Richard Stevens

  • UNIX Network Programming, Volume 2: Interprocess Communications, Second EditionUNIX Network Programming, Volume 2: Interprocess Communications, Second Edition by W. Richard Stevens

  • TCP/IP Illustrated, Volume 1: The Protocols (Addison-Wesley Professional Computing Series) 2nd Edition
    by Kevin Fall (Author), W. Stevens (Author)

  • The TCP/IP Guide: A Comprehensive, Illustrated Internet Protocols Reference 1st Edition
    by Charles M. Kozierok


I am NOT a network programmer or software developer my only interest is Networking and Replication for Unreal Engine for game-development only. Please don't send me PMs or ask questions regarding about Networking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文