#include<winsock2.h>
#include<iostream.h>
#pragma comment(lib,"ws2_32.lib")
void main()
{
SOCKET s;
s=soket(AF_INET,SOCK_DGRAM,0);
if(s==INVALID_SOCKET)
cout<<"error";
}
怎么老是error

解决方案 »

  1.   

    socket
    The Windows Sockets socket function creates a socket that is bound to a specific service provider.SOCKET socket (
      int af,       
      int type,     
      int protocol  
    );
     
    Parameters
    af 
    [in] An address family specification. 
    type 
    [in] A type specification for the new socket. 
    The following are the only two type specifications supported for Windows Sockets 1.1: Type Explanation 
    SOCK_STREAM Provides sequenced, reliable, two-way, connection-based byte streams with an out-of-band data transmission mechanism. Uses TCP for the Internet address family. 
    SOCK_DGRAM Supports datagrams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. Uses UDP for the Internet address family. In Windows Sockets 2, many new socket types will be introduced and don' need to be specified now because an application can dynamically discover the attributes of each available transport protocol through the WSAEnumProtocols function. Socket type definitions will appear in WINSOCK2.H, which will be periodically updated as new socket types, address families and protocols are defined. protocol 
    [in] A particular protocol to be used with the socket that is specific to the indicated address family. 
    Res
    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 non-overlapped usage on an overlapped socket if the values for parameters related to overlapped operation are NULL.When selecting a protocol and its supporting service provider this procedure will 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 connected state before any data can be sent or received on it. A connection to another socket is created with a connect call. Once connected, 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 implement a 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 peers using 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 RAW is not required, but service providers are encourage to support raw sockets whenever it makes sense to do so.Windows CE: Windows CE supports the PF_INET and AF_IRDA ARPA Internet address formats. To use IrSock, set the af parameter to AF_IRDA and set the protocol parameter to NULL.Windows CE supports SOCK_STREAM and SOCK_DCRAM socket types.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 Codes
    WSANOTINITIALISED A successful WSAStartup 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 more socket 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. 
    QuickInfo
      Windows NT: Yes
      Windows: Yes
      Windows CE: Use version 1.0 and later.
      Header: Declared in winsock2.h.
      Import Library: Link with ws2_32.lib.See Also
    accept, bind, connect, getsockname, getsockopt, ioctlsocket, listen, recv, recvfrom, select, send, sendto, setsockopt, shutdown, WSASocket  
      

  2.   

    #include<winsock2.h>
    #include<iostream.h>
    #pragma comment(lib,"ws2_32.lib")
    void main()
    {
    SOCKET s;
    WSADATA wsaData; 
    WSAStartup(MAKEWORD( 1, 1 ), &wsaData );//初始化
    s=socket(AF_INET,SOCK_DGRAM,0);
    if(s==INVALID_SOCKET)
    cout<<"error";
    }
      

  3.   

    先调用WSAStartup初始化winsock2的库。