1,通信
(1)身份:IP
(2)规则:协议
(3)分机:端口号
2,UDP,无需确认,实时性高.例如:视频会议,视频点播.
3,网络字节顺序: 网络高位先存.
intel:低位先存.
4,网间进程通信完全是异步的.
给出两个实例:(注意工程设置,需要加载库文件ws2_32.lib)
(1)基于TCP可靠连接的SOCKET.
服务器端:
#include <stdio.h>
#include <WINSOCK2.H>
int main()
{
WORD wVersionRequested;//版本号
WSADATA wsaData; //记录套接字的详细信息
int err;
wVersionRequested = MAKEWORD( 1, 1 );//宏,返回版本号
err = WSAStartup( wVersionRequested, &wsaData );//加载套接字库
//initiates use of the Winsock DLL by a process.
//成功返回0.
if ( err != 0 ) //表示有错误发生.
{
return 1;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup( );//有加载,就要有对应的释放.
return 1;
}
SOCKET sockSvrver=socket(AF_INET,SOCK_STREAM,0);//创建套接字
if(sockSvrver==INVALID_SOCKET)
{
printf("套接字创建失败.");
return 1;
}
SOCKADDR_IN addServer; //定义地址结构体变量
addServer.sin_addr.S_un.S_addr=htonl(INADDR_ANY);//u_long类型转换为网络字节序 htons u_short类型
addServer.sin_family=AF_INET;
addServer.sin_port=htons(6000);
int re_bind;
re_bind=bind(sockSvrver,(SOCKADDR*)&addServer,sizeof(SOCKADDR)); //绑定
if(re_bind==SOCKET_ERROR)
{
closesocket(sockSvrver);
printf("套接字绑定失败.");
return 1;
}
listen(sockSvrver,5);//等待队列的最大成员数
SOCKADDR_IN addrClient; //接受变量
int len=sizeof(SOCKADDR);//接收变量的长度
char recvBuf[100];
char sendBuf[100];
char tempBuf[100];
SOCKET sockConnect=accept(sockSvrver,(SOCKADDR*)&addrClient,&len);
while(1)
{
recv(sockConnect,recvBuf,100,0);
sprintf(tempBuf,"%s say: %s", inet_ntoa(addrClient.sin_addr),recvBuf);
printf("%s\n",tempBuf);
if('q'==recvBuf[0])//表示客户端想要退出
{
send(sockConnect,"q",strlen("q")+1,0);
printf("Chat end!\n");
break;
}
printf("Please input data:\n");
gets(sendBuf);//可以得到一行数据
send(sockConnect,sendBuf,strlen(sendBuf)+1,0);
}
closesocket(sockConnect); //关闭套接字
WSACleanup();
//如果不是死循环,需要关闭监听套接字
//并调用WSACleanup()关闭套接字库
return 0;
}
客户端:
#include <stdio.h>
#include <WINSOCK2.H>
int main()
{
WORD wVersionRequested;//版本号
WSADATA wsaData; //记录套接字的详细信息
int err;
wVersionRequested = MAKEWORD( 1, 1 );//宏,返回版本号
err = WSAStartup( wVersionRequested, &wsaData );//加载套接字库
//initiates use of the Winsock DLL by a process.
//成功返回0.
if ( err != 0 ) //表示有错误发生.
{
return 1;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup( );//有加载,就要有对应的释放.
return 1;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);//创建套接字
SOCKADDR_IN addrServer;
addrServer.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");//本地环路地址
addrServer.sin_port=htons(6000);
addrServer.sin_family=AF_INET;
connect(sockClient,(SOCKADDR*)&addrServer,sizeof(SOCKADDR));
char recvBuf[100];
char sendBuf[100];
char tempBuf[100];
while(1)
{
printf("Please input data:\n");
gets(sendBuf);//可以得到一行数据
send(sockClient,sendBuf,strlen(sendBuf)+1,0);
recv(sockClient,recvBuf,100,0);
sprintf(tempBuf,"server say: %s",recvBuf);
printf("%s\n",tempBuf);
if('q'==recvBuf[0])//表示客户端想要退出
{
send(sockClient,"q",strlen("q")+1,0);
printf("Chat end!\n");
break;
}
}
//5.终止对套接字库的使用
closesocket(sockClient); //关闭套接字
WSACleanup();
return 0;
}
(2)基于UDP连接的SOCKET.
服务器端:
//编写一个基于UDP的聊天工具
//发送端代码
#include <Winsock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );//版本号1.1
//1.加载套接字库
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return;
}
//判断是否我们请求的winsocket版本,如果不是
//则调用WSACleanup终止winsocket的使用并返回
if ( LOBYTE( wsaData.wVersion ) != 1 ||HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup( );
return;
}
//2.创建套接字
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));
char recvBuf[100];
char sendBuf[100];
char tempBuf[100];
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
while(1)
{
recvfrom(sockSrv,recvBuf,100,0,(SOCKADDR*)&addrClient,&len);
if('q'==recvBuf[0])//表示客户端想要退出
{
sendto(sockSrv,"q",strlen("q")+1,0,(SOCKADDR*)&addrClient,len);
printf("Chat end!\n");
break;
}
sprintf(tempBuf,"%s say: %s", inet_ntoa(addrClient.sin_addr),recvBuf);
printf("%s\n",tempBuf);
printf("Please input data:\n");
gets(sendBuf);//可以得到一行数据
sendto (sockSrv,sendBuf,strlen(sendBuf)+1,0,
(SOCKADDR*)&addrClient,len);//发送数据
}
closesocket(sockSrv);
WSACleanup();
}
客户端:
//发送端代码
#include <Winsock2.h>
#include <stdio.h>
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );//版本号1.1
//1.加载套接字库
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return;
}
//判断是否我们请求的winsocket版本,如果不是
//则调用WSACleanup终止winsocket的使用并返回
if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 )
{
WSACleanup( );
return;
}
//2.创建套接字
SOCKET sockClient = socket(AF_INET,SOCK_DGRAM,0);
//3.发送消息
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);
while (1)
{
printf("Please input data:\n");
gets(sendBuf);
sendto(sockClient,sendBuf,strlen(sendBuf)+1,0,(SOCKADDR*)&addrSrv,len);
recvfrom(sockClient,recvBuf,100,0,(SOCKADDR*) &addrSrv,&len);
if('q' ==recvBuf[0])
{
sendto(sockClient,"q",strlen("q")+1,0,(SOCKADDR*) &addrSrv,len);
printf("chat end!\n");
break;
}
sprintf(tempBuf,"%s say: %s",inet_ntoa(addrSrv.sin_addr),recvBuf);
printf("%s\n",tempBuf);
}
closesocket(sockClient);
WSACleanup();
}