孙鑫VC学习(第14课网络编程,TCP/IP,UDP)

孙鑫VC学习(第14课网络编程,TCP/IP,UDP)_第1张图片

孙鑫教程14课网络编程

使用TCP/IP协议的服务器端程序:

WIN32 控制台程序

#include <Winsock2.h>
#include <stdio.h>
void main()
{
 WORD wVersionRequested;
 WSADATA wsaData;
 int err;
 
 wVersionRequested = MAKEWORD( 1, 1 );
 
 err = WSAStartup( wVersionRequested, &wsaData );
 if ( err != 0 )
 {
  return;
 }
 
 if ( LOBYTE( wsaData.wVersion ) != 1||
  HIBYTE( wsaData.wVersion ) != 1 )
 {
  WSACleanup( );
  return;
 }

 SOCKET sksrv=socket(AF_INET,SOCK_STREAM,0); 
 SOCKADDR_IN adsrv; 
 adsrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);//网络字节序 
 adsrv.sin_family=AF_INET; 
 adsrv.sin_port=htons(6000); 
 bind(sksrv,(sockaddr*)&adsrv,sizeof(SOCKADDR_IN)); 
 listen(sksrv,5); 
 SOCKADDR_IN addrclient; 
 int addrlen=sizeof(SOCKADDR_IN);

 char sendbuf[100]="服务器发送的数据是我是服务器";;
 char recvbuf[100];
 char tempbuf[200];
 while (true)  
 {  
  SOCKET sockconn=accept(sksrv,(sockaddr*)&addrclient,&addrlen);

  send(sockconn,sendbuf,strlen(sendbuf)+1,0); 
  sprintf(tempbuf,"服务器%s说 :%s",inet_ntoa(adsrv.sin_addr),sendbuf);
  printf("%s/n",tempbuf);
           
  recv(sockconn,recvbuf,100,0);
  sprintf(tempbuf,"服务器%s从%s接收到的数据是:%s",
   inet_ntoa(adsrv.sin_addr),inet_ntoa(addrclient.sin_addr),recvbuf);
  printf("%s/n",tempbuf);  
  closesocket(sockconn);  
 }
}

注:要在工程中的设置的链接中加上Ws2_32.lib

#include <Winsock2.h>
#include <stdio.h>
void main()
{
 WORD wVersionRequested;
 WSADATA wsaData;
 int err;
 
 wVersionRequested = MAKEWORD( 1, 1 );
 
 err = WSAStartup( wVersionRequested, &wsaData );
 if ( err != 0 )
 {
  return;
 }
 
 if ( LOBYTE( wsaData.wVersion ) != 1||
  HIBYTE( wsaData.wVersion ) != 1 )
 {
  WSACleanup( );
  return;
 }
 SOCKET skclient=socket(AF_INET,SOCK_STREAM,0);
 SOCKADDR_IN adsrv;
 adsrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");//在本机上做测试,所以选此地址
 adsrv.sin_family=AF_INET;
 adsrv.sin_port=htons(6000);
 int len=sizeof(SOCKADDR_IN);
 connect(skclient,(SOCKADDR*)&adsrv,len);
//下面这段代码adclient只是为了演示客户端的地址。若不需要显示客户端地址,不需要SOCKADDR_IN adclient。
 SOCKADDR_IN adclient;
 adclient.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");//在本机上做测试,所以选此地址
 adclient.sin_family=AF_INET;
 adclient.sin_port=htons(6000);

 char recvBuf[100];
 char sendBuf[100]="客户端发送的数据是我是客户端";
 char tempBuf[100];
 
 recv(skclient,recvBuf,100,0);
 sprintf(tempBuf,"客户端%s从%s接收的数据是:%s",inet_ntoa(adclient.sin_addr),
  inet_ntoa(adsrv.sin_addr),recvBuf);
 printf("%s/n",tempBuf);
 
 send(skclient,sendBuf,strlen(sendBuf)+1,0);
 sprintf(tempBuf,"客户端%s向%s发送的数据是:%s",inet_ntoa(adclient.sin_addr),
  inet_ntoa(adsrv.sin_addr),sendBuf);
 printf("%s/n",tempBuf);
 
 closesocket(skclient);
 WSACleanup( );  
}

注意,客户端和服务器端的目录应该是平级的。运行的时候,先运行服务器端,再运行客户端。注:要在工程中的设置的链接中加上Ws2_32.lib

 

UDP 的服务器端的程序:

#include <Winsock2.h>

#include <stdio.h>

void main()

{

      

      

       WORD wVersionRequested;

       WSADATA wsaData;

       int err;

      

       wVersionRequested = MAKEWORD( 1, 1 );

      

       err = WSAStartup( wVersionRequested, &wsaData );

       if ( err != 0 )

       {

             

              return;

       }

      

      

       if ( LOBYTE( wsaData.wVersion ) != 1 ||

              HIBYTE( wsaData.wVersion ) != 1 )

       {

              WSACleanup( );

              return;

       }

 

       SOCKET sk=socket(AF_INET,SOCK_DGRAM,0);

       SOCKADDR_IN skaddr;

       skaddr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);

       skaddr.sin_port=htons(6000);

       skaddr.sin_family=AF_INET;

       bind(sk,(SOCKADDR*)&skaddr,sizeof(SOCKADDR_IN));

       SOCKADDR_IN addrclient;

       char recvbuf[100];

       int len=sizeof(SOCKADDR_IN);

       recvfrom(sk,recvbuf,100,0,(SOCKADDR*)&addrclient,&len);

       printf("%s/n",recvbuf);

       closesocket(sk);

       WSACleanup();

}

注:要在工程中的设置的链接中加上Ws2_32.lib

下面是UDP的客户端程序:

注:和服务器端平级。

include <Winsock2.h>

#include <stdio.h>

void main()

{

      

      

       WORD wVersionRequested;

       WSADATA wsaData;

       int err;

      

       wVersionRequested = MAKEWORD( 1, 1 );

      

       err = WSAStartup( wVersionRequested, &wsaData );

       if ( err != 0 )

       {

             

              return;

       }

      

      

       if ( LOBYTE( wsaData.wVersion ) != 1 ||

              HIBYTE( wsaData.wVersion ) != 1 )

       {

              WSACleanup( );

              return;

       }

 

       SOCKET sk=socket(AF_INET,SOCK_DGRAM,0);

       SOCKADDR_IN addrsrv;

       addrsrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");

       addrsrv.sin_family=AF_INET;

       addrsrv.sin_port=htons(6000);

 

       char sendBuf[100];

      

       sendto(sk,"hello",sizeof("hello")+1,0,(SOCKADDR*)&addrsrv,sizeof(SOCKADDR));

       closesocket(sk);

       WSACleanup();

}

下面是基于字符界面的聊天程序:

注:要在工程中的设置的链接中加上Ws2_32.lib

服务器端的应用程序:WIN32控制台的:

#include <Winsock2.h>

#include <stdio.h>

void main()

{

      

      

       WORD wVersionRequested;

       WSADATA wsaData;

       int err;

      

       wVersionRequested = MAKEWORD( 1, 1 );

      

       err = WSAStartup( wVersionRequested, &wsaData );

       if ( err != 0 )

       {

             

              return;

       }

      

      

       if ( LOBYTE( wsaData.wVersion ) != 1 ||

              HIBYTE( wsaData.wVersion ) != 1 )

       {

              WSACleanup( );

              return;

       }

       SOCKET sksrv=socket(AF_INET,SOCK_DGRAM,0);

       SOCKADDR_IN addrsrv;

       addrsrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);

       addrsrv.sin_family=AF_INET;

       addrsrv.sin_port=htons(6000);

 

       bind(sksrv,(SOCKADDR*)&addrsrv,sizeof(SOCKADDR_IN));

       char recvBuf[100];

       char sendBuf[100];

       char tempBuf[200];

       SOCKADDR_IN addrclient;

       int len=sizeof(SOCKADDR_IN);

      

       while (1)

       {

              recvfrom(sksrv,recvBuf,100,0,(SOCKADDR*)&addrclient,&len);

              if ('q'==recvBuf[0])//判断第一个字符是否是'q',其代表着想退出

              {

                     sendto(sksrv,"q",strlen("q")+1,0,(SOCKADDR*)&addrclient,len);

                     printf(tempBuf,"chat end!/n");

                     break;

 

              }

             

              sprintf(tempBuf,"%s say %s",inet_ntoa(addrclient.sin_addr),recvBuf);

              printf(tempBuf);

              printf("please input data:/n");

              gets(sendBuf);

              sendto(sksrv,sendBuf,sizeof(sendBuf),0,(SOCKADDR*)&addrclient,len);

             

       }

       closesocket(sksrv);

       WSACleanup();

}

注:要在工程中的设置的链接中加上Ws2_32.lib

客户端的应用程序:WIN32控制台的:

#include <Winsock2.h>

#include <stdio.h>

void main()

{

      

      

       WORD wVersionRequested;

       WSADATA wsaData;

       int err;

      

       wVersionRequested = MAKEWORD( 1, 1 );

      

       err = WSAStartup( wVersionRequested, &wsaData );

       if ( err != 0 )

       {

             

              return;

       }

      

      

       if ( LOBYTE( wsaData.wVersion ) != 1 ||

              HIBYTE( wsaData.wVersion ) != 1 )

       {

              WSACleanup( );

              return;

       }

 

       SOCKET skclient=socket(AF_INET,SOCK_DGRAM,0);

       SOCKADDR_IN addrsrv;

       addrsrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");

       addrsrv.sin_family=AF_INET;

       addrsrv.sin_port=htons(6000);

 

       char recvBuf[100];

       char sendBuf[100];

       char tempBuf[200];

       int len=sizeof(SOCKADDR_IN);

       while (1)

       {

              printf("please input data/n");

              gets(sendBuf);

              sendto(skclient,sendBuf,sizeof(sendBuf),0,(SOCKADDR*)&addrsrv,len);

              recvfrom(skclient,recvBuf,100,0,(SOCKADDR*)&addrsrv,&len);

                     if ('q'==recvBuf[0])

                     {

                            sendto(skclient,"q",sizeof("q")+1,0,(SOCKADDR*)&addrsrv,len);

                            printf("chat end/n");

                            break;

                     }

              sprintf(tempBuf,"%s say %s",inet_ntoa(addrsrv.sin_addr),recvBuf);

              printf(tempBuf);

       }

}

 

加载套接字库,绑定地址信息,基于TCP,设置监听,用ACCEPT接收客户端请求。对于UDP来说,不需要建立连接,直接接收和发送数据。通过这几个函数就可以完成。

 

你可能感兴趣的:(编程,网络,socket,Stream,服务器)