#include "stdafx.h"
#include <WINSOCK2.H>
#include <iostream.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc, char* argv[])
{
SOCKET m_sock;
WSADATA wsaData;
if (0 !=WSAStartup(MAKEWORD(2,2),&wsaData))
{
cout<<"加载套接字失败!\n";
return 0;
}
m_sock = socket(AF_INET,SOCK_STREAM,0);
if (m_sock==INVALID_SOCKET)
{
cout<<"socket失败!\n";
return 0;
}
sockaddr_in baddr;
baddr.sin_family=AF_INET;
baddr.sin_port=htons(6789);
baddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
if (SOCKET_ERROR==bind(m_sock,(sockaddr*)&baddr,sizeof(sockaddr_in)))
{
cout<<"bind失败!\n";
closesocket(m_sock);
WSACleanup();
return 0;
}
if (SOCKET_ERROR == listen(m_sock,5))
{
cout<<"listen失败!\n";
closesocket(m_sock);
WSACleanup();
return 0;
}
return 0;
}