哪位老兄能给提供一下CAsyncSocket类关于服务器编程的例子或思路(最好SDK的)?

w46630043 2003-04-28 10:14:34
研究CAsyncSocket类的网络编程数日,始终不明其理,哪位老兄能给指点一下,最好能有个深一点的例子(能接受多连接),谢谢啊;
...全文
91 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
w46630043 2003-04-28
  • 打赏
  • 举报
回复
这我也知道,可总弄不好,最好能给个例子研究研究一下
*亮点* 2003-04-28
  • 打赏
  • 举报
回复
原理都是一样的,只是用的函数不一样而已,要接收多连接就要使用多线程
leon7909 2003-04-28
  • 打赏
  • 举报
回复
SDK里又哪會有CAsyncSocket,<<Windows网絡編程>>里有很多這樣子一服務器對多client端的例子啊!抄一段代碼給你:
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>

#define PORT 5150
#define DATA_BUFSIZE 8192

typedef struct _SOCKET_INFORMATION {
CHAR Buffer[DATA_BUFSIZE];
WSABUF DataBuf;
SOCKET Socket;
DWORD BytesSEND;
DWORD BytesRECV;
} SOCKET_INFORMATION, * LPSOCKET_INFORMATION;

BOOL CreateSocketInformation(SOCKET s);
void FreeSocketInformation(DWORD Event);

DWORD EventTotal = 0;
WSAEVENT EventArray[WSA_MAXIMUM_WAIT_EVENTS];
LPSOCKET_INFORMATION SocketArray[WSA_MAXIMUM_WAIT_EVENTS];

void main(void)
{
SOCKET Listen;
SOCKET Accept;
SOCKADDR_IN InternetAddr;
DWORD Event;
WSANETWORKEVENTS NetworkEvents;
WSADATA wsaData;
DWORD Ret;
DWORD Flags;
DWORD RecvBytes;
DWORD SendBytes;

if ((Ret = WSAStartup(0x0202, &wsaData)) != 0)
{
printf("WSAStartup() failed with error %d\n", Ret);
return;
}

if ((Listen = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
printf("socket() failed with error %d\n", WSAGetLastError());
return;
}

CreateSocketInformation(Listen);

if (WSAEventSelect(Listen, EventArray[EventTotal - 1], FD_ACCEPT|FD_CLOSE) == SOCKET_ERROR)
{
printf("WSAEventSelect() failed with error %d\n", WSAGetLastError());
return;
}

InternetAddr.sin_family = AF_INET;
InternetAddr.sin_addr.s_addr = htonl(INADDR_ANY);
InternetAddr.sin_port = htons(PORT);

if (bind(Listen, (PSOCKADDR) &InternetAddr, sizeof(InternetAddr)) == SOCKET_ERROR)
{
printf("bind() failed with error %d\n", WSAGetLastError());
return;
}

if (listen(Listen, 5))
{
printf("listen() failed with error %d\n", WSAGetLastError());
return;
}


while(TRUE)
{
// Wait for one of the sockets to receive I/O notification and
if ((Event = WSAWaitForMultipleEvents(EventTotal, EventArray, FALSE,
WSA_INFINITE, FALSE)) == WSA_WAIT_FAILED)
{
printf("WSAWaitForMultipleEvents failed with error %d\n", WSAGetLastError());
return;
}


if (WSAEnumNetworkEvents(SocketArray[Event - WSA_WAIT_EVENT_0]->Socket, EventArray[Event -
WSA_WAIT_EVENT_0], &NetworkEvents) == SOCKET_ERROR)
{
printf("WSAEnumNetworkEvents failed with error %d\n", WSAGetLastError());
return;
}


if (NetworkEvents.lNetworkEvents & FD_ACCEPT)
{
if (NetworkEvents.iErrorCode[FD_ACCEPT_BIT] != 0)
{
printf("FD_ACCEPT failed with error %d\n", NetworkEvents.iErrorCode[FD_ACCEPT_BIT]);
break;
}

if ((Accept = accept(SocketArray[Event - WSA_WAIT_EVENT_0]->Socket, NULL, NULL)) == INVALID_SOCKET)
{
printf("accept() failed with error %d\n", WSAGetLastError());
break;
}

if (EventTotal > WSA_MAXIMUM_WAIT_EVENTS)
{
printf("Too many connections - closing socket.\n");
closesocket(Accept);
break;
}

CreateSocketInformation(Accept);

if (WSAEventSelect(Accept, EventArray[EventTotal - 1], FD_READ|FD_WRITE|FD_CLOSE) == SOCKET_ERROR)
{
printf("WSAEventSelect() failed with error %d\n", WSAGetLastError());
return;
}

printf("Socket %d connected\n", Accept);
}


// Try to read and write data to and from the data buffer if read and write events occur.

if (NetworkEvents.lNetworkEvents & FD_READ ||
NetworkEvents.lNetworkEvents & FD_WRITE)
{
if (NetworkEvents.lNetworkEvents & FD_READ &&
NetworkEvents.iErrorCode[FD_READ_BIT] != 0)
{
printf("FD_READ failed with error %d\n", NetworkEvents.iErrorCode[FD_READ_BIT]);
break;
}

if (NetworkEvents.lNetworkEvents & FD_WRITE &&
NetworkEvents.iErrorCode[FD_WRITE_BIT] != 0)
{
printf("FD_WRITE failed with error %d\n", NetworkEvents.iErrorCode[FD_WRITE_BIT]);
break;
}

LPSOCKET_INFORMATION SocketInfo = SocketArray[Event - WSA_WAIT_EVENT_0];

// Read data only if the receive buffer is empty.

if (SocketInfo->BytesRECV == 0)
{
SocketInfo->DataBuf.buf = SocketInfo->Buffer;
SocketInfo->DataBuf.len = DATA_BUFSIZE;

Flags = 0;
if (WSARecv(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &RecvBytes,
&Flags, NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("WSARecv() failed with error %d\n", WSAGetLastError());
FreeSocketInformation(Event - WSA_WAIT_EVENT_0);
return;
}
}
else
{
SocketInfo->BytesRECV = RecvBytes;
}
}

// Write buffer data if it is available.

if (SocketInfo->BytesRECV > SocketInfo->BytesSEND)
{
SocketInfo->DataBuf.buf = SocketInfo->Buffer + SocketInfo->BytesSEND;
SocketInfo->DataBuf.len = SocketInfo->BytesRECV - SocketInfo->BytesSEND;

if (WSASend(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &SendBytes, 0,
NULL, NULL) == SOCKET_ERROR)
{
if (WSAGetLastError() != WSAEWOULDBLOCK)
{
printf("WSASend() failed with error %d\n", WSAGetLastError());
FreeSocketInformation(Event - WSA_WAIT_EVENT_0);
return;
}

// A WSAEWOULDBLOCK error has occured. An FD_WRITE event will be posted
// when more buffer space becomes available
}
else
{
SocketInfo->BytesSEND += SendBytes;

if (SocketInfo->BytesSEND == SocketInfo->BytesRECV)
{
SocketInfo->BytesSEND = 0;
SocketInfo->BytesRECV = 0;
}
}
}
}

if (NetworkEvents.lNetworkEvents & FD_CLOSE)
{
if (NetworkEvents.iErrorCode[FD_CLOSE_BIT] != 0)
{
printf("FD_CLOSE failed with error %d\n", NetworkEvents.iErrorCode[FD_CLOSE_BIT]);
break;
}

printf("Closing socket information %d\n", SocketArray[Event - WSA_WAIT_EVENT_0]->Socket);

FreeSocketInformation(Event - WSA_WAIT_EVENT_0);
}
}
return;
}


BOOL CreateSocketInformation(SOCKET s)
{
LPSOCKET_INFORMATION SI;

if ((EventArray[EventTotal] = WSACreateEvent()) == WSA_INVALID_EVENT)
{
printf("WSACreateEvent() failed with error %d\n", WSAGetLastError());
return FALSE;
}

if ((SI = (LPSOCKET_INFORMATION) GlobalAlloc(GPTR,
sizeof(SOCKET_INFORMATION))) == NULL)
{
printf("GlobalAlloc() failed with error %d\n", GetLastError());
return FALSE;
}

// Prepare SocketInfo structure for use.

SI->Socket = s;
SI->BytesSEND = 0;
SI->BytesRECV = 0;

SocketArray[EventTotal] = SI;

EventTotal++;

return(TRUE);
}


void FreeSocketInformation(DWORD Event)
{
LPSOCKET_INFORMATION SI = SocketArray[Event];
DWORD i;

closesocket(SI->Socket);

GlobalFree(SI);

WSACloseEvent(EventArray[Event]);

// Squash the socket and event arrays

for (i = Event; i < EventTotal; i++)
{
EventArray[i] = EventArray[i + 1];
SocketArray[i] = SocketArray[i + 1];
}

EventTotal--;
}

w46630043 2003-04-28
  • 打赏
  • 举报
回复
大虾们是不是嫌分太少,如回答满意,再加100分或更多都可以,如不便公开源码,我的邮箱:zi_lang@163.com,谢了
yydyqy 2003-04-28
  • 打赏
  • 举报
回复
up
loopyifly 2003-04-28
  • 打赏
  • 举报
回复
up
dawner 2003-04-28
  • 打赏
  • 举报
回复
我也遇到这个问题,现在也不能解决,一客户机对一服务器还好说,可多客户机对一服务器弄起来就难,有例子的朋友不妨也给我一份,谢谢!dawner@yeah.net
内容概要:本文围绕可变桨叶四旋翼无人机的规范控制与点对点运动模拟展开,重点研究优化推力分配策略在翻转动作中的应用与性能比较。通过Matlab代码实现,构建了四旋翼动力学模型,并设计了多种控制算法以实现精确的姿态调整与轨迹跟踪。研究对比了不同推力分配方案在执行高机动性翻转动作时的稳定性、能耗效率与响应速度,旨在提升无人机在复杂飞行任务中的动态性能与控制精度。该仿真研究为无人机飞控系统的设计与优化提供了理论依据和技术支持。; 适合人群:具备一定自动控制理论基础和Matlab编程能力,从事无人机控制、飞行器动力学或机器人系统研究的科研人员及研究生。; 使用场景及目标:① 实现四旋翼无人机在三维空间中的精确点对点运动控制;② 对比分析不同推力分配策略在执行翻转等高难度动作时的控制效果与能耗表现,优化飞行性能;③ 为无人机自主飞行、特技飞行及复杂环境下的机动控制提供算法验证平台。; 阅读建议:此资源以Matlab仿真为核心,建议读者结合相关控制理论知识,深入理解代码实现细节,重点关注动力学建模、控制律设计与推力分配模块。在学习过程中,应动手调试参数,复现文中翻转动作的仿真结果,并尝试拓展至其他复杂飞行任务,以加深对无人机控制机理的理解。

18,357

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧