I have made a server and a client.At first, I made a very simple server and client, the connection worked - client connected to server without any problems.But now I rewrote the server to make it better, but it doesnt connect now.The code is almost the same as before.I havent changed the client, im testing both server and client on the same computer.
------------SERVER
Code:
#include <winsock.h>
#include <iostream>
using namespace std;
WSADATA wsaData;
SOCKET Socket;
SOCKADDR_IN SockAddr;
SOCKET SetUpClient (const char* Clip, int nPort);
void AcceptClient(SOCKET ListeningSocket);
SOCKET Test(SOCKET ListeningSocket);
int nPort = 50;
int main (const char* Clip, int nPort) {
printf("Initializing WSA...\n");
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
printf("WSA Initialization failed.");
} else {
printf("->Succeeded.\n\n");
SOCKET ListeningSocket = SetUpClient(Clip, htons(nPort));
if ((ListeningSocket == 0) || (ListeningSocket == INVALID_SOCKET)) {
printf("Fatal error: Socket couldn't be initialized.");
system("PAUSE");
return 0;
} else {
printf("Waiting for connections...\n\n");
while (1) {
AcceptClient(ListeningSocket);
printf("Acceptor restarting...\n");
}
}
}
}
SOCKET SetUpClient (const char* Clip, int nPort) {
printf("Creating socket...\n");
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET) {
printf("Socket creation failed.");
} else {
printf("->Succeeded.\n\n");
SockAddr.sin_port = nPort;
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.S_un.S_addr = INADDR_ANY;
printf("Binding socket...\n");
if (bind(Socket, (SOCKADDR *)(&SockAddr), sizeof(SOCKADDR_IN)) == SOCKET_ERROR) {
printf("Attempt to bind failed with error code: ");
cout << GetLastError() << endl;
return 0;
} else {
printf("->Succeeded.\n\n");
listen(Socket, SOMAXCONN);
printf("Server initialized at port 50.\n\n");
return Socket;
}
}
return INVALID_SOCKET;
}
void AcceptClient(SOCKET ListeningSocket) {
SOCKADDR_IN sinRemote;
SOCKET AcceptSock;
int nAddrSize = sizeof(sinRemote);
while (1) {
SOCKET AcceptSock = accept(ListeningSocket, (SOCKADDR *)(&sinRemote),
&nAddrSize);
/*AcceptSock == SOCKET_ERROR;
while (AcceptSock == SOCKET_ERROR) {
AcceptSock = accept(ListeningSocket, (SOCKADDR *)(&sinRemote),
&nAddrSize);
cout << "test";
}
Socket = AcceptSock;*/
if (AcceptSock != INVALID_SOCKET) {
cout << "Accepted connection from " <<
inet_ntoa(sinRemote.sin_addr) << ":" <<
ntohs(sinRemote.sin_port) << "." <<
endl;
Socket = AcceptSock;
DWORD nThreadID;
//CreateThread(0, 0, EchoHandler, (void*)sd, 0, &nThreadID);
} else {
cout << "Fatal error: error code: " << GetLastError();
system("PAUSE");
return;
}
}
}
As you can see, ive been trying things out.I have checked if listen() worked, and it did.
------------CLIENT
Code:
#include <winsock.h>
#include <iostream>
using namespace std;
WSADATA wsaData;
int main () {
SOCKADDR_IN SockAddr;
printf("Initializing WSA...\n");
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
printf("WSA Initialization failed.");
} else {
printf("->Succeeded.\n\n");
}
printf("Creating socket...\n");
SOCKET Socket;
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET) {
printf("Socket creation failed.");
} else {
printf("->Succeeded.\n\n");
}
int port = 50;
SockAddr.sin_port = htons(port);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.S_un.S_un_b.s_b1 = 127;
SockAddr.sin_addr.S_un.S_un_b.s_b2 = 0;
SockAddr.sin_addr.S_un.S_un_b.s_b3 = 0;
SockAddr.sin_addr.S_un.S_un_b.s_b4 = 1;
//SockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
printf("Connecting to server...\n");
if (connect(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) != 0) {
int test = GetLastError();
printf("Failed to establish connection with server.\n\n");
printf("Error code:\n");
if (test == 10061) {
printf("WSAECONNREFUSED: Connection was refused by the server, server could be offline.");
} else if (test == 10058) {
printf("WSAESHUTDOWN: Shutdown commenced, cannot connect.");
} else if (test == 10060) {
printf("WSAETIMEDOUT: Timeout in connecting to server.");
} else {
cout << "Unknown error code: " << test;
}
} else {
printf("->Connection succeeded on 127.0.0.1.\n");
}
printf("\n\n\n*Test program ended for now*\n");
system("PAUSE");
}
So why cant the client connect to the server, it always gives me WSAECONNREFUSED?