what form of socket file descriptor in memory?
int main()
{
int a;
struct sockaddr_in data;
data.sin_addr.s_addr = INADDR_ANY;
data.sin_family = AF_INET;
data.sin_port = htons(1111); //assuming run on little-endian a.k.a x86
a = socket(AF_INET,SOCK_STREAM,0);
bind(a,(struct sockaddr*)&data, sizeof(data));
close(a);
return 0;
}
now focus on variable int a
, im curious, how come an integer
to be file socket descriptor
as the door of 开发者_开发百科receive in
or sending out
the data, how the int a
work exactly in the memory ?
i thought kernel change form of integer a
to pointer to integer a
while initiating the socket, but that just really a thought, what really exactly happened ?
anyone ?
a is a Handle, see e.g. http://en.wikipedia.org/wiki/Handle_%28computing%29
The kernel keeps the structures it needs in a table, so that every time you refer to the handle, it can look up the data it needs.
You could implement a simple handle mechanism with e.g. an array:
struct MyData {
float f;
};
MyData TheArray[100];
void setFloat(int Handle, float v) {
TheArray[Handle].f = v;
}
float getFloat(int Handle) {
return TheArray[Handle].f;
}
The user of your functions only know that setFloat and getFloat exists. Using this, he does not know how you internally keep track of floats, but can still reliably access them using the handle.
精彩评论