当前位置: 技术问答>linux和unix
linux socket 编程,诡异事件??
来源: 互联网 发布时间:2016-08-15
本文导语: 这个函数用来读取本地文件,然后发送到socket上,源代码如下: int cmd_get(int sock,int sockmsg) { char file_name[100]; char buff[1024]; ssize_t read_bytes;//to record how many bytes are readed. FILE *fd_file; bzero(file_name,100); bzero(buff,...
这个函数用来读取本地文件,然后发送到socket上,源代码如下:
int cmd_get(int sock,int sockmsg)
{
char file_name[100];
char buff[1024];
ssize_t read_bytes;//to record how many bytes are readed.
FILE *fd_file;
bzero(file_name,100);
bzero(buff,1024);
if( recv(sockmsg,file_name,100,0) == -1 )
err_sys("recv error");
strcat(current_path,"/");
strcat(current_path,file_name);
if(( fd_file = fopen(current_path,"r")) == NULL )
err_sys("fopen error");
while(fgets(buff,10,fd_file) != NULL )
{
if( send(sock,buff,strlen(buff),0) == -1 )
err_sys("send error");
bzero(buff,1024);//clear
}
//close(sock); :大家注意这里
//close(sockmsg);
return(0);
}
诡异之处:当我注释掉close两行时,程序阻塞在fgets处,当我不注释时,函数出错提示:read error
: Bad file descriptor
还请大家多指教。
int cmd_get(int sock,int sockmsg)
{
char file_name[100];
char buff[1024];
ssize_t read_bytes;//to record how many bytes are readed.
FILE *fd_file;
bzero(file_name,100);
bzero(buff,1024);
if( recv(sockmsg,file_name,100,0) == -1 )
err_sys("recv error");
strcat(current_path,"/");
strcat(current_path,file_name);
if(( fd_file = fopen(current_path,"r")) == NULL )
err_sys("fopen error");
while(fgets(buff,10,fd_file) != NULL )
{
if( send(sock,buff,strlen(buff),0) == -1 )
err_sys("send error");
bzero(buff,1024);//clear
}
//close(sock); :大家注意这里
//close(sockmsg);
return(0);
}
诡异之处:当我注释掉close两行时,程序阻塞在fgets处,当我不注释时,函数出错提示:read error
: Bad file descriptor
还请大家多指教。
|
数据流
汗。。。