古詩詞大全網 - 成語大全 - 關於CSocket的三個實際問題!

關於CSocket的三個實際問題!

剛才看錯了MSDN上的解釋是這樣的

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".

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.

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.

----------------------

sendto

The sendto function sends data to a specific destination.

int sendto(

SOCKET s,

const char* buf,

int len,

int flags,

const struct sockaddr* to,

int tolen

);

Parameters

s

[in] Descriptor identifying a (possibly connected) socket.

buf

[in] Buffer containing the data to be transmitted.

len

[in] Length of the data in buf, in bytes.

flags

[in] Indicator specifying the way in which the call is made.

to

[in] Optional pointer to a sockaddr structure that contains the address of the target socket.

tolen

[in] Size of the address in to, in bytes.

Return Values

If no error occurs, sendto returns the total number of bytes sent, which can be less than the number indicated by len. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

Error code Meaning

WSANOTINITIALISED A successful WSAStartup call must occur before using this function.

WSAENETDOWN The network subsystem has failed.

WSAEACCES The requested address is a broadcast address, but the appropriate flag was not set. Call setsockopt with the SO_BROADCAST parameter to allow the use of the broadcast address.

WSAEINVAL An unknown flag was specified, or MSG_OOB was specified for a socket with SO_OOBINLINE enabled.

WSAEINTR A blocking Windows Sockets 1.1 call was canceled through WSACancelBlockingCall.

WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.

WSAEFAULT The buf or to parameters are not part of the user address space, or the tolen parameter is too small.

WSAENETRESET The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress.

WSAENOBUFS No buffer space is available.

WSAENOTCONN The socket is not connected (connection-oriented sockets only).

WSAENOTSOCK The descriptor is not a socket.

WSAEOPNOTSUPP MSG_OOB was specified, but the socket is not stream-style such as type SOCK_STREAM, OOB data is not supported in the communication domain associated with this socket, or the socket is unidirectional and supports only receive operations.

WSAESHUTDOWN The socket has been shut down; it is not possible to sendto on a socket after shutdown has been invoked with how set to SD_SEND or SD_BOTH.

WSAEWOULDBLOCK The socket is marked as nonblocking and the requested operation would block.

WSAEMSGSIZE The socket is message oriented, and the message is larger than the maximum supported by the underlying transport.

WSAEHOSTUNREACH The remote host cannot be reached from this host at this time.

WSAECONNABORTED The virtual circuit was terminated due to a time-out or other failure. The application should close the socket as it is no longer usable.

WSAECONNRESET The virtual circuit was reset by the remote side executing a hard or abortive close. For UPD sockets, the remote host was unable to deliver a previously sent UDP datagram and responded with a "Port Unreachable" ICMP packet. The application should close the socket as it is no longer usable.

WSAEADDRNOTAVAIL The remote address is not a valid address, for example, ADDR_ANY.

WSAEAFNOSUPPORT Addresses in the specified family cannot be used with this socket.

WSAEDESTADDRREQ A destination address is required.

WSAENETUNREACH The network cannot be reached from this host at this time.

WSAEHOSTUNREACH A socket operation was attempted to an unreachable host.

WSAETIMEDOUT The connection has been dropped, because of a network failure or because the system on the other end went down without notice.

Remarks

The sendto function is used to write outgoing data on a socket. For message-oriented sockets, care must be taken not to exceed the maximum packet size of the underlying subnets, which can be obtained by using getsockopt to retrieve the value of socket option SO_MAX_MSG_SIZE. If the data is too long to pass atomically through the underlying protocol, the error WSAEMSGSIZE is returned and no data is transmitted.

The to parameter can be any valid address in the socket's address family, including a broadcast or any multicast address. To send to a broadcast address, an application must have used setsockopt with SO_BROADCAST enabled. Otherwise, sendto will fail with the error code WSAEACCES. For TCP/IP, an application can send to any multicast address (without becoming a group member).

Note If a socket is opened, a setsockopt call is made, and then a sendto call is made, Windows Sockets performs an implicit bind function call.

If the socket is unbound, unique values are assigned to the local association by the system, and the socket is then marked as bound. An application can use getsockname to determine the local socket name in this case.

The successful completion of a sendto does not indicate that the data was successfully delivered.

The sendto function is normally used on a connectionless socket to send a datagram to a specific peer socket identified by the to parameter. Even if the connectionless socket has been previously connected to a specific address, the to parameter overrides the destination address for that particular datagram only. On a connection-oriented socket, the to and tolen parameters are ignored, making sendto equivalent to send.

這些妳可以查看MSDN,上面都有很詳細的解釋的。