02-15-2011, 05:49 PM
Just posting this here. This is a server that justs waits for a connection from anything, even telnet.
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.
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.