作业1:TCP模型
服务器端:
#include <myhead.h>
#define SER_IP "192.168.199.129"
#define SER_PORT 8899int main(int argc, const char *argv[])
{//1.创建用于连接的套接字文件int sfd=socket(AF_INET,SOCK_STREAM,0);if(sfd==-1){perror("socket error");return -1;}//2.绑定端口号和IP地址//填充地址信息结构体struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(SER_PORT);sin.sin_addr.s_addr = inet_addr(SER_IP);if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin))==-1){perror("bind error");return -1;}printf("bind success\n");//3.启动监听if(listen(sfd,128)==-1){perror("listen error");return -1;}printf("listen success\n");//4.接受请求struct sockaddr_in cin;socklen_t socklen=sizeof(cin);//接收请求后创建用于通信的套接字int newfd=accept(sfd,(struct sockaddr *)&cin,&socklen);if(newfd==-1){perror("accept error");return -1;}printf("newfd=%d 您有新的客户端已上线\n",newfd);//5.数据收发char sbuf[128]="";while(1){//清空数组bzero(sbuf,sizeof(sbuf));//接收客户端发来的消息int res=recv(newfd,sbuf,sizeof(sbuf),0);if(res==0){break;}//回复客户端发来的消息strcat(sbuf," !!!");send(newfd,sbuf,strlen(sbuf),0);}//6.关闭套接字文件close(newfd);close(sfd);return 0;
}
客户端:
#include <myhead.h>
#define SER_IP "192.168.199.129"
#define SER_PORT 8899int main(int argc, const char *argv[])
{//1.创建用于通信的套接字int cfd=socket(AF_INET,SOCK_STREAM,0);if(cfd==-1){perror("socket error");return -1;}//2.连接服务器//填充地址信息结构体struct sockaddr_in sin;sin.sin_family=AF_INET;sin.sin_port=htons(SER_PORT);sin.sin_addr.s_addr=inet_addr(SER_IP);//发出连接请求if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1){perror("connect error");return -1;}//3.进行数据的收发char wbuf[128]="";while(1){//清空数组bzero(wbuf,sizeof(wbuf));//从终端写入数据printf("请输入>>>");fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1]='\0';//将数据发送到服务端send(cfd,wbuf,strlen(wbuf),0);printf("发送成功\n");if(strcmp(wbuf,"quit")==0){break;}//接收服务端发送来的消息//清空数组bzero(wbuf,sizeof(wbuf));recv(cfd,wbuf,sizeof(wbuf),0);printf("接收到的数据为:%s\n",wbuf);}//4.关闭套接字close(cfd);return 0;
}
效果图:
作业2:UDP模型
服务器端:
#include <myhead.h>
#define SER_IP "192.168.199.129"
#define SER_PORT 9988
int main(int argc, const char *argv[])
{//1.创建用于通信的套接字int sfd=socket(AF_INET,SOCK_DGRAM,0);if(sfd==-1){perror("socket error");return -1;}printf("socket success\n");//2.绑定端口号//填充地址信息结构体struct sockaddr_in sin;sin.sin_family=AF_INET;sin.sin_port=htons(SER_PORT);sin.sin_addr.s_addr=inet_addr(SER_IP);if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1){perror("bind error");return -1;}printf("bind success\n");//3.进行数据收发//定义客户端地址信息结构体struct sockaddr_in cin;socklen_t socklen=sizeof(cin);char rbuf[128]="";while(1){//清空数组bzero(rbuf,sizeof(rbuf));//读取客户端信息recvfrom(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&cin,&socklen);printf("收到的消息为:%s\n",rbuf);if(strcmp(rbuf,"quit")==0){break;}//向客户端回复消息strcat(rbuf," !!!");sendto(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&cin,socklen);}//4.关闭套接字close(sfd);return 0;
}
客户端:
#include <myhead.h>
#define SER_IP "192.168.199.129"
#define SER_PORT 9988
int main(int argc, const char *argv[])
{//1.创建用于通信的套接字int cfd=socket(AF_INET,SOCK_DGRAM,0);if(cfd==-1){perror("socket error");return -1;}//数据收发char wbuf[128]="";//填充地址信息结构体struct sockaddr_in sin;socklen_t socklen=sizeof(sin);sin.sin_family=AF_INET;sin.sin_port=htons(SER_PORT);sin.sin_addr.s_addr=inet_addr(SER_IP);while(1){//清空数组bzero(wbuf,sizeof(wbuf));//从终端获取数据printf("请输入>>>");fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1]='\0';//将数据发送到服务端sendto(cfd,wbuf,sizeof(wbuf),0,(struct sockaddr*)&sin,socklen);printf("发送成功\n");if(strcmp(wbuf,"quit")==0){break;}//接收从服务端回复的消息recvfrom(cfd,wbuf,sizeof(wbuf),0,(struct sockaddr*)&sin,&socklen);printf("接收到的消息为:%s\n",wbuf);}//关闭套接字close(cfd);return 0;
}
效果图: