为什么不能连接到目标机器.源代码:
sock=socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in your_addr;
hostent *hont;
hont=gethostbyname("10.42.0.10");
your_addr.sin_family=AF_INET;
your_addr.sin_port=htons(2000);
memcpy(&your_addr.sin_addr.S_un.S_addr,hont->h_name,hont->h_name,hont->h_length);
connect(sock,(struct sockaddr *)&your_addr,sizeof(your_addr));为什么老是连接失败?
初学socket,没有人指点,求助CSDN上的朋友.
sock=socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in your_addr;
hostent *hont;
hont=gethostbyname("10.42.0.10");
your_addr.sin_family=AF_INET;
your_addr.sin_port=htons(2000);
memcpy(&your_addr.sin_addr.S_un.S_addr,hont->h_name,hont->h_name,hont->h_length);
connect(sock,(struct sockaddr *)&your_addr,sizeof(your_addr));为什么老是连接失败?
初学socket,没有人指点,求助CSDN上的朋友.
另外,都知道ip地址了,何必还用gethostbyname?
直接输入IP就可以了吧不用GETNAME
还有,最好的呢,调用了host = gethostbyname("...");后,要判断一下一host是不是等于 NULL。
your_addr.sin_addr.S_un.S_addr = htonl("10.42.0.10");
long ad = inet_addr("10.42.0.10");
memcpy(&your_addr.sin_addr, &ad, 4);
memset(&your_addr, 0, sizeof(sockaddr_in));your_addr.sin_addr.S_un.S_addr = inet_addr("10.42.0.10");
your_addr.sin_family=AF_INET;
your_addr.sin_port=htons(2000);connect(sock,(struct sockaddr *)&your_addr,sizeof(your_addr));
的参数好像不是IP吧,应该是gethostname()后得到的计算机名字啊