环球科创网

socket编程原理(socket编程)

更新时间:2023-08-03 04:33:08

导读 大家好,我是小环,我来为大家解答以上问题。socket编程原理,socket编程很多人还不知道,现在让我们一起来看看吧!1、#include #includ...

大家好,我是小环,我来为大家解答以上问题。socket编程原理,socket编程很多人还不知道,现在让我们一起来看看吧!

1、#include #include #include #include #pragma comment (lib,"ws2_32.lib") int main (int argc, char *argv[]) { int iportFrom,iportTo; int testsocket; int iopenedport = 0; struct sockaddr_in target_addr; WSADATA wsaData; WORD wVersionRequested=MAKEWORD(1,1); if (argc <= 3) { cout << "使用格式 : " << argv[0] << " 主机IP地址 开始端口号 结束端口号 " << endl; exit(1); } if (atoi (argv[2]) > atoi (argv[3])) { cout << "错误!开始端口号必须小于结束端口号" << endl; exit(1); } else { if (WSAStartup (wVersionRequested , &wsaData) ) { cout << "连接socket库失败。

2、请检查版本号是否为1.1 " << endl; exit(1); } iportFrom=atoi (argv[2]); iportTo=atoi (argv[3]); for (int i=iportFrom; i <= iportTo; i++) { cout << "正在建立socket................................" << endl; if ((testsocket=socket (AF_INET,SOCK_STREAM,0) ) == INVALID_SOCKET) { cout << "Socket建立失败!" << endl; exit(0); } target_addr.sin_family = AF_INET; target_addr.sin_port = htons(i); target_addr.sin_addr.s_addr = inet_addr (argv[1]); cout << "正在扫描端口:" << i << endl; if (connect (testsocket, (struct sockaddr *) &target_addr, sizeof(struct sockaddr)) == SOCKET_ERROR) cout << "端口" << i << "关闭!" << endl; else { iopenedport++; cout << "端口" << i << "开放 " << endl; } } cout << "目标主机" << argv[1] << "从" << iportFrom << "--" << iportTo << "共有" << iopenedport << "个端口开放" << endl; closesocket (testsocket); WSACleanup(); } return 0; } vc6.0 下 编译

本文到此讲解完毕了,希望对大家有帮助。

免责声明:本文由用户上传,如有侵权请联系删除!