您的当前位置:首页正文

孙鑫VC++视频教程笔记(十四)网络编程(转载)

2024-11-30 来源:个人技术集锦
 

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

SOCKET sockSrv=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(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));

SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
char recvBuf[100];

recvfrom(sockSrv,recvBuf,100,0,(SOCKADDR*)&addrClient,&len);
printf("%s/n",recvBuf);
closesocket(sockSrv);
WSACleanup();
}

客户端代码:
#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 sockClient=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);

sendto(sockClient,"Hello",strlen("Hello")+1,0,
   (SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
closesocket(sockClient);
WSACleanup();
}
===========下面是字符界面下的一个简单UDP聊天程序=====
服务器端:==============================
#include <stdio.h>
#include <WINSOCK2.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 sockSrv=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(6101);

bind(sockSrv,(sockaddr*)&addrSrv,sizeof(sockaddr));

char sendBuf[100];
char recvBuf[100];
char tempBuf[100];
int len=sizeof(sockaddr);
     SOCKADDR_IN addrClient;
while(1)
{
  
   recvfrom(sockSrv,tempBuf,strlen(tempBuf),0,(sockaddr*)&addrClient,&len);
   if('q'!=tempBuf[0])
   {
    sprintf(recvBuf,"%s say: %s",inet_ntoa(addrClient.sin_addr),tempBuf);
       printf("%s/n",recvBuf);
       printf("please input your data: ");
       gets(sendBuf);
       sendto(sockSrv,sendBuf,strlen(sendBuf)+1,0,(sockaddr*)&addrClient,len);
   }
   else
   {
    printf("%s request to quit the chat platform./n",
     inet_ntoa(addrClient.sin_addr));
    sendto(sockSrv,"q",strlen("q")+1,0,(sockaddr*)&addrClient,len);
    break;
   }
}
closesocket(sockSrv);
     WSACleanup();

}

客户端:===============================
#include <stdio.h>
#include <WINSOCK2.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 sockClient=socket(AF_INET,SOCK_DGRAM,0);

SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
//htonl htons inet_addr inet_ntoa
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6101);

char sendBuf[100];
char tempBuf[100];
char recvBuf[100];
int   len=sizeof(sockaddr);

while(1)
{
   printf("please input your data: ");
   gets(sendBuf);
   sendto(sockClient,sendBuf,strlen(sendBuf)+1,0,(sockaddr*)&addrSrv,len);
   recvfrom(sockClient,tempBuf,strlen(tempBuf),0,(sockaddr*)&addrSrv,&len);
   if('q'!=tempBuf[0])
   {
    sprintf(recvBuf,"%s say: %s",inet_ntoa(addrSrv.sin_addr),tempBuf);
    printf("%s/n",recvBuf);
   }
   else
   {
    printf("the server has been closed!/n");
    sendto(sockClient,"q",strlen("q")+1,0,(sockaddr*)&addrSrv,len);
    break;
   }

}

closesocket(sockClient);
WSACleanup();

显示全文