网络接口调试器
Posted skdev
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网络接口调试器相关的知识,希望对你有一定的参考价值。
模拟串口调试器,为了方便网络调试作的,哪位朋友喜欢的话EMAIL:
songkun607@yahoo.com.cn,主要功能是连收客户机的连接,发送和接收数据,可以自动获取本地IP,可以ASCII和十六进制的显示。
以下是我写的一个服务类,主要针对SDK编程
///wsaserver.h//
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib,"WS2_32.lib")
#define
WM_SOCKET WM_USER+1
class WsaServer
{
public:
WsaServer();
~WsaServer();
int SetServer(HWND hWnd,char *IP,int port);
int WsaAccept(HWND hWnd);
int WsaRecv(SOCKET clientfrm,char *buff,int len);
int WsaSend(char *buff);
int SetClient(SOCKET clientfrm);
int GetLocalIP(char FAR *name,char FAR *ip);
private:
/*WSAAsyncSelect 句柄,参数设置*/
WSADATA wsa;
SOCKET m_server,m_client;
SOCKADDR_IN ServerAddr,ClientAddr;
};
///wsaserver.cpp//
#include "wsaserver.h"
WsaServer::WsaServer()
{
WSAStartup(MAKEWORD(2,2), &wsa);
}
WsaServer::~WsaServer()
{
WSACleanup( );
}
/*名称:SetServer
功能:设置IP和端口
参数:hWnd窗口句柄,IP服务器IP,port端口号
*/
int WsaServer::SetServer(HWND hWnd,char *IP,int port)
{
m_server=socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
ServerAddr.sin_family=AF_INET;
if(strcmp(IP,"")==1)
ServerAddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
else
ServerAddr.sin_addr.S_un.S_addr=inet_addr(IP);
ServerAddr.sin_port=htons(port);
bind(m_server, (PSOCKADDR) &ServerAddr,sizeof(ServerAddr));
WSAAsyncSelect(m_server,hWnd,WM_SOCKET,FD_ACCEPT|FD_CLOSE);
listen(m_server, 5);
return 1;
}
/*名称:WsaAccept
功能:接收客户端的接连
参数:hWnd窗口句柄,clietnfrm客户区的socket
*/
int WsaServer::WsaAccept(HWND hWnd)
{
int caddrlen;
caddrlen=sizeof(ClientAddr);
m_client=accept(m_server,(PSOCKADDR)&ClientAddr,&caddrlen);
WSAAsyncSelect(m_client,hWnd,WM_SOCKET,FD_READ|FD_WRITE|FD_CLOSE);
return 1;
}
/*名称:WsaRecv
功能:接收客户端发来的数据
参数:客户区socket句柄
*/
int WsaServer::WsaRecv(SOCKET clientfrm,char *buff,int len)
{
int ret;
ret=recv((SOCKET)clientfrm,buff,len,0);
buff[ret]='/0';
return ret;
}
/*名称:WsaSend
功能:发送数据
参数:数据缓存buff
*/
int WsaServer::WsaSend(char *buff)
{
int ret;
ret=send(m_client,buff,strlen(buff),0);
return 1;
}
/*名称:SetClient
功能:获取当前client句柄
参数:clientfrm句柄
*/
int WsaServer::SetClient(SOCKET clientfrm)
{
m_client=clientfrm;
return 1;
}
/*名称:GetLocalIP
功能:获取本机名和IP
参数:name 保存的本机名,IP保存的本机IP
*/
int WsaServer::GetLocalIP(char FAR *name,char FAR *ip)
{
HOSTENT *hostinfo;
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
strcpy(ip,inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list));
return 1;
}
}
return 0;
}
以上是关于网络接口调试器的主要内容,如果未能解决你的问题,请参考以下文章