只实现了服务器端的服务
和其他语言一样,实现通信需要以下步骤
首先要导入socket库
#include
因为我们需要和客户端进行连接,所以既要创建服务端套接字,也要创建接收端套接字
SOCKET s_socket;
SOCkET s_accept;
SOCKADDR_IN server_addr;
server_addr.sin_family = AF_INET; //指定地址族为IPV4
server_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);// Host to network long INADDR_ANY对应0.0.0,0
server_addr.sin_port = htos(5010)// Host to newtwork short 设置端口
if(bind(s_server, (SOCKADDR*)&server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR)
{
cout<<"绑定失败"<<endl;
WSACleanup();
return;
}
cout<<"绑定成功"<<endl;
if(listen(s_server, SOMAXCONN) < 0)
{
cout<<"开启监听失败"<<endl;
WSACleanup();
return;
}
cout<<"开启监听成功"<<endl;
当客户端与服务端进行连接时,需要创建里一个套接字地址来进行接收
SOCKADDR accept_addr;
int len = sizeof(SOCKADDR)
s_accept = accept(s_server, (SOCKADDR*)&accept_addr, &sizeof(SOCKADDR));
if(s_accept == SOCKET_ERROR)
{
cout<<"建立连接失败";
WSACleanup();
return;
}
cout<<"建立连接成功"<<endl<<"准备接受数据";
while(true)
{
recv_len = recv(s_accept, recv_buf, 1024, 0);
if(recv_len < 0)
{
cout << "接受失败" <<endl;
break;
}
else{
cout << "客户信息:" << endl << recv_buf << endl;
}
string s= "HTTP/1.1 200 OK\nContent-Type: text/html; charset=UTF-8\nConnection: keep-alive\nCache-Control: no-store, no-cache, must-revalidate\nPragma: no-cache\nContent-Length: ";
String data = "Hello C++ Socket
";
s = s + data.length() + "\n\n" + data ;
strcpy(send_bug, s.c_str());
memcpy(send_buf, ptr, 1024);
send_len = send(s_accept, send_buf, 1024, 0);
if(send_len < 0)
{
cout << "发送失败" << endl;
break;
}
}
closesocket(s_server);
closesocket(s_accept);
WSACleanup();
retrun;
当socket版本不符合时,是不能通信的
这就需要在通信前检测版本是否符合
void checkVision()
{
WORD w_req = MAKEWORD(2, 2);
WSADATA wsadata;
int err;
err = WSAStartup(w_req, &wsadata);
if(err!=0)
{
cout<<"初始化套接字库失败";
}
else
{
cout<<"初始化套接字库成功";
}
// 检测版本号
if(LOBYTE(wsadata.wVersion)!= 2|| HIBYTE(wsadata.wHighVersion)!=2 )
{
cout<<"套接字库版本号不符合";
WSACleanup();
}
else
{
cout<<"套接字库版本正确";
}
}
整体代码
使用的编译器是QtCraete,就简单使用qDebug来进行打印输出了
#include "selfsocket.h"
SelfSocket::SelfSocket()
{
WORD w_req = MAKEWORD(2, 2);
WSADATA wsadata;
int err;
err = WSAStartup(w_req, &wsadata);
if(err!=0)
{
qDebug()<<"初始化套接字库失败";
}
else
{
qDebug()<<"初始化套接字库成功";
}
// 检测版本号
if(LOBYTE(wsadata.wVersion)!= 2|| HIBYTE(wsadata.wHighVersion)!=2 )
{
qDebug()<<"套接字库版本号不符合";
WSACleanup();
}
else
{
qDebug()<<"套接字库版本正确";
}
}
void SelfSocket::Init()
{
//创建套接字
SOCKET s_server;
SOCKET s_accept;
s_server = socket(AF_INET, SOCK_STREAM, 0);// param: 协议族,协议类型,协议编号
//创建长度
int send_len, recv_len, len = 0;
// 创建接受和发送缓存区
char send_buf[1024];
char recv_buf[1024];
// 填充服务端信息:端口,地址
SOCKADDR_IN server_addr;
SOCKADDR_IN accept_addr;
server_addr.sin_family = AF_INET; //指定地址族为IPV4
server_addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);// Host to network long INADDR_ANY对应0.0.0,0
server_addr.sin_port = htons(5010);// Host to network short
// 套接字绑定
if(bind(s_server,(SOCKADDR *)&server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR){
qDebug()<<"套接字绑定失败";
WSACleanup();
return;
}
qDebug()<<"套接字绑定成功";
// 开启监听
if(listen(s_server, SOMAXCONN) < 0)
{
qDebug()<<"监听开启失败";
WSACleanup();
return;
}
qDebug()<<"开启监听"<<endl<<"服务器监听连接中>>:";
// 接受连接请求
len = sizeof(SOCKADDR);
s_accept = accept(s_server,(SOCKADDR*)&accept_addr, &sizeof(SOCKADDR));
if(s_accept == SOCKET_ERROR)
{
qDebug()<<"建立连接失败";
WSACleanup();
return;
}
qDebug()<<"建立连接成功"<<endl<<"准备接受数据";
while(true)
{
recv_len = recv(s_accept, recv_buf, 1024, 0);
if(recv_len < 0)
{
qDebug()<<"接受失败"<<endl;
break;
}
else{
qDebug()<<"客户信息:"<<recv_buf << endl;
}
QString s= "HTTP/1.1 200 OK\nContent-Type: text/html; charset=UTF-8\nConnection: keep-alive\nCache-Control: no-store, no-cache, must-revalidate\nPragma: no-cache\nContent-Length: %1\n\n";
QString data = "Hello C++ Socket
";
s += data;
s = s.arg(data.length());
qDebug()<<s.toLocal8Bit().data()<<endl;
QByteArray qb = s.toLatin1();
char *ptr;
ptr = qb.data();
memcpy(send_buf, ptr, 1024);
send_len = send(s_accept, send_buf, 1024, 0);
if(send_len < 0)
{
qDebug()<<"发送失败"<<endl;
break;
}
}
// 关闭套接字
closesocket(s_server);
closesocket(s_accept);
WSACleanup();
return;
}