您的位置:程序门 -> vc/mfc -> 网络编程



请教在异步模式下wsasend的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


请教在异步模式下wsasend的问题[已结贴,结贴人:lasylasypig]
发表于:2007-01-15 07:33:37 楼主
问题是这样的:我的服务器要向客户端发送一个自定义的结构体,例如:
struct   mystruct
{
    int   id;
    char   name[30];
};
在异步模式下(mfc),在服务器端我调用wsasend(...)来发送这个结构体:
          mystruct   stru;
          stru.id   =   10;
          stru.name   =   "xxx ";
         
          wsabuf   data;
          int   len   =   sizeof(mystruct);          
          data.len   =   len;
          data.buf   =(char*)&stru;
最后调用   wsasend发送。

在客户端,使用wsarecv接收。可是在客户端接收的数据是错误的,还出现了内存访问错误的情况。而在win32控制台环境下,我使用同步方式编写的程序就能发送成功。请问这是什么原因啊?怎么解决这个问题?请各位大侠不吝赐教。谢谢!
发表于:2007-01-16 05:12:171楼 得分:0
怎么没有人答啊?我自顶
发表于:2007-01-16 08:06:422楼 得分:20
mystruct   stru;
是个局部变量,在真正发送数据时stru已经析构,data.buf(&stru),   已经是个无效的地址了。
发表于:2007-01-16 08:28:293楼 得分:0
stru.name   =   "xxx ";有问题啊,这样能运行吗??
strcpy
发表于:2007-01-16 10:11:194楼 得分:0
stru.name   =   "xxx ";
---------
不知道你程序怎么编译通过的。。。
发表于:2007-01-22 12:52:525楼 得分:40
没看见你的重叠i/o,异步发送是用重叠i/o来实现的。

mystruct   stru;
是个局部变量,在真正发送数据时stru已经析构,data.buf(&stru),   已经是个无效的地址了。

type
    piobuffer   =   ^tiobuffer;
    tiobuffer   =   packed   record
        ol:   twsaoverlapped;
        sclient:   tsocket;
        noperation:   byte;
        buf:   pbyte;
        nlen:   integer;
        nseq:   integer;
        next:   piobuffer;
    end;

function   ttcpsock.postsend(pcontext:   piocontext;   pbuffer:   piobuffer):   bool;
var
dwbytes,dwflags:   dword;
buf:   wsabuf;
begin
    result   :=   true;
    dwflags   :=   0;
    pbuffer^.noperation   :=   op_write;
    buf.len   :=   pbuffer^.nlen;
    buf.buf   :=   pchar(pbuffer^.buf);
    if((wsasend(pcontext^.sconnect,@buf,1,dwbytes,dwflags,@pbuffer^.ol,nil)   <>   no_error))   then
    begin
        if(wsagetlasterror   <>   wsa_io_pending)   then
        begin
            outpshow(true,pchar( '投递wsasend失败! '));
            releasebuffer(pbuffer);
            result   :=   false;
            exit;
        end;
    end;
    inc(pcontext^.noutsend);
end;


快速检索

最新资讯
热门点击