HackerJLY's Blog

我不是一个真正的程序员!没有编写过一个真正的程序,整天在写“伪代码”!我只是一个打杂的!- 2008.02.29_0:05

jackie JiID:HackerJLY
10962次访问,排名9890(-4)好友59人,关注者63
C,C++,API,MFC,VB6,Win32_ASM,C#,SQL,JavaScript,HTML
HackerJLY的文章
原创 26 篇
翻译 0 篇
转载 66 篇
评论 2 篇
Jackie Ji的公告
初级程序员用VB !
高级程序员用Delphi !
真正的程序员用C++ !

web 
page counters
Leave a message to me with QQ?
Google


最近评论
moomee:初级程序员用VB !
高级程序员用Delphi !
真正的程序员用C++ !


我最开始学VB
后来因为实习单位所以就学Java
经过了一段时间迷茫后,我决定今后学习C++和汇编。

帮你补充一下,真正的程序员用的是C++和汇编。
PrideRock:Good
文章分类
收藏
    相册
    My Other Blog
    HackerJLY - CSDN 下载频道
    HackerJLY's Blog - BlogSpot.com
    HackerJLY's Blog - CNBlogs.com
    HackerJLY's Blog - CNXHacker.net
    HackerJLY's Blog - HackBase.com - 黑基
    HackerJLY's Blog - MSN(RSS)
    HackerJLY's Google Page
    HackerJLY's Source - CSDN
    HackerJLY's 个人空间 - CSDN
    HackerJLY's 网摘 - CSDN
    Study(学习)
    Tree - MSDN Library
    Tree - MSDN 技术资料库
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    转载 Windows Sockets: Using Class CAsyncSocket收藏

    新一篇: Windows Sockets: Socket Notifications | 旧一篇: 透明窗体实现 - C++ & MFC & API

    Windows Sockets: Using Class CAsyncSocket

    HomeOverviewHow Do ISample

    This article explains how to use class CAsyncSocket. Be aware that this class encapsulates the Windows Sockets API at a very low level. CAsyncSocket is for use by programmers who know network communications in detail but want the convenience of callbacks for notification of network events. Based on this assumption, this article provides only basic instruction. You should probably consider using CAsyncSocket if you want Windows Sockets’ ease of dealing with multiple network protocols in an MFC application but don’t want to sacrifice flexibility. You might also feel that you can get better efficiency by programming the communications more directly yourself than you could using the more general alternative model of class CSocket.

    CAsyncSocket is documented in the Class Library Reference. Visual C++ also supplies the Windows Sockets specification, located in the Win32 SDK. The details are left to you. Visual C++ does not supply a sample application for CAsyncSocket.

    If you aren’t highly knowledgeable about network communications and want a simple solution that shields you from most of the details, use class CSocket with a CArchive object. See the article Windows Sockets: Using Sockets with Archives.

    This article covers:

    Creating and Using a CAsyncSocket Object

    To use CAsyncSocket

    1. Construct a CAsyncSocket object and use the object to create the underlying SOCKET handle.

      Creation of a socket follows the MFC pattern of two-stage construction.

      For example:

      CAsyncSocket sock;
      sock.Create( );    // Use the default parameters
      

      -or-

      CAsyncSocket* pSocket = new CAsyncSocket;
      int nPort = 27;
      pSocket->Create( nPort, SOCK_DGRAM );
      

      The first constructor above creates a CAsyncSocket object on the stack. The second constructor creates a CAsyncSocket on the heap. The first Create call above uses the default parameters to create a stream socket. The second Create call creates a datagram socket with a specified port and address. (You can use either Create version with either construction method.)

      The parameters to Create are:

      • A “port”: a short integer.

        For a server socket, you must specify a port. For a client socket, you’ll typically accept the default value for this parameter, which lets Windows Sockets select a port.

      • A socket type: SOCK_STREAM (the default) or SOCK_DGRAM.
      • A socket “address,” such as “ftp.microsoft.com” or “128.56.22.8”.

        This is your Internet Protocol (IP) address on the network. You’ll probably always rely on the default value for this parameter.

      The terms “port” and “socket address” are explained in the article Windows Sockets: Ports and Socket Addresses.

    2. If the socket is a client, connect the socket object to a server socket, using CAsyncSocket::Connect.

      -or-

      If the socket is a server, set the socket to begin listening (with CAsyncSocket::Listen) for connect attempts from a client. Upon receiving a connection request, accept it with CAsyncSocket::Accept.

      After accepting a connection, you can perform such tasks as validating passwords.

      Note   The Accept member function takes a reference to a new, empty CSocket object as its parameter. You must construct this object before you call Accept. Keep in mind that if this socket object goes out of scope, the connection closes. Do not call Create for this new socket object. For an example, see the article Windows Sockets: Sequence of Operations.

    3. Carry out communications with other sockets by calling the CAsyncSocket object’s member functions that encapsulate the Windows Sockets API functions.

      See the Windows Sockets specification and class CAsyncSocket in the Class Library Reference.

    4. Destroy the CAsyncSocket object.

      If you created the socket object on the stack, its destructor is called when the containing function goes out of scope. If you created the socket object on the heap, using the new operator, you are responsible for using the delete operator to destroy the object.

      The destructor calls the object’s Close member function before destroying the object.

    For an example of this sequence in code (actually for a CSocket object), see the article Windows Sockets: Sequence of Operations.

    Your Responsibilities with CAsyncSocket

    When you create an object of class CAsyncSocket, the object encapsulates a Windows SOCKET handle and supplies operations on that handle. When you use CAsyncSocket, you must deal with all of the issues you might face if using the API directly. For example:

    • “Blocking” scenarios
    • Byte order differences between the sending and receiving machines
    • Converting between Unicode and multibyte character set (MBCS) strings

    For definitions of these terms and additional information, see the articles Windows Sockets: Blocking, Windows Sockets: Byte Ordering, Windows Sockets: Converting Strings.

    Despite these issues, class CAsycnSocket may be the right choice for you if your application requires all the flexibility and control you can get. If not, you should consider using class CSocket instead. CSocket hides a lot of detail from you: it pumps Windows messages during blocking calls and gives you access to CArchive, which manages byte order differences and string conversion for you.

    What do you want to know more about?

    发表于 @ 2008年06月14日 03:02:00|评论(loading...)|编辑|收藏

    新一篇: Windows Sockets: Socket Notifications | 旧一篇: 透明窗体实现 - C++ & MFC & API

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © Jackie Ji