问一下,udp的服务器用什么方式监听客服端发送过来的消息,并且获得客服端的ip和端口,谢谢

例如tcp的tcplistner那样的... 例如tcp的tcplistner那样的 展开
 我来答
化学魔人
2010-08-15 · TA获得超过143个赞
知道答主
回答量:48
采纳率:0%
帮助的人:66.9万
展开全部
先socket,再bind,再recvfrom
服务器端代码:

SOCKADDR_IN svrAddr;
svrAddr.sin_family=AF_INET;
svrAddr.sin_addr.S_un.S_addr=inet_addr(myIP);
svrAddr.sin_port=htons(myPort);

SOCKET svr;
svr=socket(AF_INET,SOCK_DGRAM,0);
if (svr==INVALID_SOCKET)
{
MessageBox(NULL,"socket error","Error!",MB_OK);
goto exit;
}

ir=bind(svr,(SOCKADDR*)&svrAddr,sizeof(SOCKADDR_IN));
if (ir==SOCKET_ERROR)
{
MessageBox(NULL,"bind error","Error!",MB_OK);
}
else
{
SOCKADDR_IN addrClient;
UCHAR buf[1024*4];
int size=sizeof(addrClient);
memset(&addrClient,0,size);
ir=recvfrom(svr,(PCHAR)&buf,sizeof(buf),0,(SOCKADDR*)&addrClient,&size);
// 客户端信息在addrClient里
}

或者采用sniffer的方法:
代码:
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdlib.h>

#pragma comment(lib,"ws2_32.lib")
#define MAX_HOSTNAME_LAN 255
#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
#define MAX_ADDR_LEN 16

struct ipheader {
unsigned char ip_hl:4, ip_v:4; /* this means that each member is 4 bits */
unsigned char ip_tos;
unsigned short int ip_len;
unsigned short int ip_id;
unsigned short int ip_off;
unsigned char ip_ttl;
unsigned char ip_p;
unsigned short int ip_sum;
unsigned int ip_src;
unsigned int ip_dst;
}; /* total ip header length: 20 bytes (=160 bits) */
typedef struct tcpheader {
unsigned short int sport;
unsigned short int dport;
unsigned int th_seq;
unsigned int th_ack;
unsigned char th_x:4;
unsigned char th_off:4;
unsigned char Flags;
unsigned short int th_win;
unsigned short int th_sum;
unsigned short int th_urp;
}TCP_HDR;
typedef struct udphdr {
unsigned short sport;
unsigned short dport;
unsigned short len;
unsigned short cksum;
}UDP_HDR;
void main()
{
SOCKET sock;
WSADATA wsd;
DWORD dwBytesRet;
unsigned int optval = 1;
unsigned char *dataudp,*datatcp;
int i,pCount=0,lentcp, lenudp;
SOCKADDR_IN sa,saSource, saDest;
struct hostent FAR * pHostent;
char FAR name[MAX_HOSTNAME_LAN];
char RecvBuf[65535] = {0};
struct udphdr *pUdpheader;
struct ipheader *pIpheader;
struct tcpheader *pTcpheader;
WSAStartup(MAKEWORD(2,1),&wsd);
if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP))==SOCKET_ERROR)exit(1);
gethostname(name, MAX_HOSTNAME_LAN);
pHostent = gethostbyname(name);
sa.sin_family = AF_INET;
sa.sin_port = htons(6000);
memcpy(&sa.sin_addr.S_un.S_addr, pHostent->h_addr_list[0], pHostent->h_length);
bind(sock, (SOCKADDR *)&sa, sizeof(sa));
if ((WSAGetLastError())==10013)exit(1);
WSAIoctl(sock, SIO_RCVALL, &optval, sizeof(optval), NULL, 0, &dwBytesRet, NULL, NULL);
pIpheader = (struct ipheader *)RecvBuf;
pTcpheader = (struct tcpheader *)(RecvBuf+ sizeof(struct ipheader ));
pUdpheader = (struct udphdr *) (RecvBuf+ sizeof(struct ipheader ));
while (1)
{
memset(RecvBuf, 0, sizeof(RecvBuf));
recv(sock, RecvBuf, sizeof(RecvBuf), 0);
saSource.sin_addr.s_addr = pIpheader->ip_src;
saDest.sin_addr.s_addr = pIpheader->ip_dst;
lentcp =(ntohs(pIpheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct tcpheader)));
lenudp =(ntohs(pIpheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct udphdr)));
if((pIpheader->ip_p)==IPPROTO_TCP&&lentcp!=0)
{
printf("*******************************************\n");
pCount++;
datatcp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct tcpheader);
printf("-TCP-\n");
printf("\nSrc IP:Port->%15s:%i\n",inet_ntoa(saSource.sin_addr),ntohs(pTcpheader->sport));
printf("\nDestIP:Port->%15s:%i\n",inet_ntoa(saDest.sin_addr),ntohs(pTcpheader->dport));
printf("datatcp address->%x\n",datatcp);
printf("size of ipheader->%i\n",sizeof(struct ipheader));
printf("size of tcpheader->%i\n",sizeof(struct tcpheader));
printf("size of the hole packet->%i\n",ntohs(pIpheader->ip_len));
printf("\nchar Packet%i [%i]=\"",pCount,lentcp);
for (i=0;i<lentcp;i++)
{
printf("\\x%.2x",*(datatcp+i));
if (i%10==0)printf("\"\n\"");
}
printf("\";\n\n\n");
for (i=0;i<lentcp;i++)
{
if( *(datatcp+i)<=127&&*(datatcp+i)>=20)printf("%c",*(datatcp+i));
else printf(".");
}
printf("\n\n*******************************************\n");
}
if((pIpheader->ip_p)==IPPROTO_UDP&&lenudp!=0)
{
pCount++;
dataudp=(unsigned char *) RecvBuf+sizeof(struct ipheader)+sizeof(struct udphdr);
printf("-UDP-\n");
printf("\nSrc IP:Port->%15s:%i\n",inet_ntoa(saSource.sin_addr),ntohs(pUdpheader->sport));
printf("\nDestIP:Port->%15s:%i\n",inet_ntoa(saDest.sin_addr),ntohs(pUdpheader->dport));
printf("dataudp address->%x\n",dataudp);
printf("size of ipheader->%i\n",sizeof(struct ipheader));
printf("size of udpheader->%i\n",sizeof(struct udphdr));
printf("size of the hole packet->%i\n",ntohs(pIpheader->ip_len));
printf("\nchar Packet%i [%i]=\"",pCount,lenudp);
for (i=0;i<lenudp;i++)
{
printf("\\x%.2x",*(dataudp+i));
if (i%10==0)printf("\"\n\"");
}
printf("\";\n\n\n");
for (i=0;i<lenudp;i++)
{
if( *(dataudp+i)<=127&&*(dataudp+i)>=20)printf("%c",*(dataudp+i));
else printf(".");
}
printf("\n\n*******************************************\n");
}
}
}
这样tcp udp通吃。
Storm代理
2023-05-30 广告
StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,IP... 点击进入详情页
本回答由Storm代理提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式