#include "server.h"
#include <iostream>
#include <thread>
#include <string>
#include <WS2tcpip.h>
using std::cerr;
using std::cout;
using std::endl;
#define SERVER_PORT 1024
#define MSG_BUF_SIZE 1024
std::list<SOCKET> list_sockets;
Server::Server()
{
//cout << "Initializing server...\n";
//
winsock_ver = MAKEWORD(2, 2);
addr_len = sizeof(SOCKADDR_IN);
addr_svr.sin_family = AF_INET;
addr_svr.sin_port = ::htons(SERVER_PORT);
addr_svr.sin_addr.S_un.S_addr = ADDR_ANY;
memset(buf_ip, 0, IP_BUF_SIZE);
//WSA启动成功…
ret_val = WSAStartup(winsock_ver, &wsa_data);
if (ret_val != 0)
{
cerr << "WSA failed to start up!Error code: " << ::WSAGetLastError() << "\n";
system("pause");
exit(1);
}
//cout << "WSA started up successfully...\n";
//创建服务器套接字
sock_svr = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock_svr == INVALID_SOCKET)
{
cerr << "Failed to create server socket!Error code: " << ::WSAGetLastError() << "\n";
WSACleanup();
system("pause");
exit(1);
}
//cout << "Server socket created successfully...\n";
// 服务器套接字绑定成功
ret_val = ::bind(sock_svr, (SOCKADDR*)&addr_svr, addr_len);
if (ret_val != 0)
{
cerr << "Failed to bind server socket!Error code: " << ::WSAGetLastError() << "\n";
WSACleanup();
system("pause");
exit(1);
}
//cout << "Server socket bound successfully...\n";
// 服务器套接字开始侦听
ret_val = ::listen(sock_svr, SOMAXCONN);
if (ret_val == SOCKET_ERROR)
{
cerr << "Server socket failed to listen!Error code: " << ::WSAGetLastError() << "\n";
WSACleanup();
system("pause");
exit(1);
}
//cout << "Server socket started to listen...\n";
// 服务器已成功启动..
cout << "Server started successfully..." << endl;
}
Server::~Server()
{
closesocket(sock_svr);
closesocket(sock_clt);
WSACleanup();
cout << "Socket closed..." << endl;
}
void sendMess() {
while (1) {
std::string input;
std::cin >> input;
//printf("you are send %s", buf);
std::list<SOCKET>::iterator it;
for (it = list_sockets.begin(); it != list_sockets.end(); ++it){
send(*it, input.c_str(), input.size(), 0);
}
}
}
DWORD WINAPI CreateClient(LPVOID lpParameter);
//
void Server::waitForClient()
{
while (true)
{
sock_clt = accept(sock_svr, (SOCKADDR*)&addr_clt, &addr_len);
if (sock_clt == INVALID_SOCKET)
{
cerr << "Failed to accept client!Error code: " << WSAGetLastError() << "\n";
WSACleanup();
system("pause");
exit(1);
}
list_sockets.push_back(sock_clt);
InetNtop(addr_clt.sin_family, &addr_clt,(PWSTR)buf_ip, IP_BUF_SIZE);
cout << "A new client connected...IP sock_svr: " << sock_svr << ", port number: " << ntohs(addr_clt.sin_port) << endl;
h_thread = ::CreateThread(nullptr, 0, CreateClient, (LPVOID)sock_clt, 0, nullptr);
if (h_thread == NULL)
{
cerr << "Failed to create a new thread!Error code: " << ::WSAGetLastError() << "\n";
WSACleanup();
system("pause");
exit(1);
}
CloseHandle(h_thread);
std::thread (sendMess).detach();
}
}
DWORD WINAPI CreateClient(LPVOID lpParameter)
{
SOCKET sock_clt = (SOCKET)lpParameter;
char buf_msg[MSG_BUF_SIZE];
int ret_val = 0;
int snd_result = 0;
do
{
memset(buf_msg, 0, MSG_BUF_SIZE);
ret_val = ::recv(sock_clt, buf_msg, MSG_BUF_SIZE, 0);
if (ret_val > 0)
{
if (strcmp(buf_msg, "exit") == 0)
{
cout << "客户端请求关闭连接..." << endl;
list_sockets.remove(sock_clt);
closesocket(sock_clt);
break;
}
else {
cout << "socket_number:" << sock_clt << " message_log: " << buf_msg << endl;
std::list<SOCKET>::iterator it;
for (it = list_sockets.begin(); it != list_sockets.end(); ++it){
if (send(*it, buf_msg, MSG_BUF_SIZE, 0) == SOCKET_ERROR)
{
cerr << "向客户端发送消息失败!错误代码: " << ::GetLastError() << "\n";
list_sockets.remove(sock_clt);
closesocket(sock_clt);
system("pause");
return 1;
}
}
}
}else if (ret_val == 0)
{
cout << "连接已关闭..." << endl;
list_sockets.remove(sock_clt);
closesocket(sock_clt);
}
else
{
cerr << "无法从客户端接收消息!错误代码: " << ::GetLastError() << "\n";
list_sockets.remove(sock_clt);
closesocket(sock_clt);
//system("pause");
//return 1;
}
} while (ret_val > 0);
//
ret_val = ::shutdown(sock_clt, SD_SEND);
if (ret_val == SOCKET_ERROR)
{
cerr << "Failed to shutdown the client socket!Error code: " << ::GetLastError() << "\n";
closesocket(sock_clt);
system("pause");
return 1;
}
return 0;
}