C# socket 如何获取收到数据的事件?
在C#中我引用了socket类,但是找不到收到数据时应触发的事件,就像在VB中的winsock控件收到数据时会立即触发DataArrival事件.请代码写的尽量简洁,与问...
在C#中我引用了socket类,但是找不到收到数据时应触发的事件,就像在VB中的winsock控件收到数据时会立即触发DataArrival事件.请代码写的尽量简洁,与问题无关的代码请勿放上来!谢谢合作!
能不能给段回调函数的代码? 展开
能不能给段回调函数的代码? 展开
4个回答
展开全部
代码实在懒得写,
Socket有个BeginReceive方法,用这个方法来接收数据,
这个函数有个参数是个委托AsyncCallBack,是个回调函数..
接收到数据会执行你的回调函数
(我是一楼)补充:Socket还有个方法是BeginReveiceFrom是用于UDP协议的.用法与BeginReceive相同,而且UDP不监听的理解是错误的.UDP同样也监听.并不是说只有在TCP中调用了Socket的Listen函数才是监听.BeginReveiceFrom其实已经完成了监听过程
Socket有个BeginReceive方法,用这个方法来接收数据,
这个函数有个参数是个委托AsyncCallBack,是个回调函数..
接收到数据会执行你的回调函数
(我是一楼)补充:Socket还有个方法是BeginReveiceFrom是用于UDP协议的.用法与BeginReceive相同,而且UDP不监听的理解是错误的.UDP同样也监听.并不是说只有在TCP中调用了Socket的Listen函数才是监听.BeginReveiceFrom其实已经完成了监听过程
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Socket类并没有封装到lz说的那种地步。
你必须绑定端口,启动监听,调用Receive方法,然后调用收到数据后的代码。
若采用事件机制,你必须执行上述几步,在Receive方法之后发布事件。
你必须绑定端口,启动监听,调用Receive方法,然后调用收到数据后的代码。
若采用事件机制,你必须执行上述几步,在Receive方法之后发布事件。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1.简单服务器
/*
#include <winsock2.h>
#pragma comment(lib,"WS2_32.lib")
*/
WSADATA wsd;
static UINT port=%%1;
UINT Listen(LPVOID pParam)
{
SOCKET sServer,sClient;
char buf[1024];
int retVal;
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
return -1;//失败
}
sServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==sServer)
{
WSACleanup();
return -1;//创建套接字失败
}
SOCKADDR_IN addrServ;
addrServ.sin_family=AF_INET;
addrServ.sin_port=htons((short)pParam);
addrServ.sin_addr.s_addr=INADDR_ANY;
retVal=bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN));
if(SOCKET_ERROR==retVal)
{
closesocket(sServer);
WSACleanup();
return -1;//绑定套接字失败
}
retVal=listen(sServer,1);
if(SOCKET_ERROR==retVal)
{
closesocket(sServer);
WSACleanup();
return -1;//开始监听失败
}
sockaddr_in addrClient;
int addrClientlen=sizeof(addrClient);
sClient=accept(sServer,(sockaddr FAR*)&addrClient,&addrClientlen);
if(INVALID_SOCKET==sClient)
{
closesocket(sServer);
WSACleanup();
return -1;//开始接受客户端连接失败
}
ZeroMemory(buf,sizeof(buf));
retVal=recv(sClient,buf,sizeof(buf),0);
if(SOCKET_ERROR==retVal)
{
closesocket(sServer);
closesocket(sClient);
WSACleanup();
return -1;//接收数据失败
}
CString %%2(buf);
closesocket(sServer);
closesocket(sClient);
WSACleanup();
return 0;
}
CWinThread *pThread=AfxBeginThread(Listen,&port);
2.简单客户端
/*
#include <winsock2.h>
#pragma comment(lib,"WS2_32.lib")
*/
WSADATA wsd;
SOCKET sHost;
SOCKADDR_IN servAddr;
char buf[1024];
int retVal;
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
return -1;//失败
}
sHost=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==sHost)
{
WSACleanup();
return -1;//创建套接字失败
}
servAddr.sin_family=AF_INET;
servAddr.sin_addr.s_addr=inet_addr(%%3);
servAddr.sin_port=htons((short)%%2);
int nServAddlen=sizeof(servAddr);
retVal=connect(sHost,(LPSOCKADDR)&servAddr,sizeof(servAddr));
if(SOCKET_ERROR==retVal) {
closesocket(sHost);
WSACleanup();
return -1;//连接服务器失败
}
ZeroMemory(buf,sizeof(buf));
strcpy(buf,%%3);
retVal=send(sHost,buf,sizeof(buf),0);
if(SOCKET_ERROR==retVal)
{
closesocket(sHost);
WSACleanup();
return -1;//向服务器发送数据失败
}
closesocket(sHost);
WSACleanup();
3.获得本机IP
/*
#include <winsock2.h>
#pragma comment(lib,"WS2_32.lib")
*/
WSADATA wsd;
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
return -1;//失败
}
char szHostname[100],szHostaddress[200];
if(gethostname(szHostname,sizeof(szHostname))!=SOCKET_ERROR)
{
HOSTENT *pHostEnt=gethostbyname(szHostname);
if(pHostEnt!=NULL){
sprintf(szHostaddress,"%d.%d.%d.%d",
( pHostEnt->h_addr_list[0][0]&0x00ff ),
( pHostEnt->h_addr_list[0][1]&0x00ff ),
( pHostEnt->h_addr_list[0][2]&0x00ff ),
( pHostEnt->h_addr_list[0][3]&0x00ff ));
}
}
else
return;
CString %%1(szHostaddress);
5.点对点通信
/*
#include <winsock2.h>
#pragma comment(lib,"WS2_32.lib")
*/
WSADATA wsd;
SOCKADDR_IN addrServ,addrServ2;
SOCKET sServer,sClient,sHost;
int retVal;
sockaddr_in addrClient;
char buf[1024];
static UINT port=%%2;
BOOL listenerRun=TRUE;
UINT Listen(LPVOID pParam)
{
addrServ.sin_family=AF_INET;
addrServ.sin_port=htons((UINT)pParam);
addrServ.sin_addr.s_addr=INADDR_ANY;
retVal=bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN));
if(SOCKET_ERROR==retVal)
{
closesocket(sServer);
WSACleanup();
return -1;//绑定套接字失败
}
retVal=listen(sServer,1);
if(SOCKET_ERROR==retVal)
{
closesocket(sServer);
WSACleanup();
return -1;//开始监听失败
}
int addrClientlen=sizeof(addrClient);
sClient=accept(sServer,(sockaddr FAR*)&addrClient,&addClientlen);
if(INVALID_SOCKET==sClient)
{
closesocket(sServer);
WSACleanup();
return -1;//接收客户端请求失败
}
while(listenerRun)
{
ZeroMemory(buf,sizeof(buf));
retVal=recv(sClient,buf,sizeof(buf));
if(SOCKET_ERROR==retVal)
{
closesocket(sServer);
closesocket(sClient);
WSACleanup();
return -1;//接收客户端数据失败
}
CString %%4(buf);
}
}
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
return -1;//失败
}
sServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==sServer)
{
WSACleanup();
return -1;//创建套接字失败
}
CWinThread *pThread=AfxBeginThread(Listen,&port);
sHost=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==sHost)
{
WSACleanup();
return -1;//创建套接字失败
}
servAddr2.sin_family=AF_INET;
servAddr2.sin_addr.s_addr=inet_addr(%%1);
servAddr.sin_port=htons((short)%%3);
int nServerAddrlen=sizeof(servAddr2);
retVal=connect(sHost,(LPSOCKADDR)&servAddr2,sizeof(servAddr2));
if(SOCKET_ERROR==retVal)
{
closesocket(sHost);
WSACleanup();
return -1;//连接失败
}
zeroMemory(buf,sizeof(buf));
strcpy(buf,%%5);
retVal=send(sHost,buf,sizeof(buf),0);
if(SOCKET_ERROR==retVal)
{
closesocket(sHost);
WSACleanup();
return -1;//向发送数据失败
}
listenerRun=FALSE;
DWORD dwExitCode;
::GetExitCodeThread(pThread->m_hThread,&dwExitCode);
pThread=null;
closesocket(sServer);
closesocket(sClient);
closesocket(sHost);
WSACleanup();
/*
#include <winsock2.h>
#pragma comment(lib,"WS2_32.lib")
*/
WSADATA wsd;
static UINT port=%%1;
UINT Listen(LPVOID pParam)
{
SOCKET sServer,sClient;
char buf[1024];
int retVal;
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
return -1;//失败
}
sServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==sServer)
{
WSACleanup();
return -1;//创建套接字失败
}
SOCKADDR_IN addrServ;
addrServ.sin_family=AF_INET;
addrServ.sin_port=htons((short)pParam);
addrServ.sin_addr.s_addr=INADDR_ANY;
retVal=bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN));
if(SOCKET_ERROR==retVal)
{
closesocket(sServer);
WSACleanup();
return -1;//绑定套接字失败
}
retVal=listen(sServer,1);
if(SOCKET_ERROR==retVal)
{
closesocket(sServer);
WSACleanup();
return -1;//开始监听失败
}
sockaddr_in addrClient;
int addrClientlen=sizeof(addrClient);
sClient=accept(sServer,(sockaddr FAR*)&addrClient,&addrClientlen);
if(INVALID_SOCKET==sClient)
{
closesocket(sServer);
WSACleanup();
return -1;//开始接受客户端连接失败
}
ZeroMemory(buf,sizeof(buf));
retVal=recv(sClient,buf,sizeof(buf),0);
if(SOCKET_ERROR==retVal)
{
closesocket(sServer);
closesocket(sClient);
WSACleanup();
return -1;//接收数据失败
}
CString %%2(buf);
closesocket(sServer);
closesocket(sClient);
WSACleanup();
return 0;
}
CWinThread *pThread=AfxBeginThread(Listen,&port);
2.简单客户端
/*
#include <winsock2.h>
#pragma comment(lib,"WS2_32.lib")
*/
WSADATA wsd;
SOCKET sHost;
SOCKADDR_IN servAddr;
char buf[1024];
int retVal;
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
return -1;//失败
}
sHost=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==sHost)
{
WSACleanup();
return -1;//创建套接字失败
}
servAddr.sin_family=AF_INET;
servAddr.sin_addr.s_addr=inet_addr(%%3);
servAddr.sin_port=htons((short)%%2);
int nServAddlen=sizeof(servAddr);
retVal=connect(sHost,(LPSOCKADDR)&servAddr,sizeof(servAddr));
if(SOCKET_ERROR==retVal) {
closesocket(sHost);
WSACleanup();
return -1;//连接服务器失败
}
ZeroMemory(buf,sizeof(buf));
strcpy(buf,%%3);
retVal=send(sHost,buf,sizeof(buf),0);
if(SOCKET_ERROR==retVal)
{
closesocket(sHost);
WSACleanup();
return -1;//向服务器发送数据失败
}
closesocket(sHost);
WSACleanup();
3.获得本机IP
/*
#include <winsock2.h>
#pragma comment(lib,"WS2_32.lib")
*/
WSADATA wsd;
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
return -1;//失败
}
char szHostname[100],szHostaddress[200];
if(gethostname(szHostname,sizeof(szHostname))!=SOCKET_ERROR)
{
HOSTENT *pHostEnt=gethostbyname(szHostname);
if(pHostEnt!=NULL){
sprintf(szHostaddress,"%d.%d.%d.%d",
( pHostEnt->h_addr_list[0][0]&0x00ff ),
( pHostEnt->h_addr_list[0][1]&0x00ff ),
( pHostEnt->h_addr_list[0][2]&0x00ff ),
( pHostEnt->h_addr_list[0][3]&0x00ff ));
}
}
else
return;
CString %%1(szHostaddress);
5.点对点通信
/*
#include <winsock2.h>
#pragma comment(lib,"WS2_32.lib")
*/
WSADATA wsd;
SOCKADDR_IN addrServ,addrServ2;
SOCKET sServer,sClient,sHost;
int retVal;
sockaddr_in addrClient;
char buf[1024];
static UINT port=%%2;
BOOL listenerRun=TRUE;
UINT Listen(LPVOID pParam)
{
addrServ.sin_family=AF_INET;
addrServ.sin_port=htons((UINT)pParam);
addrServ.sin_addr.s_addr=INADDR_ANY;
retVal=bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN));
if(SOCKET_ERROR==retVal)
{
closesocket(sServer);
WSACleanup();
return -1;//绑定套接字失败
}
retVal=listen(sServer,1);
if(SOCKET_ERROR==retVal)
{
closesocket(sServer);
WSACleanup();
return -1;//开始监听失败
}
int addrClientlen=sizeof(addrClient);
sClient=accept(sServer,(sockaddr FAR*)&addrClient,&addClientlen);
if(INVALID_SOCKET==sClient)
{
closesocket(sServer);
WSACleanup();
return -1;//接收客户端请求失败
}
while(listenerRun)
{
ZeroMemory(buf,sizeof(buf));
retVal=recv(sClient,buf,sizeof(buf));
if(SOCKET_ERROR==retVal)
{
closesocket(sServer);
closesocket(sClient);
WSACleanup();
return -1;//接收客户端数据失败
}
CString %%4(buf);
}
}
if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)
{
return -1;//失败
}
sServer=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==sServer)
{
WSACleanup();
return -1;//创建套接字失败
}
CWinThread *pThread=AfxBeginThread(Listen,&port);
sHost=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET==sHost)
{
WSACleanup();
return -1;//创建套接字失败
}
servAddr2.sin_family=AF_INET;
servAddr2.sin_addr.s_addr=inet_addr(%%1);
servAddr.sin_port=htons((short)%%3);
int nServerAddrlen=sizeof(servAddr2);
retVal=connect(sHost,(LPSOCKADDR)&servAddr2,sizeof(servAddr2));
if(SOCKET_ERROR==retVal)
{
closesocket(sHost);
WSACleanup();
return -1;//连接失败
}
zeroMemory(buf,sizeof(buf));
strcpy(buf,%%5);
retVal=send(sHost,buf,sizeof(buf),0);
if(SOCKET_ERROR==retVal)
{
closesocket(sHost);
WSACleanup();
return -1;//向发送数据失败
}
listenerRun=FALSE;
DWORD dwExitCode;
::GetExitCodeThread(pThread->m_hThread,&dwExitCode);
pThread=null;
closesocket(sServer);
closesocket(sClient);
closesocket(sHost);
WSACleanup();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1楼正确
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询