#include <iostream>
#include <windows.h>
using namespace std;
#pragma comment (lib,"ws2_32.lib")
void main ()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}
/* The WinSock DLL is acceptable. Proceed. */
///////////////////////////////////////
SOCKET sockSer;
sockSer=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSer,addrCli;
addrSer.sin_family= AF_INET;
addrSer.sin_port=htons(5050);
addrSer.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
bind(sockSer,(SOCKADDR*)&addrSer,sizeof(SOCKADDR));
listen (sockSer,5);
SOCKET sockconn;
int len=sizeof(SOCKADDR);
cout<<"server wait client connect......"<<endl;
sockconn=accept(sockSer,(SOCKADDR*)&addrCli,&len);
if(sockconn == INVALID_SOCKET)
{
cout<<"server accept client connnect fail!"<<endl;
return ;
}
else
{
cout<<"server accept client connnect success!"<<endl;
}
char sendbuf[256];
char recvbuf[256];
while(1)
{
cout<<"Ser:>";
cin>>sendbuf;
if(strcmp(sendbuf,"quit")==0)
{
break;
}
send(sockconn,sendbuf,strlen(sendbuf)+1,0);
recv(sockconn,recvbuf,256,0);
cout<<"Cli:>"<<recvbuf<<endl;
}
closesocket(sockSer);
WSACleanup();
}