linux c++下的最简单的服务端hello world
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <string>
#include <cstring>
#include <arpa/inet.h>
#include <sys/socket.h>
using namespace std;
int main()
{
char message[]="hello world";
//创建socket
int serv_sock = socket(PF_INET,SOCK_STREAM,0);
if(serv_sock == -1)
cout<<"socket error!"<<endl;
//设置地址和端口
struct sockaddr_in serv_addr={0};
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
serv_addr.sin_port = htons(11800);
//绑定地址和端口
int ret = bind(serv_sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
if (ret == -1)
cout<<"bind error!"<<endl;
//设置队列大小并监听
ret = listen(serv_sock,5);
if(ret == -1)
cout<<"listen error!"<<endl;
//客户socket
int clnt_sock = 0;
struct sockaddr_in clnt_addr={0};
socklen_t clnt_addr_size = sizeof(clnt_addr);
//有连接到来会解除accept的阻塞状态
clnt_sock = accept(serv_sock,(struct sockaddr*)&clnt_addr,&clnt_addr_size);
if(clnt_sock == -1)
cout<<"accept error !"<<endl;
//效果等同于send函数
write(clnt_sock,message,sizeof(message));
close(serv_sock);
close(clnt_sock);
return 0;
}
windows c++下的最简单的服务端hello world
注意添加头文件#include <WinSock2.h> 和连接库ws2_32.lib
#include<iostream>
#include <WinSock2.h>
using namespace std;
int main()
{
WSAData wsdata = { 0 };
int ret = WSAStartup(MAKEWORD(2,2),&wsdata);
if (ret != 0)
std::cout << "WSAStartup error !!" << std::endl;
char message[] = "hello world";
SOCKET serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if (serv_sock == INVALID_SOCKET)
cout << "socket error!" << endl;
struct sockaddr_in serv_addr = { 0 };
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("0.0.0.0");
serv_addr.sin_port = htons(11800);
ret = bind(serv_sock, (struct sockaddr*) & serv_addr, sizeof(serv_addr));
if (ret == -SOCKET_ERROR)
cout << "bind error!" << endl;
ret = listen(serv_sock, 5);
if (ret == SOCKET_ERROR)
cout << "listen error!" << endl;
SOCKET clnt_sock = 0;
struct sockaddr_in clnt_addr = { 0 };
int clnt_addr_size = sizeof(clnt_addr);
clnt_sock = accept(serv_sock, (struct sockaddr*) & clnt_addr, &clnt_addr_size);
if (clnt_sock == INVALID_SOCKET)
cout << "accept error !" << endl;
send(clnt_sock, message,strlen(message)+1,0);
closesocket(clnt_sock);
closesocket(serv_sock);
cout << "end!!!!!!" << endl;
WSACleanup();
return 0;
}
windows 下select模型
#include<iostream>
#include <WinSock2.h>
using namespace std;
int main()
{
WSAData wsdata = { 0 };
int ret = WSAStartup(MAKEWORD(2, 2), &wsdata);
if (ret != 0)
std::cout << "WSAStartup error !!" << std::endl;
char message[] = "hello world";
SOCKET serv_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serv_sock == INVALID_SOCKET)
cout << "socket error!" << endl;
struct sockaddr_in serv_addr = { 0 };
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("0.0.0.0");
serv_addr.sin_port = htons(11801);
ret = bind(serv_sock, (struct sockaddr*) & serv_addr, sizeof(serv_addr));
if (ret == SOCKET_ERROR)
cout << "bind error!" << endl;
ret = listen(serv_sock, 5);
if (ret == SOCKET_ERROR)
cout << "listen error!" << endl;
fd_set allSockts;
清空客户socket队列
//FD_ZERO(&clinetSockts);
在队列中添加socket
//FD_SET(serv_sock, &clinetSockts);
删除队列中指定的socket 不会执行 closesocket
//FD_CLR(serv_sock, &clinetSockts);
判断 socket是否在队列中 包含返回非0 不在返回 0
//FD_ISSET(serv_sock, &clinetSockts);
FD_ZERO(&allSockts);
FD_SET(serv_sock,&allSockts);
SOCKET clnt_sock = 0;
struct sockaddr_in clnt_addr = { 0 };
int clnt_addr_size = sizeof(clnt_addr);
while (true)
{
//确保,数组不会被覆盖所以每次复制一份
fd_set allSockts_cpy = allSockts;
struct timeval st= {3,200};
int ret = select(0,&allSockts_cpy, 0,0,&st);
if (ret == 0)
{
continue;
}
else if (ret > 0)
{
for (size_t i = 0; i < allSockts_cpy.fd_count; i++)
{
if (allSockts_cpy.fd_array[i] == serv_sock)
{
clnt_sock = accept(allSockts_cpy.fd_array[i], (struct sockaddr*) & clnt_addr, &clnt_addr_size);
if (clnt_sock == INVALID_SOCKET)
{
continue;
}
cout << "和客户端连接成功" << endl;
//和客户端连接成功
FD_SET(clnt_sock, &allSockts);
}
else
{
char buf[1024] = { 0 };
int nrecv = recv(allSockts_cpy.fd_array[i],buf,1024,0);
if (0 == nrecv)
{
SOCKET so = allSockts_cpy.fd_array[i];
FD_CLR(allSockts_cpy.fd_array[i], &allSockts);
closesocket(so);
}
else if (nrecv > 0)
{
cout << buf << endl;
send(allSockts_cpy.fd_array[i], buf,strlen(buf),0);
}
else //error
{
int a = WSAGetLastError();
cout << "WSAGetLastError() = " <<a <<endl;
SOCKET so = -1;
switch (a)
{
case 10054:
so = allSockts_cpy.fd_array[i];
FD_CLR(allSockts_cpy.fd_array[i], &allSockts);
closesocket(so);
break;
default:
break;
}
}
}
}
}
else
{
//发生错误
int a = WSAGetLastError();
cout << "WSAGetLastError() = " <<endl;
}
}
WSACleanup();
return 0;
}