I got a server and a client, i run the server on my own computer, i first tested the client to connect to 127.0.0.1 and that worked fine, now i wanted to test it outside my network, so i sent it to a couple of friends.I forwarded the used port on my router.
They get error 10060 Connection timed out error, while server is running, and ports are forwarded.Whats wrong?
Server src:
Code:
#include <winsock.h>
#include <iostream>
using namespace std;
WSADATA wsaData;
SOCKET Socket;
SOCKET TempSock = SOCKET_ERROR;
SOCKADDR_IN SockAddr;
int main () {
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(AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET) {
printf("Socket creation failed.");
} else {
printf("->Succeeded.\n\n");
}
//We want to use port 50
SockAddr.sin_port = 8080;
//We want an internet type connection (TCP/IP)
SockAddr.sin_family = AF_INET;
//We want to listen on IP address 127.0.0.1
//I'll give a few better ways to set thi // s value later
SockAddr.sin_addr.S_un.S_addr = INADDR_ANY;
//Ok all the information is set, lets bind()
printf("Binding socket...\n");
if (bind(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)) == SOCKET_ERROR) {
printf("Attempt to bind failed.");
return 0;
} else {
printf("->Succeeded.\n\n");
printf("Server listening at port 8080, waiting for clients.\n\n");
}
listen(Socket, 1);
while (TempSock == SOCKET_ERROR) {
TempSock = accept(Socket, NULL, NULL);
printf("Server:: Succesfully connected.\n\n");
}
Socket = TempSock;
printf("Server:: Waiting for messages.");
int RetVal = SOCKET_ERROR;
char String[50];
while (RetVal == SOCKET_ERROR) {
RetVal = recv(Socket, String, 50, 0);
if ((RetVal == 0)||(RetVal == WSAECONNRESET)||(RetVal == WSAECONNABORTED)) {
printf("Connection closed at other end.");
break;
}
}
printf("\n\n");
system("PAUSE");
}
Client src:
Code:
#include <winsock.h>
#include <windows.h>
#include <iostream>
#include <stdio.h>
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");
}
SockAddr.sin_port = 8080;
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.S_un.S_un_b.s_b1 = 62;
SockAddr.sin_addr.S_un.S_un_b.s_b2 = 131;
SockAddr.sin_addr.S_un.S_un_b.s_b3 = 160;
SockAddr.sin_addr.S_un.S_un_b.s_b4 = 46;
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 << "Unknow error code" << test;
}
} else {
printf("->Connection succeeded on 62.131.160.46:8080.\n");
}
printf("\n\n\n*Test program ended for now*\n");
system("PAUSE");
}
Can someone please help me here?
Btw 62.131.160.46 is my ip, and 8080 is the port im using.