我正在编写与服务器通信,发送和接收数据的客户端代码.
我正在使用select函数来监视两个文件描述符-0作为标准输入fd,4(socket_dect)作为套接字fd.
主要思想是查找当前fd的活动状态,如果STDING是活动状态,则表示错误地将其读取并将数据发送到服务器,如果套接字fd是活动状态,则错误地接收数据并打印.
我不知道为什么,但是在运行此客户端时一无所有.
这是我的代码:
//聊天客户端
//由omri撰写
#include <stdio.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <arpa/inet.h> //inet_addr
#include <netinet/in.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#define PORT 9034 // defined port like the server
#define STDIN 0
void main()
{
struct timeval tv; // time values for select function
int activity = 0 ;
int ready; // contains the result of the select function
char buf[256];
int len = strlen(buf);
int byte_sent,i;
int socket_dect; // creating socket descriptor
struct sockaddr_in ServerInfo;
fd_set readfds;
// creating a new socket, its a number represent a file descriptor
// socket args : 1)ip protocol ipv4,second tcp/udp, third is number of protocol used
socket_dect = socket(AF_INET,SOCK_STREAM,0);
if(socket_dect == -1){
perror("error creating socket");
}
// fill the values of the server
ServerInfo.sin_family = AF_INET; // ipv4
ServerInfo.sin_port = htons(PORT); // port number
//ServerInfo.sin_addr = 127.0.0.1;
inet_pton(AF_INET, "127.0.0.1", &ServerInfo.sin_addr);//insert the ip to the sin addr
//fill the values of the time for the select function
tv.tv_sec = 5; // 5 sec
tv.tv_usec = 0; // 0 mili sec
FD_ZERO(&readfds); // clear the socket set
for(i = 0; i<= socket_dect; i++){
FD_SET(i,&readfds);// set the sockets
}
// making the connection to the server
//ServerInfo.sin_addr.s_addr = inet_addr("127.0.0.1"); // another way to put ip addr
connect(socket_dect,(struct sockaddr *)&ServerInfo,sizeof(ServerInfo)); // connected to the server
printf("after connect");
while(1){
ready = select(socket_dect,&readfds,NULL,NULL,&tv);
printf("%d is ready",ready);
if(ready < 0){
perror("select error");
printf("select error");
}
// reading from STDIN to buff
else if(ready == STDIN_FILENO){
if(fread(buf,256,1,&readfds) < 0){
printf("read error");
perror("read error");
}
if(send(socket_dect,buf,len,NULL) < 0){
perror("send connection");
printf("send error");
}//end if send
}//end elseif STDIN
else if(ready == socket_dect){
if(recv(socket_dect,buf,len,0) < 0 ){
printf("recv error");
}
printf("data recived is :%s",buf);
}//end elseif
else{
printf("none is ready");
}
}//end while
}//end main
解决方法:
对于初学者来说,还有两个问题.
这个
ready = select(socket_dect,&readfds,NULL,NULL,&tv);
应该
ready = select(socket_dect + 1, &readfds, NULL, NULL, &tv);
从man select
开始:
nfds is the highest-numbered file descriptor in any of the three sets, plus 1.
代替
if(ready == STDIN_FILENO)
采用
if (FD_ISET(STDIN_FILENO, &readfds)
select()修改了传递的参数,因此您每次需要在调用select()(在循环内部)之前重新初始化它们.
另外,您必须测试所有系统调用的结果.这包括connect().
同样,^ 2您可能不会认为recv()返回的字节数要等于被告知的字节数(此处为len).它可能会收到很少的回报.为确保获得所需的内容,请在recv()计数周围循环.
标签:sockets,select,c-3,linux 来源: https://codeday.me/bug/20191118/2029842.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。