vc sockets编程

sockets(套接字)编程有三种,流式套接字(SOCK_STREAM),数据报套接字(SOCK_DGRAM),原始套接字(SOCK_RAW);基于TCP的socket编程是采用的流式套接字。在这个程序中,将两个工程添加到一个工作区。要链接一个ws2_32.lib的库文件。

服务器端编程的步骤:

1:加载套接字库,创建套接字(WSAStartup()/socket());

2:绑定套接字到一个IP地址和一个端口上(bind());

3:将套接字设置为监听模式等待连接请求(listen());

4:请求到来后,接受连接请求,返回一个新的对应于此次连接的套接字(accept());

5:用返回的套接字和客户端进行通信(send()/recv());

6:返回,等待另一连接请求;

7:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。

服务器端代码如下:
vc sockets编程 #include  < stdio.h >
vc sockets编程#include 
< Winsock2.h >
vc sockets编程
void  main()
vc sockets编程
{
vc sockets编程 WORD wVersionRequested;
vc sockets编程 WSADATA wsaData;
vc sockets编程 
int err;
vc sockets编程 
vc sockets编程 wVersionRequested 
= MAKEWORD( 11 );
vc sockets编程 
vc sockets编程 err 
= WSAStartup( wVersionRequested, &wsaData );
vc sockets编程 
if ( err != 0 ) {
vc sockets编程  
return;
vc sockets编程 }

vc sockets编程 
vc sockets编程 
if ( LOBYTE( wsaData.wVersion ) != 1 ||
vc sockets编程        HIBYTE( wsaData.wVersion ) 
!= 1 ) {
vc sockets编程  WSACleanup( );
vc sockets编程  
return;
vc sockets编程 }

vc sockets编程 SOCKET sockSrv
=socket(AF_INET,SOCK_STREAM,0);
vc sockets编程
vc sockets编程 SOCKADDR_IN addrSrv;
vc sockets编程 addrSrv.sin_addr.S_un.S_addr
=htonl(INADDR_ANY);
vc sockets编程 addrSrv.sin_family
=AF_INET;
vc sockets编程 addrSrv.sin_port
=htons(6000);
vc sockets编程 
vc sockets编程 bind(sockSrv,(SOCKADDR
*)&addrSrv,sizeof(SOCKADDR));
vc sockets编程
vc sockets编程 listen(sockSrv,
5);
vc sockets编程
vc sockets编程 SOCKADDR_IN addrClient;
vc sockets编程 
int len=sizeof(SOCKADDR);
vc sockets编程 
while(1)
vc sockets编程 
{
vc sockets编程  SOCKET sockConn
=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
vc sockets编程  
char sendBuf[50];
vc sockets编程  sprintf(sendBuf,
"Welcome %s to here!",inet_ntoa(addrClient.sin_addr));
vc sockets编程  send(sockConn,sendBuf,strlen(sendBuf)
+1,0);
vc sockets编程  
char recvBuf[50];
vc sockets编程  recv(sockConn,recvBuf,
50,0);
vc sockets编程  printf(
"%s\n",recvBuf);
vc sockets编程  closesocket(sockConn);
vc sockets编程 }

vc sockets编程
vc sockets编程}
客户端编程的步骤:

1:加载套接字库,创建套接字(WSAStartup()/socket());

2:向服务器发出连接请求(connect());

3:和服务器端进行通信(send()/recv());

4:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。

客户端的代码如下:
vc sockets编程 #include  < stdio.h >
vc sockets编程#include 
< Winsock2.h >
vc sockets编程
void  main()
vc sockets编程
{
vc sockets编程 WORD wVersionRequested;
vc sockets编程 WSADATA wsaData;
vc sockets编程 
int err;
vc sockets编程 
vc sockets编程 wVersionRequested 
= MAKEWORD( 11 );
vc sockets编程 
vc sockets编程 err 
= WSAStartup( wVersionRequested, &wsaData );
vc sockets编程 
if ( err != 0 ) {
vc sockets编程  
return;
vc sockets编程 }

vc sockets编程 
vc sockets编程 
if ( LOBYTE( wsaData.wVersion ) != 1 ||
vc sockets编程        HIBYTE( wsaData.wVersion ) 
!= 1 ) {
vc sockets编程  WSACleanup( );
vc sockets编程  
return;
vc sockets编程 }

vc sockets编程 SOCKET sockClient
=socket(AF_INET,SOCK_STREAM,0);
vc sockets编程 
vc sockets编程 SOCKADDR_IN addrSrv;
vc sockets编程 addrSrv.sin_addr.S_un.S_addr
=inet_addr("127.0.0.1");
vc sockets编程 addrSrv.sin_family
=AF_INET;
vc sockets编程 addrSrv.sin_port
=htons(6000);
vc sockets编程 connect(sockClient,(SOCKADDR
*)&addrSrv,sizeof(SOCKADDR));
vc sockets编程 send(sockClient,
"hello",strlen("hello")+1,0);
vc sockets编程 
char recvBuf[50];
vc sockets编程 recv(sockClient,recvBuf,
50,0);
vc sockets编程 printf(
"%s\n",recvBuf);
vc sockets编程 
vc sockets编程 closesocket(sockClient);
vc sockets编程 WSACleanup();
vc sockets编程}
(转)

你可能感兴趣的:(socket)