如何读取 tcp 端口数据?

时间:2022-12-31 15:38:09
我想检测本机上某个端口是否有数据,并且想知道是什么数据,该怎么办?
请大侠帮忙!!

6 个解决方案

#1


up

#2


比较简单的方法是用raw socket

烦一点的用spi

#3


监听端口啊。

#4


下面是一段监听的代码,自己改吧

#5


CRawRevDlg *pMainDlg=(CRawRevDlg *)AfxGetApp()->m_pMainWnd;
CString strIpToDo=pMainDlg->strIpToDo;
CString strIpRemote=pMainDlg->strIpRemote;
CString strPort=pMainDlg->strPort;
int nCount=0;
//////////////////////////////////////////////////////////////////
WSADATA WSAData; 
SOCKET sock; 

IPHEADER ipHeader; 
TCPHEADER tcpHeader; 
BOOL flag;

if (WSAStartup(MAKEWORD(2,2), &WSAData)!=0) 

printf("WSAStartup Error!n"); 
return FALSE; 


if((sock=socket(AF_INET, SOCK_RAW, IPPROTO_RAW))
==INVALID_SOCKET) 

printf("Socket Setup Error!n"); 
return FALSE; 
}
flag=true; 

if (setsockopt(sock,IPPROTO_IP, IP_HDRINCL,(char*)&flag,sizeof(flag))==SOCKET_ERROR) 

printf("setsockopt IP_HDRINCL error!n"); 
return FALSE; 
}

char szNameParam[1024];
ZeroMemory(szNameParam,1024);
gethostname(szNameParam,1024);
// AfxMessageBox(&szNameParam[0]);
LPHOSTENT lpHostEntry;
lpHostEntry=gethostbyname(&szNameParam[0]);
    if (lpHostEntry == NULL)
    {
        MessageBox(NULL,"gethostbyname() error",NULL,MB_OK);
    }
// memcpy(SourIP,inet_ntoa(*(LPIN_ADDR)lpHostEntry->h_addr),16);
int addrlen;

SOCKADDR_IN sockAddr;

getsockname(sock,(struct sockaddr *)&sockAddr,&addrlen);
sockAddr.sin_family=AF_INET;
sockAddr.sin_port=htons(2100);
// memcpy(SourIP,inet_ntoa(*(LPIN_ADDR)lpHostEntry->h_addr),16);
char strTempSIp[20];
memcpy(strTempSIp,inet_ntoa(*(LPIN_ADDR)lpHostEntry->h_addr),16);
sockAddr.sin_addr.s_addr=inet_addr(strTempSIp); 
    if(bind(sock,(SOCKADDR*)&sockAddr,sizeof(sockAddr))==SOCKET_ERROR)
{
return FALSE;
}       

DWORD lpvBuffer = 1; 
DWORD lpcbBytesReturned = 0 ; 
WSAIoctl(sock,SIO_RCVALL,&lpvBuffer,sizeof(lpvBuffer),NULL, 0, & 
lpcbBytesReturned, NULL, NULL);

unsigned int  sourceIP;
unsigned int  destIP;
unsigned char port;

unsigned char sbyte1;
unsigned char sbyte2;
unsigned char sbyte3;
unsigned char sbyte4;

unsigned char dbyte1;
unsigned char dbyte2;
unsigned char dbyte3;
unsigned char dbyte4;

char pBuffer[1024*100];

CString strDIp;
CString strSIp;

int PacketLen;

#6


for(;;)
{
ZeroMemory(pBuffer,1024*100);
PacketLen=recv(sock,(char*)pBuffer,1024*100,0);
memcpy(&ipHeader,pBuffer,sizeof(IPHEADER));
memcpy(&tcpHeader,&pBuffer[20],sizeof(TCPHEADER));
port=(unsigned char)ntohs(tcpHeader.th_dport);

sourceIP=ipHeader.sourceIP;
destIP=ipHeader.destIP;

sbyte1=sourceIP>>24;
sbyte2=sourceIP>>16;
sbyte3=sourceIP>>8;
sbyte4=(unsigned char)sourceIP;
strSIp.Format("%d.%d.%d.%d",sbyte4,sbyte3,sbyte2,sbyte1);

dbyte1=destIP>>24;
dbyte2=destIP>>16;
dbyte3=destIP>>8;
dbyte4=(unsigned char)destIP;
strDIp.Format("%d.%d.%d.%d",dbyte4,dbyte3,dbyte2,dbyte1);

CString strTempPort;
strTempPort.Format("%d",port);

////////////////////////////////////////////////////////////////////////////
if((!pMainDlg->bPort)&&(!pMainDlg->bIpC))
{
if(!strIpToDo.Compare(strSIp))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{
pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);

nCount++;
}
if(!strIpToDo.Compare(strDIp))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{

pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);
nCount++;
}

}
////////////////////////////////////////////////////////////////////////////
else if(pMainDlg->bPort&&pMainDlg->bIpC)
{
if(!strTempPort.Compare(strPort))
{
if((!strIpToDo.Compare(strSIp))&&(!strIpRemote.Compare(strDIp)))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{

pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);

nCount++;
}
if((!strIpToDo.Compare(strDIp))&&(!strIpRemote.Compare(strSIp)))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{

pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);

nCount++;
}
}
}
////////////////////////////////////////////////////////////////////////////
else if(pMainDlg->bPort)
{
if(!strTempPort.Compare(strPort))
{
if(!strIpToDo.Compare(strSIp))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{

pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);

nCount++;
}
if(!strIpToDo.Compare(strDIp))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{

pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);
nCount++;
}
}
}
////////////////////////////////////////////////////////////////////////////
else if(pMainDlg->bIpC)
{
if((!strIpToDo.Compare(strSIp))&&(!strIpRemote.Compare(strDIp)))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{

pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);

nCount++;
}
if((!strIpToDo.Compare(strDIp))&&(!strIpRemote.Compare(strSIp)))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{
pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);

nCount++;
}

}
CString strCount;
strCount.Format("%d",nCount);
pMainDlg->m_static.SetWindowText(strCount);

}
}

#1


up

#2


比较简单的方法是用raw socket

烦一点的用spi

#3


监听端口啊。

#4


下面是一段监听的代码,自己改吧

#5


CRawRevDlg *pMainDlg=(CRawRevDlg *)AfxGetApp()->m_pMainWnd;
CString strIpToDo=pMainDlg->strIpToDo;
CString strIpRemote=pMainDlg->strIpRemote;
CString strPort=pMainDlg->strPort;
int nCount=0;
//////////////////////////////////////////////////////////////////
WSADATA WSAData; 
SOCKET sock; 

IPHEADER ipHeader; 
TCPHEADER tcpHeader; 
BOOL flag;

if (WSAStartup(MAKEWORD(2,2), &WSAData)!=0) 

printf("WSAStartup Error!n"); 
return FALSE; 


if((sock=socket(AF_INET, SOCK_RAW, IPPROTO_RAW))
==INVALID_SOCKET) 

printf("Socket Setup Error!n"); 
return FALSE; 
}
flag=true; 

if (setsockopt(sock,IPPROTO_IP, IP_HDRINCL,(char*)&flag,sizeof(flag))==SOCKET_ERROR) 

printf("setsockopt IP_HDRINCL error!n"); 
return FALSE; 
}

char szNameParam[1024];
ZeroMemory(szNameParam,1024);
gethostname(szNameParam,1024);
// AfxMessageBox(&szNameParam[0]);
LPHOSTENT lpHostEntry;
lpHostEntry=gethostbyname(&szNameParam[0]);
    if (lpHostEntry == NULL)
    {
        MessageBox(NULL,"gethostbyname() error",NULL,MB_OK);
    }
// memcpy(SourIP,inet_ntoa(*(LPIN_ADDR)lpHostEntry->h_addr),16);
int addrlen;

SOCKADDR_IN sockAddr;

getsockname(sock,(struct sockaddr *)&sockAddr,&addrlen);
sockAddr.sin_family=AF_INET;
sockAddr.sin_port=htons(2100);
// memcpy(SourIP,inet_ntoa(*(LPIN_ADDR)lpHostEntry->h_addr),16);
char strTempSIp[20];
memcpy(strTempSIp,inet_ntoa(*(LPIN_ADDR)lpHostEntry->h_addr),16);
sockAddr.sin_addr.s_addr=inet_addr(strTempSIp); 
    if(bind(sock,(SOCKADDR*)&sockAddr,sizeof(sockAddr))==SOCKET_ERROR)
{
return FALSE;
}       

DWORD lpvBuffer = 1; 
DWORD lpcbBytesReturned = 0 ; 
WSAIoctl(sock,SIO_RCVALL,&lpvBuffer,sizeof(lpvBuffer),NULL, 0, & 
lpcbBytesReturned, NULL, NULL);

unsigned int  sourceIP;
unsigned int  destIP;
unsigned char port;

unsigned char sbyte1;
unsigned char sbyte2;
unsigned char sbyte3;
unsigned char sbyte4;

unsigned char dbyte1;
unsigned char dbyte2;
unsigned char dbyte3;
unsigned char dbyte4;

char pBuffer[1024*100];

CString strDIp;
CString strSIp;

int PacketLen;

#6


for(;;)
{
ZeroMemory(pBuffer,1024*100);
PacketLen=recv(sock,(char*)pBuffer,1024*100,0);
memcpy(&ipHeader,pBuffer,sizeof(IPHEADER));
memcpy(&tcpHeader,&pBuffer[20],sizeof(TCPHEADER));
port=(unsigned char)ntohs(tcpHeader.th_dport);

sourceIP=ipHeader.sourceIP;
destIP=ipHeader.destIP;

sbyte1=sourceIP>>24;
sbyte2=sourceIP>>16;
sbyte3=sourceIP>>8;
sbyte4=(unsigned char)sourceIP;
strSIp.Format("%d.%d.%d.%d",sbyte4,sbyte3,sbyte2,sbyte1);

dbyte1=destIP>>24;
dbyte2=destIP>>16;
dbyte3=destIP>>8;
dbyte4=(unsigned char)destIP;
strDIp.Format("%d.%d.%d.%d",dbyte4,dbyte3,dbyte2,dbyte1);

CString strTempPort;
strTempPort.Format("%d",port);

////////////////////////////////////////////////////////////////////////////
if((!pMainDlg->bPort)&&(!pMainDlg->bIpC))
{
if(!strIpToDo.Compare(strSIp))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{
pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);

nCount++;
}
if(!strIpToDo.Compare(strDIp))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{

pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);
nCount++;
}

}
////////////////////////////////////////////////////////////////////////////
else if(pMainDlg->bPort&&pMainDlg->bIpC)
{
if(!strTempPort.Compare(strPort))
{
if((!strIpToDo.Compare(strSIp))&&(!strIpRemote.Compare(strDIp)))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{

pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);

nCount++;
}
if((!strIpToDo.Compare(strDIp))&&(!strIpRemote.Compare(strSIp)))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{

pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);

nCount++;
}
}
}
////////////////////////////////////////////////////////////////////////////
else if(pMainDlg->bPort)
{
if(!strTempPort.Compare(strPort))
{
if(!strIpToDo.Compare(strSIp))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{

pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);

nCount++;
}
if(!strIpToDo.Compare(strDIp))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{

pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);
nCount++;
}
}
}
////////////////////////////////////////////////////////////////////////////
else if(pMainDlg->bIpC)
{
if((!strIpToDo.Compare(strSIp))&&(!strIpRemote.Compare(strDIp)))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{

pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);

nCount++;
}
if((!strIpToDo.Compare(strDIp))&&(!strIpRemote.Compare(strSIp)))
{
pFile.SeekToEnd();
pFile.Write(strSIp.GetBuffer(strSIp.GetLength()),strSIp.GetLength());
pFile.Write("--->",6);
pFile.Write(strDIp.GetBuffer(strDIp.GetLength()),strDIp.GetLength());
pFile.Write(" port:",7);
pFile.Write(strTempPort.GetBuffer(strTempPort.GetLength()),strTempPort.GetLength());

if(pMainDlg->m_checkWrite.GetCheck())
{
pFile.Write("\r\n",2);
pFile.Write(&pBuffer[40],PacketLen-20-20);

}
pFile.Write("\r\n",2);

nCount++;
}

}
CString strCount;
strCount.Format("%d",nCount);
pMainDlg->m_static.SetWindowText(strCount);

}
}