c++??python???????????????????????????????
???????????? ???????[ 2017/3/15 10:24:40 ] ??????????????????? .NET python
?????????????????????????????????????????У??????????????????????????????????????????????У?????????????????????????????????????????????????????c++??python??????????????????????????????????????????????????????????????????????
?????????????????????????????????????????????????????????С?????????????????????????????????????????????ж???????????????????????С?????μ????????????????????????????4???д???
????????????????TCP/IP?й漲?????????????????????????CPU??????????????????????????????????????????????????????????????????????big endian?????????c++??python?У???4??????????????????????????????????????????£?
????htons?? ??unsigned short??????????????????????
????htonl?? ??unsigned long??????????????????????
????ntohs?? ??unsigned short??????????????????????
????ntohl?? ??unsigned long??????????????????????
???????1) ?????little endian?????? ??Щ??????????????????? ; ?????big endian????????? ????4???????????????????????κ????????κ??????
????2) ??С?????У??????int??long????????????????????紫???в??????????????????
?????????????????????linux??windows????c++?????????????????python??????????????????????????????С???????
????1.linux + c++ ??????????????????????????????
????1 #include <arpa/inet.h> //1.????arpa/inet.h
????2 #include <stdio.h>
????3 int main()
????4 {
????5 int port=6000;
????6 int netPort=htonl(port);
????7 printf("netPort=%d
"??netPort);
????8 printf("hostPort=%d
"??ntohl(netPort));
????9 return 1;
????10 }<br><br>output??<br>????netPort=1880555520<br>????hostPort=6000??
????2.windows + c++ ??????????????????????????????
????1 #include <stdio.h>
????2 #include <Winsock2.h> //1.????<Winsock2.h>??
????3 #pragma comment( lib?? "ws2_32.lib") //2.????ws2_32.lib??
????4 void main()
????5 {
????6 int port=6000;
????7 int netPort=htonl(port);
????8 printf("netPort=%d
"??netPort);
????9 printf("hostPort=%d
"??ntohl(netPort));
????10 }<br><br>output??<br>????netPort=1880555520<br>????hostPort=6000??
????3.python ??????????????????????????????????
????python?????????????????????????????????????????????????????????????????htons??htonl??ntohl??ntohs???????????????????struct????pack??unpack????????????struct????pack??unpack????????÷??????ο?????????£?python??struct.pack()??????struct.unpack()????
????3.1 htons??htonl??ntohl??ntohs????????????????
????1 #!/usr/bin/python
????2 #coding=utf-8
????3 import socket
????4 port = 6000
????5 netPort = socket.htonl(port)
????6 hostPort = socket.ntohl(netPort)
????7 print"netPort:"?? netPort
????8 print"hostPort:"?? hostPort<br><br>output??<br>????netPort: 1880555520<br>????hostPort: 6000
????3.2 struct????pack??unpack???????????????????
????1 #!/usr/bin/python
????2 #coding=utf-8
????3 from struct import *
????4 port = 6000
????5 netPort = pack(">i"??port) #?????htonl(port)???????pack????????????????
????6 hostPort = unpack(">i"??netPort)
????7 print "netPort:"??repr(netPort)
????8 print "hostPort:"??repr(hostPort[0])<br><br>output??<br>????netPort: 'x00x00x17p'<br>????hostPort: 6000
????????????????????е?????????????????????????????????????β????????????????????????????c++??????htonl????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11