C++网络编程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5773390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ network programming
提问by Lockhead
Hey, I would like to expand my knowledge in C++, so the first thing I'm taking on is network programming.
嘿,我想扩展我的 C++ 知识,所以我要做的第一件事是网络编程。
I want to make an IRC bot(which hopefully will teach me about socket programming and networking topics), but I have no idea where to start. If anyone could explain to me how IRC bots work and how to make them, and direct me to some learning resources, that would be really great. Simple snippets as well would be awesome...
我想制作一个 IRC 机器人(希望它能教我有关套接字编程和网络主题的知识),但我不知道从哪里开始。如果有人能向我解释 IRC 机器人的工作原理以及如何制作它们,并指导我找到一些学习资源,那就太好了。简单的片段也会很棒......
Thanks!
谢谢!
edit:
编辑:
forgot to mention that I use ubuntu, so the windows way is not an option
忘了说我用的是ubuntu,所以windows方式不是一个选择
采纳答案by Pete Wilson
To understand sockets and use them right, you needThe Sockets Bible:
要了解套接字并正确使用它们,您需要阅读Sockets Bible:
W. Richard Stevens, Unix Network Programming, Volume 1: The Sockets Networking API (3rd Edition)
W. Richard Stevens,Unix 网络编程,第 1 卷:套接字网络 API(第 3 版)
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.
在您坐下来编写一行套接字代码之前,您绝对必须拥有这本书。没有它不要离开家。真的。起价约 35 美元,在亚马逊使用。
EDIT:The OP asked about other volumes. Here are two others:
编辑:OP 询问了其他卷。这是另外两个:
W. Richard Stevens, UNIX Network Programming, Volume 2:
Interprocess Communications (2nd
Edition)
W. Richard Stevens, TCP/IP Illustrated, Vol. 1: The
Protocols
W. Richard Stevens,UNIX 网络编程,第 2 卷:进程间通信(第 2 版)
W. Richard Stevens,TCP/IP 插图,卷。1:协议
They are of Stevens's usual and expected superb quality. I don't know what his plans were for integrating all these books,
它们具有史蒂文斯一贯的优良品质。我不知道他整合所有这些书的计划是什么,
回答by ildjarn
回答by slezica
My recommendations:
我的建议:
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.
Learn about sockets and networking at low-level. For Unix, I'd say take a look at Unix Network Programming.
Write your bot in C++! Make mistakes, fix them, and keep at it.
我首先用编写速度快、功能强大的高级语言(例如 Python)编写机器人。习惯于使用网络工具、IRC 协议和其他东西。
在底层了解套接字和网络。对于 Unix,我会说看看 Unix Network Programming。
用 C++ 编写你的机器人!犯错误,改正错误,并坚持下去。
回答by efr4k
The best guide to learn socket programming in C/C++ must be Beej's Guide to Network Programmingby 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.
到目前为止,学习 C/C++ 套接字编程的最佳指南必须是Beej 的网络编程指南。它涵盖了您需要了解的所有步骤,包括示例和详细说明。据我所知,该站点唯一缺少的信息是 IPv6 多播。
回答by cpp
Start with a simple client-server example. It's very easy with Qt framework. For example:
从一个简单的客户端-服务器示例开始。使用 Qt 框架非常容易。例如:
server.cpp:
服务器.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:
客户端.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
您将在此处找到更多 Qt 网络示例