正点原子407开发板SAL实验遇阻

发布于 2019-10-12 17:33:59    浏览:497
各位好,今天想研究一下RT的SAL组件,配置了使能标准BSD SOCKET 结果总是connect failed ,贴上我的代码帮忙分析一下:#define SERVER_HOST "172.20.2.170"
#define SERVER_PORT 1234


int netdev_set_ipaddr_test(int argc, char **argv)
{


struct sockaddr_in server_addr;
int sockfd = -1;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
rt_kprintf("Socket create failed.\n");
return -RT_ERROR;
}
// /* 初始化预连接的服务端地址 */
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = inet_addr(SERVER_HOST);
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));

if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0)
{
rt_kprintf("socket connect failed!\n");
closesocket(sockfd);
return -RT_ERROR;
}
else
{
rt_kprintf("socket connect success!\n");
}
send(sockfd,"hello socket",10,0);
closesocket(sockfd);

}
跟踪调试发现
int connect(int s, const struct sockaddr *name, socklen_t namelen)
{
int socket = dfs_net_getsocket(s);

return sal_connect(socket, name, namelen);
}
这里 socket=0



查看更多

1 个回答
tyustli
tyustli 2019-10-12
This guy hasn't written anything yet
楼主如果是第一次使用网络部分的话建议参考现有的例程操作成功之后再看源码
https://www.rt-thread.org/docume ... t/an0014-at-client/

撰写答案

请登录后再发布答案,点击登录
关注者
0
被浏览
497

发布
问题

分享
好友

手机
浏览

扫码手机浏览