作业
1、将TCP的CS模型再敲一遍
服务器
#include <myhead.h>
#define PORT 8888
#define IP "192.168.124.123"
int main(int argc, const char *argv[])
{
//创建套接字
//绑定本机IP和端口号
//监听客户端请求
//接收客户端连接请求
//收发消息
//创建套接字
int oldfd ;
if((oldfd = socket(AF_INET,SOCK_STREAM,0))==-1) //创建套接字
{
perror("socket");
return -1;
}
//端口号快速复用
int n = 2;
if(setsockopt(oldfd,SOL_SOCKET,SO_REUSEADDR,&n,sizeof(n))==-1)
{
perror("setsockopt");
return -1;
}
//绑定本机IP和端口号
struct sockaddr_in server = {
.sin_family = AF_INET,
.sin_port = htons(PORT),
.sin_addr.s_addr = inet_addr(IP)
};
if(bind(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("bind");
return -1;
}
//监听客户端连接请求,client变量接收客户端信息
struct sockaddr_in client;
int client_len = sizeof(client);
int newfd;
if(newfd = accept(oldfd,(struct sockaddr *)&client,&client_len)==-1)
{
perror("accept");
return -1;
}
printf("%s:%d连接\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));
//收发消息
char buff[1024];
while(1)
{
int res = recv(newfd,buff,sizeof(buff),0);
if(res == 0)
{
printf("客户端退出\n");
break;
}
printf("收到消息:%s,收到消息的长度:%d\n",buff,res);
strcat(buff,"霜雪");
send(newfd,buff,sizeof(buff),0);
bzero(buff,sizeof(buff));
}
close(newfd);
close(oldfd);
return 0;
}
客户端
#include <myhead.h>
#define IP "192.168.124.123"
#define SERPORT 9999
int main(int argc, const char *argv[])
{
//1、创建套接字
//2、绑定(不是必须绑定)
//3、连接
//4、收发消息
int oldfd = socket(AF_INET,SOCK_STREAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
#if 0
//绑定固定的IP和端口号(不是必须的)
struct sockaddr_in client = {
.sin_family =AF_INET,
.sin_port = htons(7899),//自定义端口号
.sin_addr.s_addr = inet_addr("192.168.124.34")
};
if(bind(oldfd,(struct sockaddr *)&client,sizeof(client))==-1)
{
perror("bind");
return -1;
}
#endif
//连接服务器
struct sockaddr_in server = {
.sin_family =AF_INET,
.sin_port = htons(SERPORT),//注意端口号需要服务器端口
.sin_addr.s_addr = inet_addr(IP)
};
if(connect(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("connect");
return -1;
}
//收发消息
char buff[1024];
while(1)
{
fgets(buff,sizeof(buff),stdin);
buff[strlen(buff)-1] = '\0';
send(oldfd,buff,sizeof(buff),0);
if(strcmp(buff,"quit")==0)//退出客户端
{
break;
}
bzero(buff,sizeof(buff));
recv(oldfd,buff,sizeof(buff),0);//阻塞接收服务器消息
printf("服务器发来消息:%s\n",buff);
}
close(oldfd);
return 0;
}
2、UDP服务器中,使用connect函数,实现唯一的客户端与服务器通话。
服务器
#include <myhead.h>
#define PORT 8888
#define IP "192.168.124.123"
int main(int argc, const char *argv[])
{
//创建套接字
int oldfd = socket(AF_INET,SOCK_DGRAM,0);
if(oldfd == -1)
{
perror("socket");
return -1;
}
//绑定
struct sockaddr_in server = {
.sin_family = AF_INET,
.sin_port = htons(PORT),
.sin_addr.s_addr = inet_addr(IP)
};
if(bind (oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("bind");
return -1;
}
struct sockaddr_in client;
int client_len = sizeof(client);
char buff[1024];
int flag = 0;
while(1)
{
recvfrom(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&client,&client_len);
printf("%s:%d信息:%s\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port),buff);
if(connect(oldfd,(struct sockaddr *)&client,sizeof(client))==-1)
{
perror("connect");
return -1;
}
while(1)
{
int res = recvfrom(oldfd,buff,sizeof(buff),0,NULL,NULL);
printf("%s\n",buff);
strcat(buff,"周日还要上课,吴!!!!");
sendto(oldfd,buff,sizeof(buff),0,NULL,0);
bzero(buff,sizeof(buff));
if(res == 0)
{
printf("退出客户端\n");
break;
}
}
}
return 0;
}
客户端
#include <myhead.h>
#define IP "192.168.124.123"
#define PORT 8888
int main(int argc, const char *argv[])
{
//1、创建套接字
int oldfd = socket(AF_INET,SOCK_DGRAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//2、收发消息
struct sockaddr_in server = {
.sin_family = AF_INET,
.sin_port = htons(PORT),
.sin_addr.s_addr = inet_addr(IP)
};
if(connect(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("connect");
return -1;
}
char buff[1024];
while(1)
{
fgets(buff,sizeof(buff),stdin);
buff[strlen(buff)-1] = '\0';
sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&server,sizeof(server));
bzero(buff,sizeof(buff));
recvfrom(oldfd,buff,sizeof(buff),0,NULL,NULL);
printf("接收服务器信息:%s\n",buff);
}
return 0;
}
笔记整理
流程图
UDP服务器
1、创建套接字。
2、绑定本机IP和端口号。
3、收发消息,由于不知道对方是谁,对方也不知道您是谁,所以在发送时附带自己的信息,接收时接收对方信息。
UDP相关API
#include <sys/types.h>
#include <sys/socket.h>
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);
功能:发送信息函数
参数1:套接字
参数2:发送的信息
参数3:信息大小
参数4: 0:阻塞接收
MSG_DONTWAIT:费阻塞接收
参数5:填写发送目标的IP和端口号 参数6:参数5的大小。
返回值:成功返回发送的字节个数,失败返回-1,并置位错误码。
#include <sys/types.h>
#include <sys/socket.h>
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
功能:接收信息函数
参数1:套接字
参数2:发送的信息
参数3:信息大小
参数4: 0:阻塞接收
MSG_DONTWAIT:费阻塞接收
参数5:自动填充发信息的主机信息。
返回值:成功返回发送的字节个数,失败返回-1,并置位错误码。
、UDP服务器
#include <myhead.h>
#define PORT 8888
#define IP "192.168.124.34"
int main(int argc, const char *argv[])
{
//1、创建UDP套接字
int oldfd = socket(AF_INET,SOCK_DGRAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//2、绑定
struct sockaddr_in server = {
.sin_family = AF_INET,
.sin_port = htons(PORT),
.sin_addr .s_addr = inet_addr(IP)
};
if(bind(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("bind");
return -1;
}
//收发消息
struct sockaddr_in client;
int client_len = sizeof(client);
char buff[1024];
while(1)
{
//接收客户端消息时会将客户端信息写入client
recvfrom(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&client,&client_len);
printf("收到%s:%d的消息:%s\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port),buff);
strcat(buff,"元旦不放假难受");
sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&client,sizeof(client));//收到谁的信息就发给谁
}
return 0;
}
4、UDP客户端
1、创建套接字
2、收发消息
#include <myhead.h>
#define IP "192.168.124.34"
#define PORT 8888
int main(int argc, const char *argv[])
{
//1、创建套接字
int oldfd = socket(AF_INET,SOCK_DGRAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//2、收发消息
struct sockaddr_in server = {
.sin_family = AF_INET,
.sin_port = htons(PORT),
.sin_addr.s_addr = inet_addr(IP)
};
char buff[1024];
while(1)
{
fgets(buff,sizeof(buff),stdin);
buff[strlen(buff)-1] = '\0';
sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&server,sizeof(server));
bzero(buff,sizeof(buff));
recvfrom(oldfd,buff,sizeof(buff),0,NULL,NULL);
printf("接收服务器信息:%s\n",buff);
}
return 0;
}
思维导图