在Unix上使用现代C ++进行套接字编程 [英] socket programming on Unix with modern C++

查看:154
本文介绍了在Unix上使用现代C ++进行套接字编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读过几个教程,已经观看了有关 unix socket 编程的2-3个视频。我也在阅读 Beej的网络编程指南,但我因为所有的代码都写在 c 中,并且与unix系统调用相关。



我没有能够找到一个体面的 c ++ 这个主题的教程, $ c> c 将编译,但因为我学习 c ++ 我真的想在 c ++ 语言。这也是为什么我不想使用boost或Qt库去的原因。






理解:




  • 一切都是一个包括套接字的文件,但我们提供了一些系统例程来操作它们。 connect,bind


  • 套接字有两种类型,tcp和udp,包括协议,分层网络模型等。


  • IP地址,IPv4 / 6,子网,端口号,

    #include< stdio.h> //不知道为什么包括它(可以转换为cstdio我猜)
    #include< sys / types.h> //也许类型实现到套接字?
    #include< sys / socket.h> //见过&绝对涉及socket,
    内定义的结构#include< netinet / in.h> //更多结构体定义,似乎有用
    #include< netdb.h> //不知道:/


  • 我必须声明 int server,client ,我不知道为什么int而不是bool?我必须声明一个 char [buffer] ,在几个实现中 buffer 的范围从256到1024,但总是在2的权力。不知道为什么和它的大小如何影响一些实施。







现在我知道我没有一个工作实现,但我不能得到它的 c 。我不太熟悉 unix c 的东西,因为我开始与 c ++



我发现一些函数(或例程)在手册页中有定义。 man send 这是绝对好的,但是我不知道如何使用它们 c ++ 几个错误代码,我不知道如何捕获与 c ++ 和infact我想做异常,很混乱。是否必须在某处使用 std :: system ?所有这些系统调用是否可以从 c ++ 调用?这么多的指针在系统函数中传递,它们在它之后是可调用的。不被那些功能删除?



我有这么多问题,我会很高兴地问他们,因为我开发的程序,然后面对他们,但我没有一个基本的Hello World '程序。



我需要一个特定于 c ++ 的简单示例,可以说hello world, 到目前为止,我已经:

  #include< iostream> 

#include< stdio.h> //不知道为什么包括它(可以转换为cstdio我猜)
#include< sys / types.h> //也许类型实现到套接字?
#include< sys / socket.h> //见过&绝对涉及socket,
内定义的结构#include< netinet / in.h> //更多结构体定义,似乎有用
#include< netdb.h> //不知道:/

int main(int argc,char * argv []){
int server,client,port = 2000;
char buffer [256];

if(argc <2){
std :: cout<< Arguments not provided<< std :: endl;
exit(1);
}
port = atoi(argv [1]);
//这里缺少代码:/

return 0;
}


解决方案

C ++存在的原因是与大多数C代码兼容并且能够使用低级C库和API。



因此,在C ++中编程套接字的好方法是使用C API(如 BSD插槽)或POSIX < sys / sockets.h> 之上。请注意,套接字尚未被C ++ 11或C ++ 14标准化(因此也是实现特定的)。您经常需要复用(例如从/到多个套接字,例如民意调查2))在您的事件循环中。



或者,使用一些更高级的框架库,如 Qt POCO ,它提供了一些套接字抽象。另请考虑 0mq ...也请参阅 Boost ,正如您在问题中提到的,或 ASIO (由 Graham Reeds )。对于 HTTP ,请使用现有的库(例如 libcurl (客户端)和 libonion 服务器端)。您可能会找到其他协议的图书馆。



BTW,您对 socket(7)的理解不完整。在Linux上,您不仅可以使用 tcp(7)或< a href =http://man7.org/linux/man-pages/man7/udp.7.html =nofollow> udp(7)套接字,还包括 unix(7)套接字, netlink(7)一个, ddp(7) AppleTalk等。



阅读(如果您有权访问一个Linux系统;如果你没有,然后在你的笔记本电脑上安装Linux) 高级Linux编程 图书(免费在线提供)。它为套接字提供了一些章节,并解释了最重要的 syscalls(2)。还可以尝试 strace(1) - 例如在某些现有的网络客户端应用程序(如 wget(1))上。 ...



我想你的 int服务器是一些文件描述符,因此是一些小的非负数(算术是无意义的)。



了解 TCP 套接字的棘手问题是,只有字节流,没有任何消息边界(和网络路由器可以拆分或碎片整理数据包,因此,一个 recv( 2)与单个 send(2) ..)。因此,使用TCP,您始终需要缓冲两者在发送和接收两侧),您需要一些 >关于消息格式和边界,如应用程序所理解的(例如HTTP请求和响应)。


I've read a few tutorials and have already watched 2-3 videos regarding unix sockets programming. I'm also reading Beej's guide to network programming , but I'm constantly finding trouble with code since all of them are written in c and related to unix system calls.

I've not been able to find a decent c++ tutorial on this topic, though I know c would compile but since I'm learning c++ I really want to do it in c++ language. Also that is the reason why I don't want to go with boost or Qt library.


So far I've understood :

  • Everything is a file including sockets, yet we are provided a few system routines to operate on them eg. connect, bind etc.

  • Sockets are of 2 types, tcp and udp and all theory parts behind them including protocols, Layered network model etc.

  • IP addresses, IPv4/6, Subnets, Port numbers, Little/Big Endians and other theory related to them.

  • Some work has already been done eg.

    #include <stdio.h>      // no idea why including it (could convert to cstdio I guess)
    #include <sys/types.h>  // maybe types realted to sockets?
    #include <sys/socket.h> // seen & definitely related to socket, structs defined inside
    #include <netinet/in.h> // more structs defined, seems useful 
    #include <netdb.h>      // no idea again :/
    

  • I've to declare int server,client, I don't know why int and not bool? I've to declare a char[buffer] too, with buffer is ranging from 256 to 1024 in several implementations, but always in powers of 2. No idea why and how would it's size affect some implementation.


Now I know I don't have a working implementation, but I couldn't get it in anything else than c. I'm not so familiar with unix and c stuff since I'm starting out with c++.

I found out that some functions (or routines) have their definition in man pages eg. man send and it is absolutely good but then again I've no idea how to use them with c++ and it has several error codes which I've no idea how to capture with c++ and infact I wanted to do exceptions and it's too much confusing. Do I have to use std::system somewhere? Are all these system calls callable from c++? So many pointers passing around in system functions, are they callable after it ie. not deleted by those functions?

I've so many questions, and I'll be happy to ask them as I develop the program and then face them but I do not have a basic 'Hello World' program for now.

I need a minimal example specific to c++ that could say hello world and I'll be happy to learn further on it. So far I've :

#include <iostream>

#include <stdio.h>      // no idea why including it (could convert to cstdio I guess)
#include <sys/types.h>  // maybe types realted to sockets?
#include <sys/socket.h> // seen & definitely related to socket, structs defined inside
#include <netinet/in.h> // more structs defined, seems useful 
#include <netdb.h>      // no idea again :/

int main(int argc, char *argv[]){
    int server, client, port = 2000;
    char buffer[256];

    if (argc < 2) {
        std::cout << "Arguments not provided" << std::endl;
        exit(1);
    }
    port = atoi(argv[1]);
    // lack of code here :/

    return 0;
}

解决方案

The very reason of C++ existence is to be compatible with most of C code and being able to use low level C libraries and APIs.

So the good approach to programming sockets in C++ is indeed to use a C API such as BSD sockets or POSIX <sys/sockets.h> and to code genuine C++ above that. Notice that sockets are not (yet) standardized by C++11 or C++14 (so are implementation specific). You often need to multiplex (e.g. from/to several sockets, using e.g. poll(2) on Linux) in your event loop.

Alternatively, use some higher-level framework library like Qt or POCO which provides some socket abstraction. Consider also 0mq... Look also inside Boost, as you mentioned in your question, or ASIO (as commented by Graham Reeds). For HTTP, use existing libraries (like libcurl for client side, and libonion for server side). You are likely to find other libraries for other protocols.

BTW, your understanding of socket(7) is incomplete. On Linux you might use not only tcp(7) or udp(7) sockets, but also unix(7) sockets, netlink(7) ones, ddp(7) AppleTalk ones, etc.

Read (at last if you have access to a Linux system; if you don't, then install Linux on your laptop now) the Advanced Linux Programming book (freely available online). It has some chapters for sockets and explains the most important syscalls(2). Experiment also with strace(1) -e.g. on some existing network client application like wget(1)- ....

I guess that your int server is some file descriptor, so is some abstract small non-negative number (on which arithmetic is meaningless).

The tricky thing to understand about TCP sockets is that they are only streams of bytes, without any message boundaries (and the network routers can break up or defragment packets, so a single recv(2) does not match a single send(2)..). Hence, with TCP, you always need to buffer (both on sending and receiving sides) and you need some convention about message format and boundaries, as understood by applications (e.g. HTTP requests and responses).

这篇关于在Unix上使用现代C ++进行套接字编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆