Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Socket Server
#1
Just posting this here. This is a server that justs waits for a connection from anything, even telnet.

Code:
#include <iostream>
#include <winsock2.h>

int main()
{
    WSAData wsadata;
    WORD Version = MAKEWORD(2, 1);

    WSAStartup(Version, &wsadata);

    SOCKADDR_IN ex;//struct child
    short length = sizeof(ex);

    SOCKET Listen = socket(AF_INET, SOCK_STREAM, NULL);//SOCK_STREAM is basically TCP while SOCK_DGRAM is UDP
    SOCKET Connect = socket(AF_INET, SOCK_STREAM, NULL);

    ex.sin_addr.s_addr = inet_addr("127.0.0.1");//ip address
    ex.sin_family = AF_INET;
    ex.sin_port = htons(100);//port, doesn't usually matter
    //Use htons() with sin_port for byte ordering

    bind(Listen,(SOCKADDR*)&ex, sizeof(ex));

    listen(Listen, SOMAXCONN);

    for(;;)
    {
        std::cout<<"Listening...";
        if(Connect = accept(Listen, (SOCKADDR*)&ex, (int*)&length))
        {
            std::cout<<"\nConnection accepted!\n";
            break;
        }
    }
    std::cin.get();
    return 0;
}

To test for a connection i'll use telnet. Windows xp users should have it by default.
Vistsa/7 users should go to control panel->programs and features->Turn on...-> Select telnet client.
[Image: Telnet.gif]
Reply


Messages In This Thread
Socket Server - by Scorch - 02-15-2011, 05:49 PM
RE: Socket Server - by Gaijin - 02-15-2011, 05:56 PM
RE: Socket Server - by Scorch - 02-16-2011, 10:30 AM
RE: Socket Server - by Modest - 03-06-2011, 11:50 AM
RE: Socket Server - by Scorch - 03-07-2011, 04:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple in progress socket library Nightly 3 1,699 02-14-2012, 08:12 AM
Last Post: AceInfinity

Forum Jump:


Users browsing this thread: 1 Guest(s)