小弟初学VC,有个问题,请帮忙。
我用socket写了一个服务端程序,编译通过了,但是在链接的时候出了以下的错误:Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/SERVER.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.SERVER.exe - 2 error(s), 0 warning(s)我已经添加了wsock32.lib.这是什么错误呢?该怎么解决?
谢谢。

解决方案 »

  1.   

    代码很长,如下:#include <winsock2.h>
    #include "resource.h"#define DEFAULT_PORT        4012  // 默认监听端口
    #define DEFAULT_BUFFER      4096  // 默认识缓冲区大小LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        PSTR szCmdLine, int iCmdShow)
    {
        HWND                hWnd;
        MSG                 msg;
        WNDCLASS            wndClass;
        char                szClassName[] = "TCPServer";    wndClass.style          = CS_HREDRAW | CS_VREDRAW;
        wndClass.lpfnWndProc    = WndProc;
        wndClass.cbClsExtra     = 0;
        wndClass.cbWndExtra     = 0;
        wndClass.hInstance      = hInstance;
        wndClass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
        wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wndClass.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 1);
        wndClass.lpszMenuName   = MAKEINTRESOURCE(IDR_MENU1);
        wndClass.lpszClassName  = szClassName;    RegisterClass(&wndClass);    hWnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
            szClassName,
            "AngelFish Game Server",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            NULL,
            NULL,
            hInstance,
            NULL);    ShowWindow(hWnd, iCmdShow);
        UpdateWindow(hWnd);    while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }    return msg.wParam;}  // WinMain//
    // use this struct to hold thread data
    //
    typedef struct _THREADDATA
    {
    SOCKET sock;
    HWND hWnd;
    } THREADDATA, *LPTHREADDATA;//
    // Function: ClientThread
    //
    // Description:
    //    This function is called as a thread, and it handles a given
    //    client connection.  The parameter passed in is the socket 
    //    handle returned from an accept() call.  This function reads
    //    data from the client and writes it back.
    //
    DWORD WINAPI ClientThread(LPVOID lpParam)
    {
        SOCKET        sock=((LPTHREADDATA)lpParam)->sock;
        char          szBuff[DEFAULT_BUFFER];
        int           ret,
                      nLeft,
                      idx;
    char          szMsg[255];
    HWND          hWnd = ((LPTHREADDATA)lpParam)->hWnd;    while(1)
        {
            // Perform a blocking recv() call
            //
            ret = recv(sock, szBuff, DEFAULT_BUFFER, 0);
            if (ret == 0)        // Graceful close
                break;
            else if (ret == SOCKET_ERROR)
            {
                wsprintf(szMsg, "recv()失败: %d",
    WSAGetLastError());
    MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
                break;
            }
            szBuff[ret] = '\0';
            wsprintf(szMsg, "RECV: '%s'", szBuff); // add the szBuff text to ListBox
    //
    SendMessage(GetDlgItem(hWnd, 1000), LB_ADDSTRING, 0,
    (LPARAM)szMsg);
    SendMessage(GetDlgItem(hWnd, 1000), LB_SETCURSEL,
    SendMessage(GetDlgItem(hWnd, 1000), LB_GETCOUNT, 0, 0)
    - 1, 0);        nLeft = ret;
            idx = 0;
    //
            // Make sure we write all the data
            //
            while(nLeft > 0)
            {
                ret = send(sock, &szBuff[idx], nLeft, 0);
                if (ret == 0)
                    break;
                else if (ret == SOCKET_ERROR)
                {
                    wsprintf(szMsg, "send()失败: %d", 
                        WSAGetLastError());
         MessageBox(hWnd, szMsg, "", MB_OK |MB_ICONSTOP);
                    break;
                }
                nLeft -= ret;
                idx += ret;
               }
    }
        return 0;
    }//
    // Function: Process
    //
    // Description:
    //    This function is called as a thread. It initialize
    //    Winsock, create the listening socket, bind to the
    //    local address, and wait for client connections.
    //
    DWORD WINAPI Process(LPVOID lpParam)
    {
    HWND          hWnd = (HWND)lpParam;
        WSADATA       wsd;
    char          szMsg[255];
        SOCKET        sListen,
                      sClient;
        int           iAddrSize;
        HANDLE        hThread;
        DWORD         dwThreadId;
        struct sockaddr_in local,
                           client;
    THREADDATA    data;    if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
        {
            MessageBox(hWnd, "不能加载Winsock!",
    "", MB_OK | MB_ICONSTOP);
            return 1;
        }
        // Create our listening socket
        //
        sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
        if (sListen == SOCKET_ERROR)
        {
            wsprintf(szMsg, "socket()失败: %d",
    WSAGetLastError());
    MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
            return 1;
        }
        // Set the local interface and bind to it
        //
        local.sin_addr.s_addr = htonl(INADDR_ANY);
        local.sin_family = AF_INET;
        local.sin_port = htons(DEFAULT_PORT);    if (bind(sListen, (struct sockaddr *)&local, 
                sizeof(local)) == SOCKET_ERROR)
        {
            wsprintf(szMsg, "bind()失败: %d",
    WSAGetLastError());
    MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
            return 1;
        }
        listen(sListen, 8);
        //
        // In a continous loop, wait for incoming clients. Once one 
        // is detected, create a thread and pass the handle off to it.
        //
        while (1)
        {
            iAddrSize = sizeof(client);
            sClient = accept(sListen, (struct sockaddr *)&client,
                            &iAddrSize);        
            if (sClient == INVALID_SOCKET)
            {        
                wsprintf(szMsg, "accept()失败: %d",
    WSAGetLastError());
         MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
                break;
            }
            wsprintf(szMsg, "已接受客户: %s:%d", 
                inet_ntoa(client.sin_addr), ntohs(client.sin_port)); // add the szMsg text to ListBox
    //
    SendMessage(GetDlgItem(hWnd, 1000), LB_ADDSTRING, 0,
    (LPARAM)szMsg);
    SendMessage(GetDlgItem(hWnd, 1000), LB_SETCURSEL,
    SendMessage(GetDlgItem(hWnd, 1000), LB_GETCOUNT, 0, 0)
    - 1, 0); // set the ClientThread required data
    //
    data.hWnd = hWnd;
    data.sock = sClient;        hThread = CreateThread(NULL, 0, ClientThread, 
                        (LPVOID)&data, 0, &dwThreadId);
            if (hThread == NULL)
            {
                wsprintf(szMsg, "CreateThread()失败: %d",
    GetLastError());
         MessageBox(hWnd, szMsg, "", MB_OK | MB_ICONSTOP);
                break;
            }
            CloseHandle(hThread);
        }
        closesocket(sListen);
        
        WSACleanup();
        return 0;
    }
    LRESULT CALLBACK WndProc(HWND hWnd, UINT iMsg, 
       WPARAM wParam, LPARAM lParam)
    {
    static HWND hwndListBox;
    HANDLE hThread;
    DWORD dwThreadId;
    int i;    switch(iMsg)
        {
        case WM_CREATE:         // create ListBox window
    hwndListBox = CreateWindowEx(0,
    "listbox",
    "",
    WS_CHILD | WS_VISIBLE | WS_VSCROLL,
    0, 0, 0, 0,
    hWnd,
    (HMENU)1000,
    (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
    0);
    return 0; case WM_SIZE:           // size the ListBox to fill client area
    MoveWindow(hwndListBox, 0, 0, LOWORD(lParam),
    HIWORD(lParam), FALSE);
    return 0; case WM_COMMAND:
    switch (LOWORD(wParam))
    {
    case IDM_RECV:      // create a listening thread
                hThread = CreateThread(NULL, 0, Process,
    (LPVOID)hWnd, 0, &dwThreadId);
                if (hThread == NULL)
    {
        char szErrMsg[255];
                    wsprintf(szErrMsg, "CreateThread()失败: %d",
    GetLastError());
        MessageBox(hWnd, szErrMsg, "",
    MB_OK | MB_ICONSTOP);
    }
                CloseHandle(hThread);
    break;

    case IDM_CLEAR:     // clear the content of ListBox
    for (i = SendMessage(hwndListBox, LB_GETCOUNT, 0, 0) - 1;
    i >= 0 ; i--)
        SendMessage(hwndListBox, LB_DELETESTRING, i, 0);
    break;
    }
    return 0;    case WM_DESTROY:
            PostQuitMessage(0);
            return 0;    default:
            return DefWindowProc(hWnd, iMsg, wParam, lParam);
        }
    }  // WndProc
      

  2.   

    同意二楼,首先build->clean,然后build->rebuild all
      

  3.   

    编译方式不对,这是一个win32程序,你用console的配置来编译当然编不过
      

  4.   

    2楼的方法试过了,还是不行.yszmax(兩廣~早餐是南瓜餅雞蛋豆漿炒米飯):怎么知道我是用console的配置来编译的呢?谢谢
      

  5.   

    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/SERVER.exe : fatal error LNK1120: 1 unresolved externals
    这里要求的是main函数,所以猜你用的console
      

  6.   

    你可以在vc6.0的开发环境下修改一下工程的属性就可以了:
    Project -> Settings... -> Link 选项卡 -> Project Options 
    在那个框里找到/subsystem: console ,
    把它改成/subsystem:windows就可以了。