编写服务端代码如下:
#include <stdio.h>
#include <WinSock2.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}
/* The WinSock DLL is acceptable. Proceed. */
}
运行结果,报错如下:
1>------ Rebuild All started: Project: TCPSrv, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'TCPSrv', configuration 'Debug|Win32'
1>Compiling...
1>TcpSrv.cpp
1>Compiling manifest to resources...
1>Linking...
1>TcpSrv.obj : error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function _main
1>TcpSrv.obj : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function _main
1>D:/Win32Project/TCPSrv/Debug/TCPSrv.exe : fatal error LNK1120: 2 unresolved externals
1>Build log was saved at "file://d:/Win32Project/TCPSrv/Debug/BuildLog.htm"
1>TCPSrv - 3 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
解决方案:
在源文件里加 #pragma comment(lib, "Ws2_32.lib")
本文介绍了一个关于使用WinSock进行网络编程时遇到的典型错误:无法解析外部符号__imp__WSACleanup@0和__imp__WSAStartup@8。通过在源文件中添加特定的库指令解决了该问题。
768

被折叠的 条评论
为什么被折叠?



