Winsock connection problem

This is a discussion on Winsock connection problem within the C++ Programming forums, part of the General Programming Boards category; I got a server and a client, i run the server on my own computer, i first tested the client ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    29

    Winsock connection problem

    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.

  2. #2
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    almost certainly a firewall eating the request.
    Seeing as you're using port 8080 it might be that even if the port is forwarded it is also filtered to allow only http traffic (which is what 8080 is normally used for).
    Or your router allows it to pass but you have a software firewall.
    Or your friend has a firewall that blocks the program.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    Im not using firewall, ill change the port to 50.

    EDIT:
    Both my friend and me are not using firewalls, and i have changed the port to 50.Still the timed out error.
    Last edited by Nephiroth; 02-07-2006 at 07:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem about Data connection with C++
    By ralph23 in forum C++ Programming
    Replies: 0
    Last Post: 02-15-2006, 04:33 AM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. winsock problem
    By /Muad'Dib\ in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-07-2004, 12:29 AM
  4. WinSock Problem
    By loobian in forum C++ Programming
    Replies: 1
    Last Post: 02-09-2002, 10:25 AM
  5. Small Winsock problem...
    By SyntaxBubble in forum C++ Programming
    Replies: 0
    Last Post: 02-09-2002, 09:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21