2020-07-16 select-socket
参考:
https://blog.csdn.net/qq_32792075/article/details/77984292
https://blog.csdn.net/chenxun_2010/article/details/46488055
https://github.com/shineyr/Socket
1 客户端头文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#define PORT 8888
#define MAX_LINE 2048
2 readline函数
/*readline函数实现*/
ssize_t readline(int fd, char *vptr, size_t maxlen)
{
ssize_t n, rc;
char c, *ptr;
ptr = vptr;//指向同一块内存
for (n = 1; n < maxlen; n++) {
if ( (rc = read(fd, &c,1)) == 1) {
*ptr++ = c;
if (c == '\n')//换行符
break; /* newline is stored, like fgets() */
} else if (rc == 0) {
*ptr = 0;
return(n - 1); /* EOF, n - 1 bytes were read */
} else
return(-1); /* error, errno set by read() */
}
*ptr = 0; /* null terminate like fgets() */
return(n);
}
/*普通客户端消息处理函数*/
void str_cli(int sockfd)
{
/*发送和接收缓冲区*/
char sendline[MAX_LINE] , recvline[MAX_LINE];
while(fgets(sendline , MAX_LINE , stdin) != NULL)
{
write(sockfd , sendline , strlen(sendline));
//从stdin(终端标准输入流)读取,向sockfd写入
bzero(recvline , MAX_LINE);
if(readline(sockfd , recvline , MAX_LINE) == 0)
{
perror("server terminated prematurely");
exit(1);
}//if
if(fputs(recvline , stdout) == EOF)
{
perror("fputs error");
exit(1);
}//if
//从sockfd读取,向stdout(终端标准输出流)写入
bzero(sendline , MAX_LINE);
}//while
}
/*采用select的客户端消息处理函数*/
void str_cli2(FILE* fp , int sockfd)
{
int maxfd;
fd_set rset;
/*发送和接收缓冲区*/
char sendline[MAX_LINE] , recvline[MAX_LINE];
FD_ZERO(&rset);
while(1)
{
/*将文件描述符和套接字描述符添加到rset描述符集*/
FD_SET(fileno(fp) , &rset);
FD_SET(sockfd , &rset);
maxfd = max(fileno(fp) , sockfd) + 1;
select(maxfd , &rset , NULL , NULL , NULL);//加入监听集中
if(FD_ISSET(fileno(fp) , &rset))//终端读取事件发生
{
if(fgets(sendline , MAX_LINE , fp) == NULL)
{
printf("read nothing~\n");
close(sockfd); /*all done*/
return ;
}//if
sendline[strlen(sendline) - 1] = '\0';
write(sockfd , sendline , strlen(sendline));
}//if
if(FD_ISSET(sockfd , &rset))//sockfd事件
{
if(readline(sockfd , recvline , MAX_LINE) == 0)
{
perror("handleMsg: server terminated prematurely.\n");
exit(1);
}//if
if(fputs(recvline , stdout) == EOF)
{
perror("fputs error");
exit(1);
}//if
}//if
}//while
}
3 main主函数
int main(int argc , char **argv)
{
/*声明套接字和链接服务器地址*/
int sockfd;
struct sockaddr_in servaddr;
/*判断是否为合法输入*/
if(argc != 2) //./client 8888
/*127.0.0.1默认本地,乱输没事 */
{
perror("usage:tcpcli <IPaddress>");
exit(1);
}//if
/*(1) 创建套接字*/
if((sockfd = socket(AF_INET , SOCK_STREAM , 0)) == -1)
{
perror("socket error");
exit(1);
}//if
/*(2) 设置链接服务器地址结构*/
bzero(&servaddr , sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
if(inet_pton(AF_INET , argv[1] , &servaddr.sin_addr) < 0)
{
printf("inet_pton error for %s\n",argv[1]);
exit(1);
}//if
/*(3) 发送链接服务器请求*/
if(connect(sockfd , (struct sockaddr *)&servaddr , sizeof(servaddr)) < 0)
{
perror("connect error");
exit(1);
}//if
/*调用普通消息处理函数*/
str_cli(sockfd);
/*调用采用select技术的消息处理函数*/
//str_cli2(stdin , sockfd);//这个不能像str_cli反复写入服务端,why?
exit(0);
}
4 补充
max函数
int max(int a , int b)
{
return a > b ? a : b;
}
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
/*read函数从打开的设备或文件中读取数据
返回值:成功返回读取的字节数,出错返回-1并设置errno,如果在调read之前
已到达文件末尾,则这次read返回0。read函数从描述符fd的当前文件位置拷贝
最多n个字节到存储器位置buf*/
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
/*write函数向打开的设备或文件中写数据
返回值:成功返回写入的字节数,出错返回-1并设置errno
write函数从存储器位置buf拷贝至多n个字节到文件描述符fd的当前文件位置*/
//demo
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
//#include <errno.h>
int main(void)
{
char buf[10];
int n;
n = read(STDIN_FILENO, buf, 10);
if (n < 0) {
printf("something worry!");
exit(1);
}
write(STDOUT_FILENO, buf, n);
return 0;
}
/*
剩下的字符仍然保存在内核的终端设备输入缓冲区中。Shell进程恢复运行,
Shell继续从终端读取用户输入的命令,于是读走了终端设备输入缓冲区中剩下的字符d和换行符*/
在调用read和write函数时可能会遇到不足值的情况,EOF(end of file)就可能是一种情况。
如一个文件从当前位置开始只含有20个字节,但是一次却读了50个字节,
就会造成不足值的情况。再如从终端读取文本,read函数是按行操作,就会造成不足值。
还有读和写网络套接字时,由于网络的延迟或者是内部缓冲约束也会造成不足值的产生。
处理不足值的一个方法就是反复的调用read和write进行处理。