这是我在网上找的一段代码,放在vs2017里面没问题。大多数是头文件在qt里面不能用,有的关键字在qt里面也不能用,会报错和警告,qt不是可以在window下面编程的嘛,遇到这种问题该怎么解决
#include <iostream>
#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main(int argc, char *argv[])
{
const int BUF_SIZE = 64;
WSADATA wsd;//WSADATA变量
SOCKET sServer;//服务端套接字
SOCKET sClient;//客户端套接字
SOCKADDR_IN addrServ;//服务器地址
char buf[BUF_SIZE];//接受数据缓冲区
char sendBuf[BUF_SIZE];//返回给客户端的数据
int retVal;//返回值
//初始化套接字动态库
if(WSAStartup(MAKEWORD(2,2),&wsd) != 0)
{
cout << "WSAStartup failed!" << endl;
return 1;
}
//创建套接字
sServer = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(INVALID_SOCKET == sServer)
{
cout << "socket failed!" << endl;
WSACleanup();//释放套接字资源
return -1;
}
//服务器套接字地址
addrServ.sin_family = AF_INET;
addrServ.sin_port = htons(6666);
addrServ.sin_addr.s_addr = INADDR_ANY;
//绑定套接字
retVal = bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN));
if(SOCKET_ERROR == retVal)
{
cout << "bind failed!" << endl;
closesocket(sServer);//关闭套接字
WSACleanup();//释放套接字资源
return -1;
}
//开始监听
retVal = listen(sServer,1);
if(SOCKET_ERROR == retVal)
{
cout << "listen failed!" << endl;
closesocket(sServer);//关闭套接字
WSACleanup();//释放套接字资源
return -1;
}
//接受客户端请求
sockaddr_in addrClient;
int addrClientlen = sizeof(addrClient);
sClient = accept(sServer,(sockaddr FAR*)&addrClient,&addrClientlen);
if(INVALID_SOCKET == sClient)
{
cout << "accept failed!" << endl;
closesocket(sServer);//关闭套接字
WSACleanup();//释放套接字资源
return -1;
}
while(true)
{
//接受客户端数据
ZeroMemory(buf,BUF_SIZE);
retVal = recv(sClient,buf,BUF_SIZE,0);
if(SOCKET_ERROR == retVal)
{
cout << "recv failed!" << endl;
closesocket(sServer);//关闭套接字
WSACleanup();//释放套接字资源
return -1;
}
if(buf[0] == '0')
{
break;
}
cout << "客户端发送的数据:" << buf << endl;
cout << "向客户端发送数据:";
cin >> sendBuf;
send(sClient,sendBuf,strlen(sendBuf),0);
}
//退出
closesocket(sServer);//关闭套接字
closesocket(sClient);//关闭套接字
WSACleanup();//释放套接字资源
return 0;
}
错误 提示:
C:UsersAdministrationDocumentsQtservermain.cpp:4: warning: ignoring #pragma comment [-Wunknown-pragmas]
#pragma comment(lib,"ws2_32.lib")
C:UsersAdministrationDocumentsQtservermain.cpp:18: error: undefined reference to `_imp__WSAStartup@8'
C:UsersAdministrationDocumentsQtservermain.cpp:24: error: undefined reference to `_imp__socket@12'
C:UsersAdministrationDocumentsQtservermain.cpp:28: error: undefined reference to `_imp__WSACleanup@0'
C:UsersAdministrationDocumentsQtservermain.cpp:33: error: undefined reference to `_imp__htons@4'
C:UsersAdministrationDocumentsQtservermain.cpp:36: error: undefined reference to `_imp__bind@12'
C:UsersAdministrationDocumentsQtservermain.cpp:40: error: undefined reference to `_imp__closesocket@4'
C:UsersAdministrationDocumentsQtservermain.cpp:41: error: undefined reference to `_imp__WSACleanup@0'
C:UsersAdministrationDocumentsQtservermain.cpp:45: error: undefined reference to `_imp__listen@8'
C:UsersAdministrationDocumentsQtservermain.cpp:49: error: undefined reference to `_imp__closesocket@4'
C:UsersAdministrationDocumentsQtservermain.cpp:50: error: undefined reference to `_imp__WSACleanup@0'
C:UsersAdministrationDocumentsQtservermain.cpp:56: error: undefined reference to `_imp__accept@12'
C:UsersAdministrationDocumentsQtservermain.cpp:60: error: undefined reference to `_imp__closesocket@4'
C:UsersAdministrationDocumentsQtservermain.cpp:61: error: undefined reference to `_imp__WSACleanup@0'
C:UsersAdministrationDocumentsQtservermain.cpp:68: error: undefined reference to `_imp__recv@16'
C:UsersAdministrationDocumentsQtservermain.cpp:72: error: undefined reference to `_imp__closesocket@4'
C:UsersAdministrationDocumentsQtservermain.cpp:73: error: undefined reference to `_imp__WSACleanup@0'
C:UsersAdministrationDocumentsQtservermain.cpp:83: error: undefined reference to `_imp__send@16'
C:UsersAdministrationDocumentsQtservermain.cpp:86: error: undefined reference to `_imp__closesocket@4'
C:UsersAdministrationDocumentsQtservermain.cpp:87: error: undefined reference to `_imp__closesocket@4'
C:UsersAdministrationDocumentsQtservermain.cpp:88: error: undefined reference to `_imp__WSACleanup@0'
collect2.exe:-1: error: error: ld returned 1 exit status
有头文件的错误,也有关键词的错误
winsock?
不是给出了警告么?
warning: ignoring #pragma comment [-Wunknown-pragmas]
链接问题.
你的qt的编译器配置的什么? mscv?
如果选的是msvc 可以试试在.pro
PS: qt有网络库的,可以通过
qt +=network
使用qt的网络库.