标签:http io ar os 使用 sp strong 文件 数据
structsockaddr_in
{
shortsin_family;
/*Addressfamily一般来说AF_INET(地址族)PF_INET(协议族)*/
unsignedshortsin_port;
/*Portnumber(必须要采用网络数据格式,普通数字可以用htons()函数转换成网络数据格式的数字)*/
structin_addrsin_addr;
/*Internetaddress*/
unsignedcharsin_zero[8];
/*Samesizeasstructsockaddr没有实际意义,只是为了 跟SOCKADDR结构在内存中对齐*/
};
structsockaddr_in
{
#if(_WIN32_WINNT<0x0600)
shortsin_family;
#else//(_WIN32_WINNT<0x0600)
address_familysin_family;
#endif//(_WIN32_WINNT<0x0600)
ushortsin_port;
in_addrsin_addr;
charsin_zero[8];
}
typedefstructin_addr
{
unsignedlongs_addr;
};
typedefstructin_addr
{
union
{
struct
{unsignedchars_b1,s_b2,s_b3,s_b4;}S_un_b;
struct
{unsignedshorts_w1,s_w2;}S_un_w;
unsignedlongS_addr;
}S_un;
}in_addr;
服务端:
int main()
{
//创建socket
intsockfd=socket(PF_LOCAL,SOCK_DGRAM,0);
if
(sockfd==-1)
perror
(
"创建socket失败"
),
exit
(-1);
//准备通信地址
structsockaddr_unaddr;
addr.sun_family=PF_UNIX;
strcpy
(addr.sun_path,
"a.sock"
);
//绑定
intres=bind(sockfd,(structsockaddr*)&addr,
sizeof
(addr));
if
(res==-1)
perror
(
"绑定失败"
),
exit
(-1);
printf
(
"绑定成功\n"
);
//通信(用读写文件方式)
charbuf[100]={};
read(sockfd,buf,
sizeof
(buf));
printf
(
"收到信息:%s\n"
,buf);
//关闭socket
close(sockfd);
return0;
}
int main()
{
intsockfd=socket(PF_LOCAL,SOCK_DGRAM,0);
if
(sockfd==-1)
perror
(
"创建socket失败"
),
exit
(-1);
structsockaddr_unaddr;
addr.sun_family=PF_UNIX;
strcpy
(addr.sun_path,
"a.sock"
);
//连接
intres=connect(sockfd,(structsockaddr*)&addr,
sizeof
(addr));
if
(res==-1)
perror
(
"失败"
),
exit
(-1);
printf
(
"成功\n"
);
write(sockfd,
"Hello,Socket!"
,14);
close(sockfd);
return0;
}
标签:http io ar os 使用 sp strong 文件 数据
原文地址:http://www.cnblogs.com/sky-of-chuanqingchen/p/4123401.html