Thread: Win32...

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Win32...

    I would like to use this article (http://codeproject.com/internet/telnet_server.asp), but it is using MFC. Can someone please help me do it in Win32? My compiler doesn't support MFC and I can't even run MFC programs, because I don't have some DLL files for that in my computer. Don't want either.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Actually, I would like to have a simple program, that waits for a connection on some random port (which I specify), and receives one little string and then closes the connection.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You mean like the examples in the "Beej" tutorial.

    Moved to the networking forum, where you'll find more information in the "read first" sticky threads.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Beej examples don't work. I get thousands of errors.

    I don't know what's wrong with Beej, even MSDN examples work fine... only that the port is opened only in localhost, so it can only be accessed from the local computer...
    Last edited by maxorator; 08-27-2006 at 09:01 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Did you pay attention to the bit about "For windows programmers".?
    Are you going to post an actual example rather than just "it doesn't work"?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This doesn't work:
    http://beej.us/guide/bgnet/output/ht...l#simpleserver

    I don't even have the include files?!?!?!?!?!?!

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    OK, I see I'm going to have to explain this in excruciating detail

    1.5. Note for Windows Programmers
    First, ignore pretty much all of the system header files I mention in here. All you need to include is:

    #include <winsock.h>
    So delete all those header files and try including winsock.h

    Then read the rest of that paragraph and try again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Still:
    Code:
    Compiler: Default compiler
    Building Makefile: "C:\Programs\Dev-Cpp\telnet\Makefile.win"
    Executing  make...
    make.exe -f "C:\Programs\Dev-Cpp\telnet\Makefile.win" all
    g++.exe -c main.cpp -o main.o -I"C:/Programs/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Programs/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Programs/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Programs/Dev-Cpp/include/c++/3.4.2"  -I"C:/Programs/Dev-Cpp/include"   
    
    main.cpp: In function `void sigchld_handler(int)':
    main.cpp:11: error: `WNOHANG' undeclared (first use this function)
    main.cpp:11: error: (Each undeclared identifier is reported only once for each function it appears in.)
    main.cpp:11: error: `waitpid' undeclared (first use this function)
    main.cpp: In function `int main()':
    main.cpp:19: error: `socklen_t' undeclared (first use this function)
    main.cpp:19: error: expected `;' before "sin_size"
    main.cpp:20: error: aggregate `sigaction sa' has incomplete type and cannot be defined
    main.cpp:28: error: cannot convert `int*' to `const char*' for argument `4' to `int setsockopt(SOCKET, int, int, const char*, int)'
    main.cpp:50: error: `sigemptyset' undeclared (first use this function)
    main.cpp:51: error: `SA_RESTART' undeclared (first use this function)
    main.cpp:52: error: `SIGCHLD' undeclared (first use this function)
    main.cpp:52: error: invalid use of undefined type `struct sigaction'
    main.cpp:20: error: forward declaration of `struct sigaction'
    main.cpp:58: error: `sin_size' undeclared (first use this function)
    main.cpp:66: error: `fork' undeclared (first use this function)
    main.cpp:67: error: `close' undeclared (first use this function)
    make.exe: *** [main.o] Error 1
    Execution terminated

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Oh well, lessons in porting code then

    Remove all the signal and signal handler stuff
    Remove the fork()

    > main.cpp:19: error: `socklen_t'
    maybe declare it as int?

    > main.cpp:28: error: cannot convert `int*' to `const char*'
    Try a cast, or stop using C++ to compile C code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I'd better find some C++ tutorials then, because C is crap (that means I don't like it) and I asked for a C++ example.

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Removed fork(), close(), defined socklen_t as int and removed all signal stuff:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <winsock.h>
    
    #define MYPORT 3490    // the port users will be connecting to
    
    #define BACKLOG 10     // how many pending connections queue will hold
    typedef int socklen_t;
    
    int main(void)
    {
        int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd
        struct sockaddr_in my_addr;    // my address information
        struct sockaddr_in their_addr; // connector's address information
        socklen_t sin_size;
        int yes=1;
    
        if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }
    
        if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(const char*)&yes,sizeof(int)) == -1) {
            perror("setsockopt");
            exit(1);
        }
        
        my_addr.sin_family = AF_INET;         // host byte order
        my_addr.sin_port = htons(MYPORT);     // short, network byte order
        my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
        memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
    
        if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))
                                                                       == -1) {
            perror("bind");
            exit(1);
        }
    
        if (listen(sockfd, BACKLOG) == -1) {
            perror("listen");
            exit(1);
        }
    
        while(1) {  // main accept() loop
            sin_size = sizeof(struct sockaddr_in);
            if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,
                                                           &sin_size)) == -1) {
                perror("accept");
                continue;
            }
            printf("server: got connection from %s\n",
                                               inet_ntoa(their_addr.sin_addr));
        }
    
        return 0;
    }
    Program just closes and does nothing.
    Why can't Beej just make any working example?

    And this seems to be missing WSAStartup...

    I am wondering why MSDN example works only in the local network...
    Last edited by maxorator; 08-27-2006 at 01:12 PM.

  12. #12
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Here maxorator, I've got a solution for ya. I wrote a program on Dev-C++ using Winsock that does just what you want, a simple server that accepts a connection, receives a string and closes the connection. I commented heavily(at least for me) and put many references in the comments for you, references to the functions being used that is. I hope it's helpful and it compiled error free on Dev-C++. Code attached.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Isn't there a way to make it work without port forwarding...?

  14. #14
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    So you plan to run this sever behind a router? You don't have to port forward if you're connecting to it from inside the LAN you're on, otherwise you may have to if you want outside connections to your server, through your router.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    But some MUD games have run like this on my computer. (Compiled with Cygwin...)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console program to Win32
    By Ducky in forum Windows Programming
    Replies: 3
    Last Post: 02-25-2008, 12:46 PM
  2. Win32 API or Win32 SDK?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-20-2005, 03:26 PM
  3. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 08:50 AM
  4. OLE Clipboard :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2002, 05:57 PM
  5. Win32 API Tutorials?
    By c++_n00b in forum C++ Programming
    Replies: 9
    Last Post: 05-09-2002, 03:51 PM