有办法没
8 个解决方案
#1
// Listener3389.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "winsock2.h"
#include "time.h"
using namespace std;
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
int _tmain(int argc, _TCHAR* argv[])
{
WSAEVENT EventArray;
SOCKET ListenSocket;
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
//-----------------------------------------
// Create a listening socket bound to a local
// IP address and the port specified
ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_port = htons(23); //修改为想监听的端口号
service.sin_addr.s_addr = INADDR_ANY;
//-----------------------------------------
// Bind the listening socket to the local IP address
// and port number
if(!bind(ListenSocket, (SOCKADDR *) &service, sizeof(SOCKADDR)))
{
printf("Binded!!\n");;}
//-----------------------------------------
// Set the socket to listen for incoming
// connection requests
listen(ListenSocket, 5);
printf("Listening...\n");
//-----------------------------------------
// Create an event handle and setup an overlapped structure.
EventArray = WSACreateEvent();
WSAEventSelect( ListenSocket,EventArray, FD_ACCEPT);
time_t rawtime;
struct tm * timeinfo;
ofstream o_file;
SOCKADDR_IN tmp;
int tmplen=16;
unsigned long ip;
unsigned char num;
while(1)
{
WSAWaitForMultipleEvents(1, &EventArray, FALSE, WSA_INFINITE, FALSE);
o_file.open("tmp.txt", ios::app);
o_file <<"\nInComing Connection\nIP:";
closesocket(accept(ListenSocket,(SOCKADDR *)&tmp,&tmplen));
ip=tmp.sin_addr.S_un.S_addr;
while(ip)
{
num=ip;
int i=num;
o_file << i ;
if(ip>255)
o_file << ".";
ip>>=8;
}
o_file << " Port:"<<ntohs(tmp.sin_port);
time ( &rawtime );
timeinfo = localtime ( &rawtime );
o_file << " Time: "<<asctime (timeinfo) ;
WSAResetEvent(EventArray);
o_file.close();
}
}
监听记录如下
InComing Connection
IP:172.25.51.73 //远程ip地址
Port:3036 //远程端口号
Time: Wed Sep 07 15:08:18 2011 //记录时间
以前弄着玩的 不知道合不合你要求
#2
我的意思是.我要查机器的某个端口在某一时刻是监听还是没监听
如3300端口,
我想查看9点的时候,它是否被监听了
如3300端口,
我想查看9点的时候,它是否被监听了
#3
用shell命令:netstat之类的
#4
楼上的想法不错,netstat把端口信息重定向到文件中,再加上时间,之后,访问这个文件来查看某个端口某个时刻的状态
#5
windows 如果做?
#6
windows一样有netstat命令的
#7
3Q.
可惜没有直接查的命令.要查以前的是没办法了,
希望这种异常可以再现
#8
char cmd[256];
int port=135;
sprintf(cmd,"echo %%date%% %%time%% >>%d.txt&netstat -na | find \"LISTENING\" | find /c \":%d \" >>%d.txt",port,port,port);
system(cmd);
//读文件135.txt的内容
#1
// Listener3389.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "winsock2.h"
#include "time.h"
using namespace std;
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
int _tmain(int argc, _TCHAR* argv[])
{
WSAEVENT EventArray;
SOCKET ListenSocket;
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
//-----------------------------------------
// Create a listening socket bound to a local
// IP address and the port specified
ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_port = htons(23); //修改为想监听的端口号
service.sin_addr.s_addr = INADDR_ANY;
//-----------------------------------------
// Bind the listening socket to the local IP address
// and port number
if(!bind(ListenSocket, (SOCKADDR *) &service, sizeof(SOCKADDR)))
{
printf("Binded!!\n");;}
//-----------------------------------------
// Set the socket to listen for incoming
// connection requests
listen(ListenSocket, 5);
printf("Listening...\n");
//-----------------------------------------
// Create an event handle and setup an overlapped structure.
EventArray = WSACreateEvent();
WSAEventSelect( ListenSocket,EventArray, FD_ACCEPT);
time_t rawtime;
struct tm * timeinfo;
ofstream o_file;
SOCKADDR_IN tmp;
int tmplen=16;
unsigned long ip;
unsigned char num;
while(1)
{
WSAWaitForMultipleEvents(1, &EventArray, FALSE, WSA_INFINITE, FALSE);
o_file.open("tmp.txt", ios::app);
o_file <<"\nInComing Connection\nIP:";
closesocket(accept(ListenSocket,(SOCKADDR *)&tmp,&tmplen));
ip=tmp.sin_addr.S_un.S_addr;
while(ip)
{
num=ip;
int i=num;
o_file << i ;
if(ip>255)
o_file << ".";
ip>>=8;
}
o_file << " Port:"<<ntohs(tmp.sin_port);
time ( &rawtime );
timeinfo = localtime ( &rawtime );
o_file << " Time: "<<asctime (timeinfo) ;
WSAResetEvent(EventArray);
o_file.close();
}
}
监听记录如下
InComing Connection
IP:172.25.51.73 //远程ip地址
Port:3036 //远程端口号
Time: Wed Sep 07 15:08:18 2011 //记录时间
以前弄着玩的 不知道合不合你要求
#2
我的意思是.我要查机器的某个端口在某一时刻是监听还是没监听
如3300端口,
我想查看9点的时候,它是否被监听了
如3300端口,
我想查看9点的时候,它是否被监听了
#3
用shell命令:netstat之类的
#4
楼上的想法不错,netstat把端口信息重定向到文件中,再加上时间,之后,访问这个文件来查看某个端口某个时刻的状态
#5
windows 如果做?
#6
windows一样有netstat命令的
#7
3Q.
可惜没有直接查的命令.要查以前的是没办法了,
希望这种异常可以再现
#8
char cmd[256];
int port=135;
sprintf(cmd,"echo %%date%% %%time%% >>%d.txt&netstat -na | find \"LISTENING\" | find /c \":%d \" >>%d.txt",port,port,port);
system(cmd);
//读文件135.txt的内容