(C++) Winsock connect and listen program. Need Help!

This is a discussion on (C++) Winsock connect and listen program. Need Help! within the Networking/Device Communication forums, part of the General Programming Boards category; Question is below code. MY CODE FOR RECIVING CONNECTING PROGRAM: Code: #include <windows.h> //Required for socket init #include <iostream> int ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    76

    (C++) Winsock connect and listen program. Need Help!

    Question is below code.
    MY CODE FOR RECIVING CONNECTING PROGRAM:

    Code:
    #include <windows.h> //Required for socket init
    #include <iostream>
    int main(){
    char buf[256];
    WSAData wsdata; //Declare WSAData
    WORD wsver=MAKEWORD(2, 0); //We want Winsock 2.0
    int nret=WSAStartup(wsver, &wsdata); //Pass version 2.0 and pointer to implement
    if(nret != 0){ //Init failed
    
    std::cout<<"Startup failed, error code: "<<WSAGetLastError(); //Returns error code
    WSACleanup(); //Cleanup Winsock library
    return -1;
    }
    std::cout<<"Init success\n";
    SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
    if(kSock == INVALID_SOCKET){
    std::cout<<"Socket init failed";
    return -1;
    }
    std::cout<<"Socket initialized\n";
    sockaddr_in sin;
    
    std::cout<<"Enter a port number: ";
    int porta;
    std::cin >> porta;
    
    sin.sin_port=htons(porta); //Connect to port 80
    
    std::cout<<"Enter a ip number: ";
    std::string ipad;
    ipad = "";
    std::cin >> ipad;
    char ipaad[256];
    strcpy(ipaad, ipad.c_str());
    
    sin.sin_addr.s_addr=inet_addr(ipaad); //Connect to localhost
    sin.sin_family=AF_INET;
    if(connect(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){ //Check the condition
    std::cout<<"Connect failed, error: "<<WSAGetLastError(); //Returns error code
    WSACleanup(); //Cleanup the library
    std::string lisquit;
    std::cin>>lisquit;
    }
    std::cout<<"Connection successful!\n";
    recv(kSock, buf, sizeof(buf), 0); //Receive "Hello" from server
    std::cout << buf;
    closesocket(kSock);
    std::string lisquit;
    std::cin>>lisquit;
    }
    CODE FOR LISTENING PROGRAM:

    Code:
    #include <windows.h> //Required for socket init
    #include <iostream>
    int main(){
    char buf[256];
    std::cout<<"Enter a message to send: \n";
    std::cin >> buf;
    WSAData wsdata; //Declare WSAData
    WORD wsver=MAKEWORD(2, 0); //We want Winsock 2.0
    int nret=WSAStartup(wsver, &wsdata); //Pass version 2.0 and pointer to implement
    if(nret != 0){ //Init failed
    /*A successful return value should be 0 
    std::cout<<"Startup failed, error code: "<<WSAGetLastError(); //Returns error code
    WSACleanup(); //Cleanup Winsock library
    return -1;
    }
    std::cout<<"Init success\n";
    SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
    if(kSock == INVALID_SOCKET){
    std::cout<<"Socket init failed";
    return -1;
    }
    std::cout<<"Socket initialized\n";
    sockaddr_in sin;
    std::cout<<"Enter a port number: ";
    int porta;
    std::cin >> porta;
    sin.sin_port=htons(porta);
    sin.sin_addr.s_addr=INADDR_ANY;
    sin.sin_family=AF_INET;
    if(bind(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR){
    std::cout<<"Failed to bind\n";
    WSACleanup(); //Cleanup Winsock library
    return -1;
    }
    std::cout<<"Bind successful!\n";
    while (listen(kSock, SOMAXCONN) == SOCKET_ERROR); //Loop in order to constantly listen
    /* set the number of connections to SOMAXCONN, in which case the provider chooses a reasonable value (5 in Windows XP
    Professional) 
    SOCKET client;
    int len = sizeof(sin);
    client=accept(kSock, (sockaddr*)&sin, &len);
    std::cout<<"Connection established!\n";
    send(client, buf, sizeof(buf), 0); //Send "Hello"
    closesocket(client); //Close both socket handles
    closesocket(kSock);
    WSACleanup();
    std::string lisquit;
    std::cin>>lisquit;
    }
    Works fine with ip 127.0.0.1 and port 808 but what ip and port do I enter for other pc's. It usually dosn't recive if I enter another ip even if I have a sender open on that pc. I have no firewall problems.
    Last edited by azjherben; 04-02-2009 at 05:46 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Some indentation wouldn't go amiss.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    WhOA!!! :S what a mess to sift through. :S
    Code:
     while (listen(kSock, SOMAXCONN) == SOCKET_ERROR);
    Please explain the use of that loop.
    Yes I know what you're trying to do but, logically speaking, explain it's function there.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What exactly does listen() do from winsock 1.1
    By *DEAD* in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-14-2007, 06:48 PM
  2. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 05:00 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. Non blocking listen() with winsock
    By manwhoonlyeats in forum C Programming
    Replies: 1
    Last Post: 12-08-2002, 07:00 PM
  5. win32 Winsock problem
    By spoon_ in forum Windows Programming
    Replies: 3
    Last Post: 07-19-2002, 02:19 AM

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