新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论高级C/C++编程、代码重构(Refactoring)、极限编程(XP)、泛型编程等话题
    [返回] 计算机科学论坛计算机技术与应用『 C/C++编程思想 』 → 一个菜鸟关于完成端口的问题[求助] 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 2741 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 一个菜鸟关于完成端口的问题[求助] 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     king1994gw 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:0
      积分:52
      门派:XML.ORG.CN
      注册:2007/8/25

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给king1994gw发送一个短消息 把king1994gw加入好友 查看king1994gw的个人资料 搜索king1994gw在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看king1994gw的博客楼主
    发贴心情 一个菜鸟关于完成端口的问题[求助]

    我是用delphi编的 由于网上只有收数据的模型 我想把它该成发送数据的 但是却不能发送 想请问各位高手哪里出错了 谢谢
    unit Unit2;

    interface
    uses Windows, unit1, WinSock, Sysutils;
    const
      port=5150;
      data_bufsize=8192;
    type
      lpvoid=pointer;
      lpper_io_operation_data=^per_io_operation_data;
      per_io_operation_data=packed record
        overlapped:overlapped;
        databuf:twsabuf;
        buffer:array[0..data_bufsize]of char;
        bytessend:dword;
        bytesrec:dword;
      end;
      lpper_handle_data=^per_handle_data;
      per_handle_data=packed record
        socket:tsocket;
      end;

      procedure main;

    implementation

    function serverworkerthread(completionportid:lpvoid):dword;stdcall;forward;

    procedure printf(fmt:string;num:integer);
    begin
      writeln(format(fmt,[num]));
    end;
    procedure main;
    var
      internetaddr:sockaddr_in;
      listen:tsocket;
      accept:tsocket;
      completionport:thandle;
      systeminfo:system_info;
      perhandledata:lpper_handle_data;
      periodata:lpper_io_operation_data;
      i:integer;
      recvbytes,sendbytes:dword;
      flags:dword;
      threadid:dword;
      wsadata:twsadata;
      ret:dword;
      threadhandle:thandle;
    begin
      ret:=wsastartup($0202,wsadata);
      if (ret<>0) then
      begin
        printf('wsas %d',ret);
        exit;
      end;
      completionport:=createiocompletionPort(INVALID_HANDLE_VALUE,0,0,0);
      if (CompletionPort=0) then
      begin
        printf('createioerror:%d',getlasterror());
        exit;
      end;
      getsysteminfo(systeminfo);
      for i:=0 to systeminfo.dwNumberOfProcessors*2-1 do
      begin
        threadhandle:=createthread(nil,0,@serverworkerthread,pointer(completionport),0,threadid);
        if (threadhandle=0) then
        begin
          printf('c',getlasterror());
          exit;
        end;
        closehandle(threadhandle);
      end;
      listen:=wsasocket(af_inet,sock_stream,0,nil,0,wsa_flag_overlapped);
      if (listen=invalid_socket) then
      begin
        printf('wsa',wsagetlasterror());
        exit;
      end;
      internetaddr.sin_family:=af_inet;
      internetaddr.sin_addr.S_addr:=inet_addr(pchar('192.168.0.2'));
      internetaddr.sin_port:=htons(port);
      perhandledata:=lpper_handle_data(globalalloc(gptr,sizeof(per_handle_data)));
      if (perhandledata=nil) then
      begin
        printf('glo',wsagetlasterror());
        exit;
      end;
      printf('socket numble%d',accept);
      perhandledata.socket:=listen;
      if (createiocompletionport(listen,completionport,dword(perhandledata),0)=0) then
      begin
        printf('createiocp%d',wsagetlasterror());
        exit;
      end;
      periodata:=lpper_io_operation_data(globalalloc(gptr,sizeof(per_io_operation_data)));
      if (periodata=nil) then
      begin
        printf('glo%d',wsagetlasterror());
        exit;
      end;
        zeromemory(@periodata.overlapped,sizeof(overlapped));
        periodata.bytessend:=0;
        periodata.bytesrec:=0;
        periodata.databuf.len:=data_bufsize;
        periodata.buffer:='wudi';
        periodata.databuf.buf:=@periodata.buffer;
        flags:=0;
        if (wsasend(listen,@(periodata.databuf),1,@sendbytes,0,@(periodata.overlapped),nil)=socket_error) then
        begin
          if (wsagetlasterror<>error_io_pending) then
          begin
            printf('wsarecvfailed%d',wsagetlasterror());
            exit;
          end
        end;
      end;

    function serverworkerthread(completionportid:lpvoid):dword;stdcall;
    var
       CompletionPort: THANDLE;
       BytesTransferred: DWORD ;
       PerHandleData: LPPER_HANDLE_DATA ;
       PerIoData: LPPER_IO_OPERATION_DATA ;
       SendBytes, RecvBytes: DWORD;
       Flags: DWORD ;
    begin
      completionport:=thandle(completionportid);
      result:=0;
      while(true) do
      begin
        if (getqueuedcompletionstatus(completionport,bytestransferred,dword(perhandledata),poverlapped(periodata),infinite)=false) then
        begin
          printf('get%d',getlasterror());
          exit;
        end;
        if (bytestransferred=0) then
        begin
          printf('closingsocket%d\',perhandledata.socket);
          if (closesocket(perhandledata.socket)=socket_error) then
          begin
            printf('closesocketerror%d',wsagetlasterror());
            exit;
          end;
          globalfree(dword(perhandledata));
          globalfree(dword(periodata));
          continue;
        end;
        if (periodata.bytesrec=0) and (periodata.bytessend=0) then
        begin
          PerIoData.BytesREC:= BytesTransferred;
          periodata.bytessend:= 0;
        end
        else
        begin
          periodata.bytesrec:=0;
          periodata.bytessend:=bytestransferred;
        end;
        if (periodata.bytesrec>periodata.bytessend) then
        begin
          zeromemory(@(periodata.overlapped),sizeof(overlapped));
          periodata.databuf.buf:= periodata.buffer+periodata.bytessend;
          periodata.databuf.len:= periodata.bytesrec-periodata.bytessend;
          printf(periodata.buffer,periodata.databuf.len);
          if (wsasend(perhandledata.socket,@(periodata.databuf),1,@sendbytes,0,@(periodata.overlapped),nil)=socket_error) then
          begin
            if (wsagetlasterror()<>error_io_pending) then
            begin
              printf('wsasend%d',wsagetlasterror());
              exit
            end;
          end;
        end
        else
        begin
          periodata.bytesrec:= 0;
          flags:= 0;
          zeromemory(@(periodata.overlapped),sizeof(overlapped));
          periodata.databuf.len:= data_bufsize;
          periodata.databuf.buf:= @periodata.buffer;
          if (wsasend(perhandledata.socket,@(periodata.databuf),1,@sendbytes,0,@(periodata.overlapped),nil)=socket_error) then
          begin
            if (wsagetlasterror()<>error_io_pending) then
            begin
              if (wsagetlasterror()<>error_io_pending) then
              begin
                printf('wsa%d',wsagetlasterror());
                exit;
              end;
            end;
          end;
        end;
      end;
    end;
    end.


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/8/25 17:49:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 C/C++编程思想 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/11/26 13:02:04

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    78.125ms