1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <winsock2.h>
int main()
{
WSAData wsa;
WORD Version=MAKEWORD(2,1);
WSAStartup(Version, &wsa);
SOCKET Listen=socket(AF_INET, SOCK_STREAM, NULL);
SOCKET sock=socket(AF_INET, SOCK_STREAM, NULL);
SOCKADDR_IN Server;
Server.sin_addr.s_addr=inet_addr("127.0.0.1");
Server.sin_family=AF_INET;
Server.sin_port=htons(100);
bind(Listen, (SOCKADDR*)&Server, sizeof(Server));
listen(Listen, 1);
int size=sizeof(Server);
std::cout<<"Listening...";
for (;;)
{
if (sock=accept(Listen, (SOCKADDR*)&Server, &size))
{
std::cout<<"\nConnection was reached";
break;
}
}
char buf[4096];
std::cout<<std::endl;
while (recv(sock,buf,4096,0))
{
std::cout<<buf;
}
WSACleanup();
std::cin.get();
return 0;
}
|