Binary Order function In Unix Network Programming

ByteOrder:

 1 #include <stdio.h>
 2
 3 int main(int argc, char**argv)
 4 {
 5     union {
 6         short s;
 7         char    c[sizeof(short)];
 8     } un;
 9     un.s = 0x0102;
10
11     if ( sizeof(short) == 2 )
12     {
13         if ( (un.c[0] == 1) && (un.c[1] == 2) )
14         {
15             printf("big-endian\n");
16         }
17         else if( (un.c[0] == 2) && (un.c[1] == 1) )
18         {
19             printf("little-endian\n");
20         }
21         else
22         {
23             printf("unknown byte order.\n");
24         }
25     }
26     else
27     {
28         printf("unsupport.\n");
29     }
30 }

BinaryOrder.cc

网络协议必须制定一个网络字节序,在每个TCP分节中,都有16位的端口号和32位的IPv4地址。发送协议栈和接受协议栈 必须就这些多字节字段各个字节的传送顺序达成一致。

网际协议使用大端字节序来传送这些多字节整数

主机字节序--->网络字节序:

#include<netinet/in.h>

uint16_t htons(uint16_t host16bitvalue);

uint32_t htonl(uint32_t host32bitvalue);

uint16_t ntohs(uint16_t net16bitvalue);

uint32_t ntohl(uint32_t net32bitvalue);

字节操纵函数:

#include <string.h>

void *memset(void *dest, int c, size_t len);

void *memcpy(void *dest, const void *src, size_t nbytes);

int memcmp(const void *ptr1, const void *ptr2, size_t nbytes);

ASCII 字符串---> 网络字节序的二进制值(存放在套接字地址结构中的值)之间 转换网际地址。

#include <arpa/inet.h>

int inet_aton( const char *strPtr, struct in_addr *addrPtr);

          /** 返回: 如字符串有效则为1,否则为0**/

in_addr_t inet_addr(const char* strPtr);

          /** 返回: 若字符串有效则为32位二进制网络字节序的IPv4地址, 否则为INADDR_NONE

char *inet_ntoa( struct in_addr inaddr);

          /**返回: 指向一个点分十进制数串的指针**/

int inet_pton( int family, const char *strPtr, void *addrPtr);

          /**若成功则为1, 若输入不是有效的表达格式则为0, 如出错则为-1**/

const char *inet_ntop( int family, const void *addrPtr, char *strPtr, size_t len);

          /**返回:若陈宫则为指向结果的指针,若出错则为NULL**/

1 struct sockaddr_in addr;
2 inet_ntop(AF_INET, &addr.sin_addr, str, sizeof(str));
3
4 struct sockaddr_in6 addr6;
5 inet_ntop(AF_INET6, &addr6.sin6_addr, str, sizeof(str));
时间: 2024-08-24 20:15:56

Binary Order function In Unix Network Programming的相关文章

Unix NetWork Programming(unix环境编程)——环境搭建(解决unp.h等源码编译问题)

此配置实例亲测成功,共勉,有问题大家留言. 环境:VMware 10 + unbuntu 14.04 为了unix进行网络编程,编程第一个unix程序时遇到的问题,不能包含unp.h文件,这个感觉和apue.h差不多,不过这里需要编译源代码,为了以后方便,现在整理如下: 主要有两点一是生成libunp.a这个库,二是得到unp.h.config.h这两个个头文件. 1,安装编译器,为了齐全还是安装一下build-essential sudo apt-get install build-essen

《Unix NetWork Programming》(Unix网络编程)—环境搭建

1,安装编译器,为了齐全还是安装一下build-essential [python] view plaincopy sudo apt-get install build-essential 2,下载本书的头文件及示例源码 点此下载unpv13e.tar.gz  http://ishare.iask.sina.com.cn/f/13238521.html 3,解压unpv13e.tar.gz后进入目录查看README,然后可以按照里面提示操作,不过会遇到问题 第一步:在终端中进入upnv13e目录

Subject: 32547 UNIX Systems Programming

University of Technology SydneyFaculty of Engineering and Information TechnologySubject: 32547 UNIX Systems Programming, Spring 2019AssignmentDescriptionThis assignment is an individual programming assignment using Perl. It addresses objectives 2and

《Python Network Programming Cookbook》读书笔记0---前言

最近打算学习python网络开发方面的内容,于是网上找来一本<Python Network Programming Cookbook>电子书做教材,顺便边学边将学到的这容通过博客的形式总结起来与大家分享. 这本书不长,英文原版也只有234页,但涉及面很广,TCP/UDP HTTP/HTTPS FTP SMTP POP3 IMAP CGI均有介绍,依照章节,博文会按如下9个部分介绍: 套接字, IPv4,  简单的Client/Server程序 更好的性能:复用套接字 I/O IPv6, UNI

TCP 、UDP网络编程作业代写、代写C 语言 TCP程序 Network Programming using C

TCP .UDP网络编程作业代写.代写C 语言 TCP程序Network Programming using COverview? This homework is due by 11:59:59 PM on Thursday, April 26, 2018.? This homework will count as 8% of your final course grade.? This homework is to be completed individually. Do not shar

Professional iOS Network Programming Connecting the Enterprise to the iPhone and iPad

Book Description Learn to develop iPhone and iPad applications for networked enterprise environments The iPhone and iPad have made a powerful impact on the business world. Developers creating iOS apps for the enterprise face unique challenges involvi

分享:Foundations of Python Network Programming(3rd) 英文版pdf 下载

Foundations of Python Network Programming Third Edition 下载http://www.amazon.com/Foundations-Python-Network-Programming-Brandon/dp/1430258543这本书是2014年底出版的,基于最新的 python3.4 版本.配书源码链接https://github.com/brandon-rhodes/fopnp 目录 Chapter 1: Introduction to C

Boost.Asio C++ Network Programming翻译的校对稿

Boost.Asio C++ Network Programming翻译的校对稿将发布在gitbook上: 大家有兴趣可以一起来校对 目前校对稿仍然在持续不断的添加中,预计明天会全部校对完毕,届时我会将校对稿以pdf的形式发布到csdn的下载频道中,以方便大家更好的阅读全书. 附上链接地址: https://www.gitbook.com/book/mmoaay/boost-asio-cpp-network-programming-chinese

Lua 自己实现排序sort比较方法,抛出错误invalid order function for sorting

明天新功能就要上了,结果刚刚突然QA说项目抛出了错误.握草,吓得立马出了一身汗. 查了一下错误,发现可能是自己写的不稳定排序造成的.自己感觉应该就是.把排序方法写成稳定的之后,代码分离编译进手机,跑了一下木有错误了.脑残的自己为何要对服务器传过来的有序数据进行排序呢?脑抽不明. 下文为转的别人总结的lua库.(该学习的地方还太多) lua的table库 函数列表: table.insert(table,[ pos,] value) table.remove(table[, pos]) table