linux socket 编程,诡异事件??
这个函数用来读取本地文件,然后发送到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
还请大家多指教。
------解决方案--------------------
探讨 发表于:2010-04-01 22:45:14 回复于:2010-04-01 23:20:35谢谢大家,这个问题已经解决了。是我自己搞错了。 另外问大家一个问题:怎样用socket实现图片和语音方面的传输??