首页 新闻 会员 周边 捐助

关于c++ winsocket

0
悬赏园豆:10 [已关闭问题] 关闭于 2020-03-18 11:22

原代码:

include<iostream>

include<winsock2.h>

pragma comment(lib,"Ws2_32.lib")

using namespace std;
in_addr transip(in_addr& n, hostent* m)
{
n.S_un.S_addr = inet_addr(m->h_addr_list[0]);
n.S_un.S_un_b.s_b1 = m->h_addr_list[0][0];
n.S_un.S_un_b.s_b2 = m->h_addr_list[0][1];
n.S_un.S_un_b.s_b3 = m->h_addr_list[0][2];
n.S_un.S_un_b.s_b4 = m->h_addr_list[0][3];
n.S_un.S_un_w.s_w1 = m->h_addr_list[0][0] + 256 * m->h_addr_list[0][1]+256;
n.S_un.S_un_w.s_w2 = m->h_addr_list[0][2] + 256 * m->h_addr_list[0][3];
return n;
}
sockaddr_in trans(sockaddr_in& q, in_addr n)
{
q.sin_zero;
q.sin_family = PF_INET;
q.sin_addr = n;
q.sin_port = htons(getservbyname("http", "tcp")->s_port);
return q;
}
int main()
{
WORD wVersionRequested = MAKEWORD(2, 1);
WSADATA wsaData;
sockaddr_in localaddr;
in_addr m;
int err = WSAStartup(wVersionRequested, &wsaData);
if (err == 0)
{
char str[20] = "192.168.0.103";
SOCKET sd = socket(PF_INET, SOCK_STREAM, getprotobyname("tcp")->p_proto);
transip(m, gethostbyname(str));
trans(localaddr, m);
localaddr.sin_port = htons(80);
bind(sd, (sockaddr*)&localaddr, sizeof(sockaddr));
}
WSACleanup();
return 0;
}
问题:bind函数总是返回-1,请大佬们指教,如果可以请给出具体修改方法,谢谢

芦苇nb的主页 芦苇nb | 初学一级 | 园豆:197
提问于:2020-03-18 10:02
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册