Witam!
Mam problem z moimi aplikacjami klient-serwer. Nie mam pojęcia jak poprawić kod, żeby się nie wysypywały, chodzi o to, że nie mogą się połączyć. Przy próbie połączenia klient się wysypuje w 41 linijce i WSAGetLastError() zwraca 10061, ale wina może być po stronie serwera, na MSDN odnośnie tego błędu przeczytałem:
No connection could be made because the target computer actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host—that is, one with no server application running.
Proszę o pomoc.
Serwer:
#include <iostream>
#include <winsock2.h>
#define PORT 2000
#define BACKLOG 5
#define BUFF_SIZE 1000
using namespace std;
void error(unsigned int line_of_error);
WSADATA WSD;
int sockaddr_size = sizeof(struct sockaddr);
sockaddr_in my_addr, client_addr;
SOCKET my_sockfd, client_sockfd;
int recv_bytes = 0;
char msg[BUFF_SIZE];
int main()
{
if (WSAStartup(MAKEWORD(2, 2), &WSD) != 0) error(__LINE__);
my_addr.sin_family = PF_INET;
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
my_addr.sin_port = htons(PORT);
memset(&my_addr.sin_zero, 0, 8);
if (static_cast<int>(my_sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) error(__LINE__);
if (static_cast<int>(bind(my_sockfd, reinterpret_cast<sockaddr *>(&my_addr), sockaddr_size)) == -1) error(__LINE__);
while (true)
{
if (recv_bytes == 0)
{
cout << "LISTENING FOR CLIENT..." << '\n';
if (static_cast<int>(listen(my_sockfd, BACKLOG)) == -1) error(__LINE__);
cout << "..." << '\n';
if (static_cast<int>(client_sockfd = accept(my_sockfd, reinterpret_cast<sockaddr *>(&client_addr), &sockaddr_size)) == -1) error(__LINE__);
cout << "CONNECT FROM " << inet_ntoa(client_addr.sin_addr) << " ACCEPTED" << '\n';
}
recv_bytes = recv(client_sockfd, msg, BUFF_SIZE-1, 0);
if (recv_bytes == -1) error(__LINE__);
if (recv_bytes == 0)
{
cout << "CLIENT DISCONNECTED" << '\n';
continue;
}
msg[recv_bytes] = '\0';
cout << msg << '\n';
}
shutdown(my_sockfd, 2);
WSACleanup();
return 0;
}
void error(unsigned int line_of_error)
{
cout << "Error at line: " << line_of_error << '\n';
cout << "WSAGetLastError(): " << WSAGetLastError() << '\n';
cout << "Program will be terminated, press any key to continue...";
cin.get();
shutdown(my_sockfd, 2);
WSACleanup();
exit(1);
}
Klient:
#include <iostream>
#include <winsock2.h>
#define PORT 2000
#define BUFF_SIZE 1000
using namespace std;
void error(unsigned int line_of_error);
WSADATA WSD;
int sockaddr_size = sizeof(struct sockaddr);
sockaddr_in server_addr;
SOCKET my_sockfd;
string IP;
char msg[BUFF_SIZE];
int main()
{
if (WSAStartup(MAKEWORD(2, 2), &WSD) != 0) error(__LINE__);
cout << "INPUT SERVER IP: ";
cin >> IP;
server_addr.sin_family = PF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr(IP.c_str());
memset(&(server_addr.sin_zero), '\0', 8);
if (static_cast<int>(my_sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) error(__LINE__);
cout << "CONNECTING TO " << IP << " ..." << '\n';
if (connect(my_sockfd, reinterpret_cast<struct sockaddr *>(&server_addr), sockaddr_size) == -1) error(__LINE__);
cout << "CONNECTED" << '\n';
while (true)
{
memset(msg, 0, BUFF_SIZE);
cin >> msg;
if (msg[0] == 0) break;
if (send(my_sockfd, msg, BUFF_SIZE, 0) == -1) error(__LINE__);
}
shutdown(my_sockfd, 2);
WSACleanup();
return 0;
}
void error(unsigned int line_of_error)
{
cout << "Error at line: " << line_of_error << '\n';
cout << "WSAGetLastError(): " << WSAGetLastError() << '\n';
cout << "Program will be terminated, press ENTER to continue...";
cin.ignore();
cin.get();
shutdown(my_sockfd, 2);
WSACleanup();
exit(1);
}