基于tcp的socket编程。

一。server端:

#include
#include
using namespace std;


#pragma comment(lib,"ws2_32.lib")


void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

//WINDOWS SOCKET APPLICITION
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");
//addrSer.sin_addr.S_un.S_addr = INADDR_ANY;


bind(sockSer,(SOCKADDR*)&addrSer,sizeof(SOCKADDR));


listen(sockSer,5);//???????


cout<<"服务器等待客户端的连接......"<

SOCKET sockConn;
int len = sizeof(SOCKADDR);
sockConn = accept(sockSer,(SOCKADDR*)&addrCli,&len);


if(sockConn == INVALID_SOCKET)
{
cout<<"Server Accept Client Connect Fail!"<WSACleanup( );
return;
}
else
{
cout<<"Server Accept Client Connect Success!"<cout<<"Client ip:>"<cout<<"Client port:>"<}


char sendbuf[256];
char recvbuf[256];
while(1)
{
cout<<"Ser:>";
cin>>sendbuf;
send(sockConn,sendbuf,strlen(sendbuf)+1,0);
recv(sockConn,recvbuf,256,0);
cout<<"Cli:>"<}


closesocket(sockSer);
WSACleanup( );
}


client端:

#include
#include
using namespace std;


#pragma comment(lib,"ws2_32.lib")


void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

//WINDOWS SOCKET APPLICITION
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 sockCli;
sockCli = socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSer;
addrSer.sin_family = AF_INET;
addrSer.sin_port = htons(5050);
addrSer.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");


int res = connect(sockCli,(SOCKADDR *)&addrSer,sizeof(SOCKADDR));
if(res != 0)
{
cout<<"Client Connect Server Fail!"<WSACleanup( );
return;
}
else
{
cout<<"Client Connect Server Success!"<}


char sendbuf[256];
char recvbuf[256];
while(1)
{
recv(sockCli,recvbuf,256,0);
cout<<"Ser:>"<cout<<"Cli:>";
cin>>sendbuf;
send(sockCli,sendbuf,strlen(sendbuf)+1,0);
}
closesocket(sockCli);
WSACleanup( );


}

你可能感兴趣的:(基于tcp的socket编程。)