Have you called WSAStartup? Here is an example using udp.
//http://www.district86.k12.il.us/central/activities/computerclub/Tutorials/Winsock/Lesson5.htm
// file name: wsudprcv.c
//
// description:     A UDP packet receiver that waits for
//                  UDP packets on a specific port#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma comment(lib,"ws2_32")
int main(int argc, int **argv)
{
   WSADATA wsda; // Structure to store info
// returned from WSAStartup   char szMessage[512];
   int iMessageLen;
   int ret;   char szAddress[64];
   int iPort;   SOCKET s; // Our TCP socket handle
   SOCKADDR_IN addr, // The local interface address
               remote_addr; // The sender's address
   int iRemoteAddrLen; // Contains the length of remte_addr, passed to recvfrom
/*
   // Check arguments
   if(argc != 2 ||
      (argc==2 && strcmp((char *) &argv[1][0], "/?")==0))
   {
      printf("wsudprcv port\n");
      printf("   port:   the port the server should listen to\n");
      exit(1);
   }   // Get the remote port
   iPort = atoi((char *) &argv[1][0]);   if(iPort<0 || iPort>65563)
   {
      printf("Invalid port number! (%s)\n", argv[2]);
      exit(1);
   }
*/
iPort = 2020;
   iMessageLen = 512; // Set to the length of szMessage buffer   // Load version 1.1 of Winsock   WSAStartup(MAKEWORD(1,1), &wsda);   // Create an UDP socket
   printf("Creating socket...");
   s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);   // Error?
   if(s == SOCKET_ERROR)
   {
      printf("Error\nCall to socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); failed with:\n%d\n", WSAGetLastError());
      exit(1);
   }   printf("OK\n");   // Fill in the interface information
   printf("Binding socket...");
   addr.sin_family = AF_INET;
   addr.sin_port = htons(iPort);
   addr.sin_addr.s_addr = INADDR_ANY;   if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) == SOCKET_ERROR)
   {
      printf("Error\nCall to bind(s, (struct sockaddr *) &addr, sizeof(addr)); failed with:\n%d\n", WSAGetLastError());
      exit(1);
   }
   printf("OK\n");   // Ready to receive data   printf("Waiting for packets (Press Ctrl-C to exit)...");   iRemoteAddrLen = sizeof(remote_addr);
   ret = recvfrom(s, szMessage, iMessageLen, 0, (struct sockaddr *) &remote_addr, &iRemoteAddrLen);   if(ret == SOCKET_ERROR)
   {
      printf("Error\nCall to recvfrom(s, szMessage, iMessageLen, 0, (struct sockaddr *) &remote_addr, &iRemoteAddrLen); failed with:\n%d\n", WSAGetLastError());
      exit(1);
   }
   printf("Packet received\n");   iMessageLen = ret; // Length of the data received   szMessage[iMessageLen] = '\0'; // Convert to cstring   printf("\"%s\" received from %s\n", szMessage, inet_ntoa(remote_addr.sin_addr));   closesocket(s);   WSACleanup();
getch();
   return 0;
}

解决方案 »

  1.   

    Have you called WSAStartup? Yes ,I have Finished it.
      

  2.   


    1)如果你是用的UDP的协议,不能调用listen
      面向联接的才需要调用listen
    2)我有一个封好的UDP类,如果需要,告诉我你的email
      

  3.   

    1)MSDN中关于listen()函数的解释:To accept connections, a socket is first created with the socket function and bound to a local address with the bind function, a backlog for incoming connections is specified with listen, and then the connections are accepted with the accept function. /****注意*/Sockets that are connection oriented, those of type SOCK_STREAM for example, are used with listen. /*注意*/The socket s is put into "passive'' mode where incoming connection requests are acknowledged and queued pending acceptance by the process.2)肯定已经调用WSAStartup,要不bind()怎不出错呢?
      

  4.   

    同意 archoo(archoo)的看法
      

  5.   

    同意 archoo(archoo), UDP不需要listen。