编了一个程序出现下面错误:
Linking...
MCServer.obj : error LNK2001: unresolved external symbol _accept@12
MCServer.obj : error LNK2001: unresolved external symbol _listen@8
MCServer.obj : error LNK2001: unresolved external symbol _bind@12
MCServer.obj : error LNK2001: unresolved external symbol _htons@4
MCServer.obj : error LNK2001: unresolved external symbol _closesocket@4
MCServer.obj : error LNK2001: unresolved external symbol _WSAAsyncSelect@16
MCServer.obj : error LNK2001: unresolved external symbol _WSACleanup@0
MCServer.obj : error LNK2001: unresolved external symbol _socket@12
MCServer.obj : error LNK2001: unresolved external symbol _WSAStartup@8
Debug/MCServer.exe : fatal error LNK1120: 9 unresolved externals
Error executing link.exe.
请问是怎么回事啊????
谢谢!!!!!!!!!!!!!!!!!!1
代码是:
#include <windows.h>
#define SERVER_PORT 1111
#define WM_SOCKET (WM_USER+1)
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpcmdLine, int nCmdShow)
{
static TCHAR szAppName[]= TEXT("ChatServer");
static TCHAR szClassName[]= TEXT("ChatServerClass");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
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)GetStockObject( WHITE_BRUSH );
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szClassName;
if ( !RegisterClass( &wndclass ) )
{
MessageBox( NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR );
return 0;
}
hwnd = CreateWindow( szClassName, TEXT("Simple WinSock Chat Servere"),
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL );
ShowWindow( hwnd, nCmdShow );
UpdateWindow( hwnd );
while ( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HINSTANCE hInstance;
int nSize;
WSADATA WSAData;
static struct sockaddr_in server,from;
static SOCKET local_server, local_listen;
int nError;
WORD wEvent, wError;
static TCHAR szReceive[1024];
static TCHAR szSend[1024];
switch( message )
{
case WM_CREATE:
hInstance = (HINSTANCE) GetWindowLong( hwnd, GWL_HINSTANCE );
if( nError = WSAStartup( MAKEWORD(2,0), &WSAData ) )
{
MessageBox( hwnd, "WinSock Startup Error!", "Server", MB_ICONERROR | MB_OK );
return TRUE;
}
local_server = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( local_server == INVALID_SOCKET )
{
MessageBox( hwnd, "Socket Creation Error!", "Server", MB_ICONERROR | MB_OK );
WSACleanup();
return TRUE;
}
if( SOCKET_ERROR == WSAAsyncSelect( local_server, hwnd,
WM_SOCKET, FD_ACCEPT | FD_READ ) )
{
MessageBox( hwnd, "WSAAsyncSelect Error!", "Server", MB_ICONERROR | MB_OK );
closesocket( local_server );
WSACleanup();
return TRUE;
}
server.sin_family = AF_INET;
server.sin_port = htons( SERVER_PORT );
if( SOCKET_ERROR == bind( local_server, (struct sockaddr *)&server, sizeof(server) ) )
{
MessageBox( hwnd, "Bind Error!", "Server", MB_ICONERROR | MB_OK );
closesocket( local_server );
WSACleanup();
return TRUE;
}
listen( local_server, 5 );
return 0;
case WM_SIZE:
return 0;
case WM_SOCKET:
wEvent = WSAGETSELECTEVENT(lParam);
wError = WSAGETSELECTERROR(lParam);
switch( wEvent )
{
case FD_ACCEPT:
if( wError )
{
MessageBox( hwnd, "Accept Error!", "Server", MB_ICONERROR | MB_OK );
return 0;
}
nSize = sizeof(from);
local_listen = accept( local_server, (struct sockaddr *)&from, &nSize );
return 0;
case FD_READ:
if( wError )
{
MessageBox( hwnd, "Read Error!", "Server", MB_ICONERROR | MB_OK );
return 0;
}
return 0;
}
return 0;
case WM_SETFOCUS:
return 0;
case WM_COMMAND:
return 0;
case WM_DESTROY:
closesocket( local_server );
closesocket( local_listen );
WSACleanup();
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hwnd, message, wParam, lParam );
}