scoket

多套用在軟體開發中兩個應用程式之間的通訊,俗稱Socket通訊。

socket,函式,

socket

用於在兩個的應用程式之間相互通信,socket是屬於TCP\IP的上一層。最早出現在UNIX系統中,是UNIX系統主要的信息傳遞方式。在WINDOWS系統中,SOCKET稱為WINSOCK。兩個基本概念:客戶方和服務方。當兩個套用之間需要採用SOCKET通信時,首先需要在兩個套用之間(可能位於同一台機器,也可能位於不同的機器)建立SOCKET連線,發起呼叫連線請求的一方為客戶方,接受呼叫連線請求的一方成為服務方。客戶方和服務方是相對的,同一個套用可以是客戶方,也可以是服務方。在客戶方呼叫連線請求之前,它必須知道服務方在哪裡。所以需要知道服務方所在機器的IP位址或機器名稱,如果客戶方和服務方事前有一個約定就好了,這個約定就是PORT(連線埠號)。也就是說,客戶方可以通過服務方所在機器的IP位址或機器名稱和連線埠號唯一的確定方式來呼叫服務方。在客戶方呼叫之前,服務方必須處於偵聽狀態,偵聽是否有客戶要求建立連線。一旦接到連線請求,服務方可以根據情況建立或拒絕連線。連線方式有兩種,同步方式(Blocking)和異步方式(noBlocking)。客戶方傳送的訊息可以是文本,也可以是二進制信息流。當客戶方的訊息到達服務方連線埠時,會自動觸發一個事件(event),服務方只要接管該事件,就可以接受來自客戶方的訊息了。

函式

The socket function creates a socket that is bound to a specific service provider.
SOCKET socket(
int af,
int type,
int protocol
);
Parameters
afAddress family specification.
type
Type specification for the new socket.
The following are the only two type specifications supported for WindowsSockets 1.1: Type Explanation
SOCK_STREAM Provides sequenced, reliable, two-way, connection-based byte streams with an OOB data transmission mechanism. Uses TCPfor the Internet address family.
SOCK_DGRAM Supports datagrams, which are connectionless, unreliable buffers of a fixed(typically small) maximum length. Uses UDPfor the Internet address family.
In Windows Sockets 2, many new socket types will be introduced and no longer need to bespecified, since an application can dynamically discover the attributes of each available transport protocol through the WSAEnumProtocols function. Socket type definitions appear in Winsock2.h, which will be periodically updated as new socket types, address families, and protocols are defined.
protocol
Protocol to be used with the socket that is specific to the indicated address family.
Return Values
If no error occurs, socket returns a descriptor referencing the new socket. Otherwise, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.
Error code Meaning
WSANOTINITIALISED A successful WSAStartupcall must occur before using this function.
WSAENETDOWN The network subsystem or the associated service provider has failed.
WSAEAFNOSUPPORT The specified address family is not supported.
WSAEINPROGRESS A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
WSAEMFILE No moresocket descriptors are available.
WSAENOBUFS No buffer space is available. The socket cannot be created.
WSAEPROTONOSUPPORT The specified protocol is not supported.
WSAEPROTOTYPE The specified protocol is the wrong type for this socket.
WSAESOCKTNOSUPPORT The specified socket type is not supported in this address family.
Remarks
The socket function causes a socket descriptor and any related resources to be allocated and bound to a specific transport-service provider. Windows Sockets will utilize the first available service provider that supports the requested combination of address family, socket type and protocol parameters. The socket that is created will have the overlapped attribute as a default. For Microsoft operating systems, the Microsoft-specific socket option, SO_OPENTYPE, defined in Mswsock.h can affect this default. See Microsoft-specific documentation for a detailed description of SO_OPENTYPE.
Sockets without the overlapped attribute can be created by using WSASocket. All functions that allow overlapped operation (WSASend, WSARecv,WSASendTo, WSARecvFrom, and WSAIoctl) also support nonoverlapped usage on an overlapped socket if the values for parameters related to overlapped operation are NULL.
Whenselecting a protocol and its supporting service provider this procedurewill only choose a base protocol or a protocol chain, not a protocol layer by itself. Unchained protocol layers are not considered to have partial matches on type or af either. That is, they do not lead to an error code of WSAEAFNOSUPPORT or WSAEPROTONOSUPPORT if no suitable protocol is found.
Important The manifest constant AF_UNSPEC continues to be defined in the header file but its use is strongly discouraged, as this can cause ambiguity in interpreting the value of the protocol parameter.
Connection-oriented sockets such as SOCK_STREAM provide full-duplex connections, and must be in a connectedstate before any data can be sent or received on it. A connection to another socket is created with a connect call. Onceconnected, data can be transferred using send and recv calls. When a session has been completed, a closesocket must be performed.
The communications protocols used to implementa reliable, connection-oriented socket ensure that data is not lost or duplicated. If data for which the peer protocol has buffer space cannot be successfully transmitted within a reasonable length of time, the connection is considered broken and subsequent calls will fail with the error code set to WSAETIMEDOUT.
Connectionless, message-oriented sockets allow sending and receiving of datagrams to and from arbitrary peersusing sendto and recvfrom. If such a socket is connected to a specific peer, datagrams can be sent to that peer using send and can be received only from this peer using recv.
Support for sockets with type SOCK_RAW is not required, but service providers are encouraged to support raw sockets as practicable.
Notes for IrDA Sockets
The Af_irda.h header file must be explicitly included.
OnlySOCK_STREAM is supported; the SOCK_DGRAM type is not supported by IrDA.
The protocol parameter is always set to 0 for IrDA.
Note On Windows NT, raw socket support requires administrative privileges.
Requirements
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winsock2.h.
Library: Use Ws2_32.lib.
See Also
Windows Sockets Programming Considerations Overview, Socket Functions, accept, bind, connect, getsockname, getsockopt, ioctlsocket, listen, recv, recvfrom, select, send, sendto, setsockopt, shutdown, WSASocket

相關詞條

熱門詞條

聯絡我們