Thread: Difference between Unix sockets and winsock.

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    80

    Difference between Unix sockets and winsock.

    Hi, I find sockets quite interesting and since I just have started to learn a little about them i'm not a pro
    (that won't be a secret to anyone who see my code.)
    What I want to know is, if there's alot difference between unix socket programming and winsock.
    Here's my unix code for a server program which does nothing except telling you when an incoming connection is accepted.
    Code:
    #include <iostream.h>
    #include <arpa/inet.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    int main()
    {
        int serv, clnt, port, sin_size = sizeof(sockaddr_in);
        sockaddr_in server, client;
        bool loop = true;
        cout << "Select port: "; cin >> port;
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = INADDR_ANY;
        server.sin_port = htons(port);
        if ((serv = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        {
            cout << "An error occured while trying to create socket... quitting!";
            return 1;
        }
        if (bind(serv, (sockaddr*)&server, sizeof(sockaddr)) < 0)
        {
            cout << "An error occured while trying to bind socket... quitting!";
            return 1;
        }    
        if (listen(serv, 1) < 0)
        {
            cout << "An error occured while trying to listen for a connection... quitting!";
            return 1;
        }    
        while(loop)
        {
            if (clnt = accept(serv, (sockaddr*)&client, &sin_size) < 0)
            {
                cout << "Could not accept connection!";
                return 1;
            }    
            cout << "Got connection from: " << inet_ntoa(client.sin_addr) << "!";
            if (send(clnt, "Connection successfull.\n", 24, 0) > 0)
            {
                loop = false;
            }    
            close(clnt);
        }    
        return 0;
    }
    Any coments on the code is very welcome, I would really appreciate
    any corrections if there's something wrong or stupid about the code.
    Most importantly I would really really appreciate a good link for a winsock tutorial.
    Last edited by antex; 01-20-2005 at 04:25 PM. Reason: Made a mistake...

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    What I want to know is, if there's alot difference between unix socket programming and winsock
    Not really. All the concepts are the same, and the code is pretty similar as well. In Winsock there are a couple of extra structs you need to declare for Winsock to use. Once you've learned Unix sockets, it just takes a little example code to convert - that's what I did, using this tutorial:

    http://www.hal-pc.org/~johnnie2/winsock.html

    And do a google search for Beej's Guide - it's quite good for sockets in general.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    Ok, thank you for your reply. I'll try to get better at the unix part before starting out on winsock since i'm not very good at sockets in general yet.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    Why the hell do you want to do it with Winsock? For winsock you need to link some winsock dll and initialize it with code! And if that Win computer, where you run your app, wont have winsock*.dll you app wont work! stupid isn't it?! Unix is MUCH more powerfull than Windows! If you would at least learn it a little, you would understand!

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Erm..

    Winsock actually has some rather powerful extensions which help with speed, scalability, etc.

    Unix is MUCH more powerfull than Windows!
    is just a fallacy.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    Quote Originally Posted by azteched
    is just a fallacy.
    Are you stupid or what?
    Windows is crashing has buggy software, viruses, worms, trojans, big price, insecure.. and you call that a powerfull OS?

    *nix has only fiew viruses, software bugs are fixed much more faster! *nix is a powerfull tool.

    For example when you install Windows, what programming tools do you get? debug.com!? I'm sure there arent that meany pleople who knows assembly!

    When you install *nix what tools do you get? C/C++, java, assembler, python etc.. compilers!

    And Windows SP2 has cut off RAW socket support for winsock! How nice a really friendly OS for a programmer!

    When you install *nix, you get tons of networking apps!
    What do you get when you install Windows!? some buggy IE and IIS? Cool isnt it?

    Don't you get tired of all thous dll's without yor app can't run?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    By the way, there is a book called UNIX NETWORK PROGRAMMING:
    http://www.amazon.com/exec/obidos/tg...books&n=507846
    And take a look at this:
    http://www.amazon.com/exec/obidos/se...278784-0943319

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    This isn't a "I love Unix/Linux/Windows/MacOS" soapbox forum. Most of your points are entirely irrelevant to network programming. Fair enough, you can hate Windows if you want. This isn't the right forum for fanboy rants though.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    Its not about hating Windows! and so what, that I am so stupid in programming? Allready searched all my posts? You just pointed that Unix isn't more powerfull than Windows! Do a little google search and you will find out that you are wrong!

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Why the hell do you want to do it with Winsock? For winsock you need to link some winsock dll and initialize it with code!
    And on unix you need to link to the appropriate library as well, so that argument holds no weight at all. The only extra thing windows requires is a call to WSAStartup().

    And if that Win computer, where you run your app, wont have winsock*.dll you app wont work!
    And if that Unix computer, where you run your app, wont have socket libraries you app wont work!

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    And if that Unix computer, where you run your app, wont have socket libraries you app wont work!
    Everything is compiled in executable file! And socket(); connect(); are system calls, aren't they? They are not functions of some libraries!

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You're absolutely right, hyaku_. Unix is so much more powerful, that programming for Windows is pointless. Why don't you go and pitch your argument in the Windows Programming Forum?

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Everything is compiled in executable file!
    On windows you can compile against the static winsock library (libwsock32.a) and do the exact same thing.

    And socket(); connect(); are system calls, aren't they? They are not functions of some libraries!
    On unix these are not system calls. They are functions in the socket library, which is why you have to pass -lsocket to the linker in order to create a socket program.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    Even on Beej's socket programming guide, socket(); connect(); bind(); etc.. are called System Calls!

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    On linux or BSD systems they are. On every unix system I have worked on, they are library functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 08:32 AM
  2. Cross platform sockets
    By zacs7 in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-27-2007, 05:16 AM
  3. Winsock - Where do i start?
    By Brain Cell in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-14-2005, 01:39 PM
  4. Sockets, Winsock
    By mattbbx in forum Networking/Device Communication
    Replies: 4
    Last Post: 08-21-2003, 04:09 PM
  5. Winsock vs Unix Sockets
    By khoxxxy in forum Networking/Device Communication
    Replies: 4
    Last Post: 08-05-2003, 05:13 AM