v831-openwrt-c-socket篇
前言
socket在python上的调用很简单,但是c上的调用并不那么简单,但是很直观。
需要的库
#include <sys/socket.h> //系统的socket函数所在
#include <netinet/in.h> //系统的socket地址结构体所在
#include <arpa/inet.h> //系统的地质转换函数所在
库详情
指向例子
void socket_init(void)
{
//bzero等价于memset
host.sin_family=AF_INET; //ipv4
host.sin_addr.s_addr=inet_addr("192.168.4.1"); //地址,实际就是将字符串转为32类型
host.sin_port=htons(3333); //端口,in.h中,看不懂司马意思
soc=socket(AF_INET,SOCK_STREAM,0); //ip4tcp的传输,最后一个可以直接设置为0
if(connect(soc,(struct sockaddr*)&host,sizeof(host))!=0) //连接
{
printf("socket connect error!");
assert(0);
}
}
发送函数:
如果关闭则用shotdown,另外,c貌似没有close。