Use*_*422 44 c sockets unix-socket
我是socket编程的新手,我正在努力了解它的操作htons().我已经在互联网上一样阅读一些教程这和这一个实例.但我无法理解究竟htons()是什么.我尝试了以下代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main( int argc, char *argv[] )
{
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("ERROR opening socket");
exit(1);
}
/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 5001;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
/* Now bind the host address using bind() call.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
{
perror("ERROR on binding");
exit(1);
}
/* Now start listening for the clients, here process will
* go in sleep mode and will wait for the incoming connection
*/
listen(sockfd,5);
clilen = sizeof(cli_addr);
/* Accept actual connection from the client */
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr,
&clilen);
if (newsockfd < 0)
{
perror("ERROR on accept");
exit(1);
}
/* If connection is established then start communicating */
bzero(buffer,256);
n = read( newsockfd,buffer,255 );
if (n < 0)
{
perror("ERROR reading from socket");
exit(1);
}
printf("Here is the message: %s\n",buffer);
/* Write a response to the client */
n = write(newsockfd,"I got your message",18);
if (n < 0)
{
perror("ERROR writing to socket");
exit(1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
值sin_port被显示为35091在调试时,我不知道如何portno从改变5001到35091.有人可以解释价值变化的原因吗?
brm*_*brm 97
它与字节存储在内存中的顺序有关.十进制数5001是 0x1389十六进制数,因此涉及的字节数为0x13和0x89.许多设备以little-endian格式存储数字,这意味着最低有效字节首先出现.因此,在这个特定的例子中,它意味着在内存中数字5001将被存储为
0x89 0x13
Run Code Online (Sandbox Code Playgroud)
该htons()函数确保数字以网络字节顺序存储在存储器中,该字节顺序是最重要的字节.因此,它将交换组成数字的字节,以便在内存中字节将按顺序存储
0x13 0x89
Run Code Online (Sandbox Code Playgroud)
在little-endian机器上,交换字节的数字是0x8913十六进制,35091采用十进制表示法.请注意,如果您正在使用大端机器,则该htons()函数不需要进行任何交换,因为该数字已经以正确的方式存储在内存中.
所有这些交换的根本原因与使用的网络协议有关,这需要传输的数据包使用网络字节顺序.
Sal*_*gar 31
htons 是 host-to-network short
这意味着它适用于16位短整数.即2个字节.
此函数交换短的字节序.
您的电话号码开头于:
0001 0011 1000 1001 = 5001
更改字节顺序时,它会交换两个字节:
1000 1001 0001 0011 = 35091
It is done to maintain the arrangement of bytes which is sent in the network(Endianness). Depending upon architecture of your device,data can be arranged in the memory either in the big endian format or little endian format. In networking, we call the representation of byte order as network byte order and in our host, it is called host byte order. All network byte order is in big endian format.If your host's memory computer architecture is in little endian format,htons() function become necessity but in case of big endian format memory architecture,it is not necessary.You can find endianness of your computer programmatically too in the following way:->
int x = 1;
if (*(char *)&x){
cout<<"Little Endian"<<endl;
}else{
cout<<"Big Endian"<<endl;
}
Run Code Online (Sandbox Code Playgroud)
and then decide whether to use htons() or not.But in order to avoid the above line,we always write htons() although it does no changes for Big Endian based memory architecture.
| 归档时间: |
|
| 查看次数: |
105993 次 |
| 最近记录: |