基于TCP的网络应用程序的编写
1.服务器端程序
#include <Winsock2.h>
#include <stdio.h>
#pragma comment(lib,"Ws2_32.lib")
void main()
{
//加载套接字库
WORD wVersionRequested; //加载的Winsock库的版本
WSADATA wsaData; //库版本的有关信息
int err;
wVersionRequested = MAKEWORD( 1, 1 ); //用 MAKEWORD 宏请求一个1.1版本的WinSock库
err = WSAStartup( wVersionRequested, &wsaData );//用WSAStartup()来加载套接字库
if( err != 0) //不等于 0 表示加载套接字库不成功
return;
if( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 )//如果低位字节不等于1 或 高位字节不等于 1则
{
WSACleanup();//释放为该应用程序分配的资源,终止对WinSock 动态库的使用
return;
}
//创建用于监听的套接字
SOCKET sockSrv = socket( AF_INET, SOCK_STREAM, 0 );
SOCKADDR_IN addrSrv; //地址结构体
//用htonl()将主机字节序转换为网络字节序,INADDR_ANY表示允许套接字向任何分配给本地机器的IP地址发送或接收数据
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET; //地址家族,对于TCP/IP协议的地址族, 只能是AF_INET 或PF_INET
addrSrv.sin_port = htons(6000);//指定端口号的时候 必须知道1024 以上的值
//绑定套接字
bind( sockSrv, (sockaddr *)&addrSrv, sizeof(sockaddr) );
//将套接字设置为监听模式,准备接收客户请求
listen( sockSrv, 5 );//等待队列为5
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
while(1)
{
//接受客户请求
SOCKET sockConn = accept( sockSrv, (sockaddr *)&addrClient, &len );
char sendBuf[100];
sprintf( sendBuf, "Hello!");
//发送数据
send( sockConn, sendBuf, strlen(sendBuf) + 1, 0);
char recvBuf[100];
//接收数据
recv( sockConn, recvBuf, 100,0 );
//打印接收的数据
printf("%s\n",recvBuf);
//关闭套接字
closesocket(sockConn);
}
}
2.客户端程序代码
#include <Winsock2.h>
#include <stdio.h>
#pragma comment(lib,"Ws2_32.lib")
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_STREAM, 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);
//向服务器发出连接请求
connect( sockClient, (sockaddr *)&addrSrv, sizeof( sockaddr ) );
//接收数据
char recvBuf[100];
recv( sockClient, recvBuf, 100, 0 );
printf( "%s\n", recvBuf );
//发送数据
send( sockClient, "I'm ZhaoLong!",sizeof( "I'm ZhaoLong!" ) + 1,0 );
//关闭套接字
closesocket(sockClient);
WSACleanup();
}
3.运行结果
基于UDP的网络应用程序的编写
1.服务器端程序
#include <Winsock2.h>
#include <stdio.h>
#pragma comment(lib,"Ws2_32.lib")
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( 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();
}
2.客户端程序
#include <Winsock2.h>
#include <stdio.h>
#pragma comment(lib,"Ws2_32.lib")
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 socketClient = 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( socketClient, "Hello!",strlen("Hello!" )+ 1, 0,
(SOCKADDR *)&addrSrv, sizeof( SOCKADDR ) );
//关闭套接字
closesocket(socketClient);
WSACleanup();
}
3.运行结果