我正在编写一个控制台聊天程序,我正在学习WinSock2库。到目前为止,我有一个工作的服务器和客户端,唯一的问题是服务器不处理客户端,但客户端说它已连接。现在除了测试连接是否工作之外没有其他功能。服务器假设在成功接受客户端时打印出消息。我一直试图解决这个问题,但不能。非常感谢任何帮助。
服务器标头:
#pragma once
#ifndef SERVER_H
#define SERVER_H
#include <iostream>
#include <string>
#include "winsock.h"
class Server
{
public:
Server(int port);
~Server();
private:
const int port;
std::string error;
WORD wVersion;
WSADATA wsaData;
SOCKET hSocket;
bool init();
bool setupSocket();
void startServer();
void closeConnection();
};
#endif
服务器:
#include "Server.h"
Server::Server(int port) : port(port), wVersion(MAKEWORD(2, 2))
{
if (init()) {
std::cout << "Server successfully setup" << std::endl << std::endl;
startServer();
} else {
std::cout << "Error: " + error << std::endl;
std::cin.get();
std::cin.get();
}
}
bool Server::init()
{
return setupSocket();
}
bool Server::setupSocket()
{
int iResult = WSAStartup(wVersion, &wsaData);
if (iResult) {
error = "Problem initializating WinSock2";
return false;
} else if (wsaData.wVersion != wVersion) {
error = "Incorrect WinSock version";
return false;
}
std::cout << "WinSock initialized" << std::endl;
sockaddr_in sockAddr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(port);
sockAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (hSocket == INVALID_SOCKET) {
error = "Problem setting up socket";
return false;
}
std::cout << "Socket setup" << std::endl;
if (bind(hSocket, (LPSOCKADDR) &sockAddr, sizeof(sockAddr)) == SOCKET_ERROR) {
error = "Problem binding socket";
return false;
}
std::cout << "Socket binded to port" << std::endl;
listen(hSocket, SOMAXCONN);
std::cout << "Server is now listening on port " << port << std::endl;
SOCKET client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (client == INVALID_SOCKET) {
error = "Problem setting up socket";
return false;
}
if (client = accept(hSocket, (SOCKADDR *)&sockAddr, NULL)) {
std::cout << "A client connected!";
}
return true;
}
void Server::startServer()
{
std::string consoleInput;
while (true) {
std::cin >> consoleInput;
std::cout << consoleInput << std::endl;
}
}
void Server::closeConnection()
{
if (hSocket) {
closesocket(hSocket);
}
WSACleanup();
}
Server::~Server()
{
closeConnection();
}
客户端标题:
#pragma once
#ifndef CLIENT_H
#define CLIENT_H
#include <iostream>
#include <string>
#include "WinSock.h"
class Client
{
public:
Client(std::string name);
bool connectToServer(char ip[], int port);
~Client();
private:
std::string name;
std::string error;
WORD wVersion;
WSADATA wsaData;
SOCKET hSocket;
};
#endif
客户端:
#include "Client.h"
Client::Client(std::string name) : name(name), wVersion(MAKEWORD(2,2))
{
}
bool Client::connectToServer(char ip[], int port)
{
int iResult = WSAStartup(wVersion, &wsaData);
if (iResult) {
error = "Problem initializating WinSock2";
return false;
} else if (wsaData.wVersion != wVersion) {
error = "Incorrect WinSock version";
return false;
}
std::cout << "WinSock initialized" << std::endl;
sockaddr_in sockAddr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(port);
sockAddr.sin_addr.S_un.S_addr = inet_addr(ip);
hSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (hSocket == INVALID_SOCKET) {
error = "Problem setting up socket";
return false;
}
std::cout << "Socket setup" << std::endl;
if (connect(hSocket, (SOCKADDR *)&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR) {
error = "Problem connecting to server";
return false;
}
std::cout << "Connected to server!";
return true;
}
Client::~Client()
{
}