原代码:
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,请大佬们指教,如果可以请给出具体修改方法,谢谢