zoukankan      html  css  js  c++  java
  • 网络序和字节序转换源码

    1 #define HTONL(n) (((((unsigned long)(n) & 0xFF)) << 24) |  ((((unsigned long)(n) & 0xFF00)) << 8) | ((((unsigned long)(n) & 0xFF0000)) >> 8) | ((((unsigned long)(n) & 0xFF000000)) >> 24))
    2 #define HTONS(n) (((unsigned short(n) & 0xFF)) << 8)) |(((unsigned short(n) & 0xFF00) >> 8))
    #pragma comment(lib,"ws2_32.lib")
    #include <stdio.h>
    #include <winsock2.h>
    #include <iostream>
    using namespace std;
    #define HTONL(n) (((((unsigned long)(n) & 0xFF)) << 24) |  ((((unsigned long)(n) & 0xFF00)) << 8) | ((((unsigned long)(n) & 0xFF0000)) >> 8) | ((((unsigned long)(n) & 0xFF000000)) >> 24))
    #define HTONS(n) (((unsigned short(n) & 0xFF)) << 8)) |(((unsigned short(n) & 0xFF00) >> 8))
    
    
    void ErrorHandling(char* message)
    {
        fputs(message,stderr);
        fputc('
    ',stderr);
        exit(1);
    }
    int main()
    {
        WSADATA wsaData;    //定义库
        unsigned short host_port = 0x1234;
        unsigned short net_port;
        unsigned long host_addr = 0x12345678;
        unsigned long net_addr;
        if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0); //库初始化
            //ErrorHandling("WSAStartup() error!");
    
        net_port = htons(host_port); //转换为网络字节序
        net_addr = htonl(host_addr);
    
        printf("Host ordered port: %#x 
    ", host_port);
        printf("Network ordered port: %#x 
    ", net_port);
        printf("Host ordered address: %#lx 
    ", host_addr);
        printf("Network ordered address: %#lx 
    ", net_addr);
        WSACleanup();//关闭库
        system("pause");
        return 0;
    }

  • 相关阅读:
    div 居中
    [转贴]Linux新增用户和组
    wmsys.WM_CONCAT
    [转]深刻理解Oracle数据库的启动和关闭
    [转]JAVA 程序中使用ORACLE 绑定变量( bind variable )
    考研的一些入门知识
    [转帖]什么是CPU的核心
    js空格处理函数
    [转]Vi 基础命令
    EMPTY_CLOB()/EMPTY_BLOB()使用
  • 原文地址:https://www.cnblogs.com/Jawen/p/11670498.html
Copyright © 2011-2022 走看看