[作业]
任务1:完成select的服务器和poll的客户端
任务2:完成select的客户端
任务3:尝试完成基于TCP的文件传输项目
select服务器:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#define ERR_SEG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.8.82"
int main(int argc, const char *argv[])
{
//创建套接字
int sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0){
ERR_SEG("socket");
return -1;
}
//服务器IP以及端口信息
struct sockaddr_in server;
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr=inet_addr(IP);
//绑定服务器
if(bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0){
ERR_SEG("bind");
return -1;
}
//将套接字设置成被动监听状态
if(listen(sockfd,128)<0){
ERR_SEG("listen");
return -1;
}
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
FD_SET(0,&readfds);
FD_SET(sockfd,&readfds);
int maxfd=sockfd;
int sockfd1,temp;
char buf[128]="";
ssize_t res=0;
struct sockaddr_in cin;
socklen_t len=sizeof(cin);
while(1){
tempfds=readfds;
if(select(maxfd+1,&tempfds,NULL,NULL,NULL)<0){
ERR_SEG("select");
return -1;
}
for(int i=0;i<=maxfd;i++){
if(FD_ISSET(i,&tempfds)==0)
continue;
if(0==i){
//终端获取
bzero(buf,sizeof(buf));
scanf("%s",buf);
printf("%s\n",buf);
}
else if(sockfd==i){
//生成新的文件描述符用于通信
sockfd1=accept(sockfd,(struct sockaddr*)&cin,&len);
if(sockfd1<0){
ERR_SEG("accept");
return -1;
}
printf("[%s:%d] newfd=%d 服务器已连接...\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),sockfd1);
FD_SET(sockfd1,&readfds);
maxfd=maxfd>sockfd1?maxfd:sockfd1;
}
else{
bzero(buf,sizeof(buf));
res=recv(sockfd1,buf,sizeof(buf),0);
if(res<0){
ERR_SEG("recv");
return -1;
}
else if(0==res){
printf("客户端断开...\n");
close(i);
FD_CLR(i,&readfds);
int j;
for(j=maxfd;j>0;j--)
if(FD_ISSET(j,&readfds)){
maxfd=j;
break;
}
}
else
printf("[%s:%d] newfd=%d:%s\n",inet_ntoa(cin.sin_addr),\
ntohs(cin.sin_port),i,buf);
}
}
}
close(sockfd);
close(sockfd1);
return 0;
}
select客户端:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#define ERR_SEG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.8.82"
int main(int argc, const char *argv[])
{
//创建套接字
int sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0){
ERR_SEG("socket");
return -1;
}
//服务器IP以及端口信息
struct sockaddr_in server;
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr=inet_addr(IP);
if(connect(sockfd,(struct sockaddr*)&server,sizeof(server))<0){
ERR_SEG("connect");
return -1;
}
printf("客户端已连接...\n");
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(0,&readfds);
FD_SET(sockfd,&readfds);
int maxfd=sockfd;
ssize_t res;
char buf[128];
while(1){
if(select(maxfd+1,&readfds,NULL,NULL,NULL)<0){
ERR_SEG("select");
return -1;
}
if(FD_ISSET(0,&readfds)){
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
if(send(sockfd,buf,sizeof(buf),0)<0){
ERR_SEG("send");
return -1;
}
}
if(FD_ISSET(sockfd,&readfds)){
res=recv(sockfd,buf,sizeof(buf),0);
if(res<0){
ERR_SEG("recv");
return -1;
}
else if(0==res){
printf("服务器已断开\n");
return -1;
}
printf("%s\n",buf);
}
}
close(sockfd);
return 0;
}
poll客户端:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#define ERR_SEG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.8.82"
int main(int argc, const char *argv[])
{
//创建套接字
int sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0){
ERR_SEG("socket");
return -1;
}
//服务器IP以及端口信息
struct sockaddr_in server;
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr=inet_addr(IP);
if(connect(sockfd,(struct sockaddr*)&server,sizeof(server))<0){
ERR_SEG("connect");
return -1;
}
printf("客户端已连接...\n");
struct pollfd fds[2];
fds[0].fd=0;
fds[0].events=POLLIN;
fds[1].fd=sockfd;
fds[1].events=POLLIN;
ssize_t res;
int num;
char buf[128];
while(1){
num=poll(fds,2,-1);
if(num<0){
ERR_SEG("poll");
return -1;
}
else if(0==num){
printf("time out...\n");
break;
}
if(fds[0].revents&POLLIN){
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
if(send(sockfd,buf,sizeof(buf),0)<0){
ERR_SEG("send");
return -1;
}
}
if(fds[1].revents&POLLIN){
res=recv(sockfd,buf,sizeof(buf),0);
if(res<0){
ERR_SEG("recv");
return -1;
}
else if(0==res){
printf("服务器已断开\n");
return -1;
}
printf("%s\n",buf);
}
}
close(sockfd);
return 0;
}