作业
1> 将TCP和UDP通信模型自己实现一遍
//客户端
#include<myhead.h>
#define USER_PORT 9002
#define USER_IP "192.168.125.39"
#define SERVER_PORT 8001
#define SERVER_IP "192.168.125.39"
int main(int argc, const char *argv[])
{
//socket()
int sock_fd=-1;
if((sock_fd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket error");
return -1;
}
//端口号快速重用
int reuse = 1;
if(setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))==-1)
{
perror("setsockopt error");
return -1;
} //(bind())
struct sockaddr_in user_info;
user_info.sin_family=AF_INET;
user_info.sin_port= htons(USER_PORT);
user_info.sin_addr.s_addr=inet_addr(USER_IP);
if(bind(sock_fd,(struct sockaddr*)&user_info,sizeof(user_info))==-1)
{
perror("bind error");
return -1;
}
//connect()
struct sockaddr_in server_info;
server_info.sin_family=AF_INET;
server_info.sin_port= htons(SERVER_PORT);
server_info.sin_addr.s_addr=inet_addr(SERVER_IP);
if(connect(sock_fd,(struct sockaddr*)&server_info,sizeof(server_info))==-1)
{
perror("connect error");
return -1;
}
printf("[%s:%d]->user:sock_fd%d 连接服务器成功",USER_IP,USER_PORT,sock_fd);
//recv/send/read/write()
char buf[128]="";
while(1)
{
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(strcmp(buf,"quit")==0)
{
strcpy(buf,"quit");
send(sock_fd,buf,sizeof(buf),0);
break;
}
send(sock_fd,buf,sizeof(buf),0);
}
//close()
close(sock_fd);
return 0;
}
//服务器
#include<myhead.h>
#define POST 8001
#define IP "192.168.125.39"
int main(int argc, const char *argv[])
{
//1.创建套接字
int sfd=-1;
//功能创建一个支持TCP通信的套接字
//AF_INET:表示跨主机的IPV4的通信
//SOCK_STREAM:表示支持tcp通信
if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket error");
return -1;
}
printf("sfd=%d\n",sfd);
//将端口号快速重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))==-1)
{
perror("setsockopt error");
return -1;
}
//2.绑定地址信息结构体(服务器必须要)
//填充地址信息结构体
struct sockaddr_in sin;
sin.sin_family= AF_INET;
sin.sin_port= htons(POST);
sin.sin_addr.s_addr=inet_addr(IP);
//绑定工作
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success _%d_%s_%s\n",__LINE__, __FILE__ ,__func__);
//3.将套接字设置成被动监听状态
if(listen(sfd,128)==-1)
{
perror("listen error");
return -1;
}
printf("listen success _%d_%s_%s\n",__LINE__, __FILE__ ,__func__);
//4.接受链接请求
//不接受客户端的连接信息
/* if(accept(sfd,NULL,NULL)==-1)
{
perror("accept error");
return -1;
}
printf("accept success _%d_%s_%s\n",__LINE__, __FILE__ ,__func__);
*/
//接受客户端的地址信息的结构体变量
struct sockaddr_in cin;
int newfd=-1;
socklen_t socklen=sizeof(cin);//接受长度
if((newfd=(accept(sfd,(struct sockaddr*)&cin,&socklen)))==-1)
{
perror("accept error");
return -1;
}
printf("[%s:%d] 连接成功,newfd=%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
//收发数据
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
// int res=read(newfd,rbuf,sizeof(rbuf));
int res=recv(newfd,rbuf,sizeof(rbuf),0);
if(res==0)
{
printf("客户端下线\n");
break;
}
printf("[%s:%d] :%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);
//数据发送到客户端
strcat(rbuf,"*_*");
// write(newfd,rbuf,sizeof(rbuf));
send(newfd,rbuf,sizeof(rbuf),0);
printf("发送成功\n");
}
//关闭文件
close(newfd);
close(sfd);
return 0;
}
?
?
//服务器
#include<myhead.h>
#define SERVE_IP "192.168.125.39"
#define SERVE_PORT 8888
int main(int argc, const char *argv[])
{
//1.socket()
int serve_fd=-1;
if((serve_fd=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
perror("socker error");
return -1;
}
//端口号快速重用
int reuse = 1;
if(setsockopt(serve_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))==-1)
{
perror("setsockopt error");
return -1;
} //2.bind()
struct sockaddr_in serve_info;
serve_info.sin_family=AF_INET;
serve_info.sin_port=htons(SERVE_PORT);
serve_info.sin_addr.s_addr=inet_addr(SERVE_IP);
if(bind(serve_fd,(struct sockaddr*)&serve_info,sizeof(serve_info))==-1)
{
perror("bind error");
return -1;
}
printf("[%s:%d]->serve_fd=%d 绑定成功\n",SERVE_IP,SERVE_PORT,serve_fd);
//3.recvfrom/sendto()
char buf[128]="";
struct sockaddr_in user_info;
socklen_t user_infolen=sizeof(user_info);
while(1)
{
bzero(buf,sizeof(buf));
recvfrom(serve_fd,buf,sizeof(buf),0,(struct sockaddr*)&user_info,&user_infolen);
printf("[%s:%d]->%s\n",SERVE_IP,SERVE_PORT,buf);
strcat(buf,">>>>>>>");
sendto(serve_fd,buf,sizeof(buf),0,(struct sockaddr*)&user_info,user_infolen);
}
//4.close()
close(serve_fd);
return 0;
}
//客户端
#include<myhead.h>
#define USER_IP "192.168.125.39"
#define USER_PORT 9999
#define SERVE_IP "192.168.125.39"
#define SERVE_PORT 8888
int main(int argc, const char *argv[])
{
//1.socket()
int user_fd=-1;
if((user_fd=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
perror("socker error");
return -1;
}
//端口号快速重用
int reuse = 1;
if(setsockopt(user_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))==-1)
{
perror("setsockopt error");
return -1;
} //2.bind()
struct sockaddr_in user_info;
user_info.sin_family=AF_INET;
user_info.sin_port=htons(USER_PORT);
user_info.sin_addr.s_addr=inet_addr(USER_IP);
if(bind(user_fd,(struct sockaddr*)&user_info,sizeof(user_info))==-1)
{
perror("bind error");
return -1;
}
printf("[%s:%d]->user_fd=%d 绑定成功\n",USER_IP,USER_PORT,user_fd);
//3.recvfrom/sendto()
char buf[128]="";
struct sockaddr_in serve_info;
serve_info.sin_family=AF_INET;
serve_info.sin_port=htons(SERVE_PORT);
serve_info.sin_addr.s_addr=inet_addr(SERVE_IP);
socklen_t serve_infolen=sizeof(serve_info);
while(1)
{
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
sendto(user_fd,buf,sizeof(buf),0,(struct sockaddr*)&serve_info,serve_infolen);
recvfrom(user_fd,buf,sizeof(buf),0,NULL,NULL);
printf("[%s:%d]->%s\n",USER_IP,USER_PORT,buf);
}
//4.close()
close(user_fd);
return 0;
}