在Unix套接字编程与现代C ++ [英] socket programming on Unix with modern C++

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

问题描述

我读过一些教程,并已看过有关的 Unix套接字的编程2-3的视频。我还在读书 Beej的指南网络编程,但我M不断发现,因为所有的人都写在 C 和相关的UNIX系统调用与code麻烦。

我一直没能找到一个体面 C ++ 关于这个主题的教程,虽然我知道 C 将编译,但自从我学习 C ++ 我真的想这样做,在 C ++ 语言。此外,这就是为什么我不想去与升压或Qt库的原因。


到目前为止,我明白了:


  • 一切是包括插座的文件,但我们提供了一些系统程序来对他们的操作如。 连接,绑定等。


  • 套接字2种类型,TCP和UDP和他们背后的理论部分,包括协议,分层网络模型等。


  • IP地址的IPv4 / 6,子网,端口号,小/大Endians等理论与他们无关。


  • 有些工作已经完成如:

     的#include<&stdio.h中GT; //不知道为什么它包括(可转换为cstdio我猜)
    #包括LT&; SYS / types.h中> //也许realted到插座类型?
    #包括LT&; SYS / socket.h中> //看到和放大器;肯定涉及到插座,中定义结构
    #包括LT&; netinet / in.h中> //更多的结构定义,似乎是有益
    #包括LT&;&netdb.h中GT;再次//不知道:/


  • 我已经声明 int服务器,客户端,我不知道为什么int而不是bool的?我已经申报了的char [缓冲] 也与缓存是从256到1024多种实现方式,但总是2.不知道为什么,以及如何将它的大小会影响一些执行权力。



现在我知道我没有工作的实施,但在C 别的比我无法得到它。我不那么熟悉的 UNIX C 东西,因为我开始了与 C ++

我发现某些功能(或程序)有自己的手册页例如定义。 人送上,这绝对是不错,但随后再次我不知道如何与使用C ++ ,它有几个错误codeS 这我不知道如何使用 C ++ 和逸岸捕捉我想要做的例外它太多混乱。我一定要使用的std ::系统地方?是否所有这些系统调用可调用从 C ++ ?如此多的三分球绕过系统的功能,是他们后,即调用。没有这些功能被删除?

我有这么多问题,我会很乐意让他们为我制定的程序,然后面对他们,但我没有一个基本的'Hello World程序现在。

我需要 C ++ 具体的一个小例子,可以说世界您好,我会很乐意进一步学习就可以了。的迄今为止我有:

 的#include<&iostream的GT;#包括LT&;&stdio.h中GT; //不知道为什么它包括(可转换为cstdio我猜)
#包括LT&; SYS / types.h中> //也许realted到插座类型?
#包括LT&; SYS / socket.h中> //看到和放大器;肯定涉及到插座,中定义结构
#包括LT&; netinet / in.h中> //更多的结构定义,似乎是有益
#包括LT&;&netdb.h中GT;再次//不知道:/INT主(INT ARGC,CHAR *的argv []){
    INT服务器,客户端端口= 2000;
    字符缓冲区[256];    如果(的argc 2){
        性病::法院LT&;< 论据没有提供<<的std :: ENDL;
        出口(1);
    }
    端口=的atoi(ARGV [1]);
    //缺乏code在这里:/    返回0;
}


解决方案

C ++存在的根本原因是与大多数C code,并且能够使用低级别的C库和API兼容。

因此​​,好做法编程插座在C ++中确实是使用C API,如 BSD套接字或POSIX < SYS / sockets.h&GT ; 以及上述的那个code真正的C ++的。请注意,插座(还)没有用C ++ 11或C ++ 14规范(这样是实现特定的)。你经常需要从/到几个插座复用(例如,使用如调查( 2)在事件循环 的在Linux上)。

此外,使用一些更高级的框架库如 Qt的或的 POCO 提供了一些接口抽象。想想也是 0mq ...也看看里面的升压,像你一样由<一个评论你的问题,或 ASIO (自提HREF =htt​​p://stackoverflow.com/users/342/graham-reeds>格雷厄姆芦苇)。对于 HTTP ,利用现有的库(比如 libcurl的为客户端和 libonion 为服务器端)。你很可能会发现其他图书馆其他协议的。

顺便说一句,您的插座(7)是理解不完整的。在Linux上,你可以使用不仅 TCP(7)或< A HREF =htt​​p://man7.org/linux/man-pages/man7/udp.7.html相对=nofollow> UDP(7)的插座,但是也 UNIX(7)插座的网络链路(7)的人,的 DDP(7)的AppleTalk的人,等等。

阅读(最后如果你有机会到Linux系统;如果你不这样做,那么你的笔记本电脑上安装Linux现在)的的高级Linux编程书(免费提供在线)。它有一个插座部分章节,并解释了最重要的系统调用(2)。实验还与 strace的(1) -e.g.像的wget(1)一些现有的网络客户端应用程序 - 。 ...

我想,你的 int服务器一些文件描述,所以一些的摘要的小的非负数(哪个运算是没有意义的)。

棘手的事情要了解有关 TCP套接字的是,他们的字节流只,没有的任何消息边界(和网络路由器可以打破或整理数据包,因此单个的recv(2)不不匹配单个发送(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天全站免登陆