共用方式為


CSocket::Create

Call the Create member function after constructing a socket object to create the Windows socket and attach it.

BOOL Create(
   UINT nSocketPort = 0,
   int nSocketType = SOCK_STREAM,
   LPCTSTR lpszSocketAddress = NULL 
);

Parameters

  • nSocketPort
    A particular port to be used with the socket, or 0 if you want MFC to select a port.

  • nSocketType
    SOCK_STREAM or SOCK_DGRAM.

  • lpszSocketAddress
    A pointer to a string containing the network address of the connected socket, a dotted number such as "128.56.22.8". Passing the NULL string for this parameter indicates the CSocket instance should listen for client activity on all network interfaces.

Return Value

Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError.

Remarks

Create then calls Bind to bind the socket to the specified address. The following socket types are supported:

  • SOCK_STREAM   Provides sequenced, reliable, two-way, connection-based byte streams. Uses Transmission Control Protocol (TCP) for the Internet address family.

  • SOCK_DGRAM   Supports datagrams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. Uses User Datagram Protocol (UDP) for the Internet address family. To use this option, you must not use the socket with a CArchive object.

    注意

    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 more information about stream and datagram sockets, see the articles Windows Sockets: Background, Windows Sockets: Ports and Socket Addresses, and Windows Sockets: Using Sockets with Archives.

Requirements

Header: afxsock.h

See Also

Reference

CSocket Class

Hierarchy Chart

CAsyncSocket::Create

CAsyncSocket::Bind

Other Resources

CSocket Members