[问题]关于socket编程

软件和网站开发以及相关技术探讨
回复
Einsxu
帖子: 26
注册时间: 2008-03-18 13:23

[问题]关于socket编程

#1

帖子 Einsxu » 2008-09-07 17:53

自己写的关于rls的代码,实现远程ls命令
但是发现服务器程序在c=fgetc(fp)得到需要的内容时是正确的,但是在发送到客户端的时候客户端却收不到
messlen=read(sock_id,buf,BUFSIZ)中messlen一直等于0
已发现是服务器程序的问题
把fclose(fin);fclose(fout);的顺序交换一下就可以,现在搞不懂的是为什么这两个的顺序会有影响呢?都已经出while循环了啊。


# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <netdb.h>

# define PORT 1300
# define HOSTLEN 256
# define oops(msg) { perror(msg);exit(1); }

int main()
{
struct sockaddr_in saddr;
char hostname[HOSTLEN];
int sock_id,sock_fd;
struct hostent *hp;
FILE *fout,*fin,*fp;
char dirname[BUFSIZ];
char command[BUFSIZ];
char c;

sock_id=socket(PF_INET,SOCK_STREAM,0);
if(sock_id==-1)
oops("socket");

bzero((void *)&saddr,sizeof(saddr));
gethostname(hostname,HOSTLEN);
hp=gethostbyname(hostname);
bcopy((void *)hp->h_addr,(void *)&saddr.sin_addr,hp->h_length);
saddr.sin_port=htons(PORT);
saddr.sin_family=AF_INET;
if(bind(sock_id,(struct sockaddr *)&saddr,sizeof(saddr))!=0)
oops("bind");
if(listen(sock_id,1)!=0)
oops("listen");

while(1){
sock_fd=accept(sock_id,NULL,NULL);
if(sock_fd==-1)
oops("accept");
fin=fdopen(sock_fd,"r");
if(fin==NULL)
oops("fdopen reading");
if(fgets(dirname,BUFSIZ-5,fin)==NULL)
oops("dirname reading");
sanitize(dirname);
sprintf(command,"ls % s",dirname);
fp=popen(command,"r");

if(fp==NULL)
oops("popen");
fout=fdopen(sock_fd,"w");
if(fout==NULL)
oops("fdopen writing");
while((c=fgetc(fp))!=EOF)
fputc(c,fout);

pclose(fp);
fclose(fin);
fclose(fout);
}


return 0;
}

sanitize(char *str)
{
char *src, *dest;

for ( src = dest = str ; *src ; src++ )
if ( *src == '/' || isalnum(*src) )
*dest++ = *src;
*dest = '\0';
}
上次由 Einsxu 在 2008-09-07 19:21,总共编辑 1 次。
Einsxu
帖子: 26
注册时间: 2008-03-18 13:23

#2

帖子 Einsxu » 2008-09-07 17:54

客户端程序如下
# include <stdio.h>
# include <stdlib.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <netdb.h>
# include <string.h>

# define oops(msg) { perror(msg);exit(1);}
# define PORT 1300

int main(int argc,char *argv[])
{
struct sockaddr_in servadd;
struct hostent *hp;
int sock_id,sock_fd;
int messlen;
char buf[BUFSIZ];

if(argc!=3)
oops("please input the write command");
sock_id=socket(AF_INET,SOCK_STREAM,0);
if(sock_id==-1)
oops("socket");

bzero(&servadd,sizeof(servadd));
hp=gethostbyname(argv[1]);
if(hp==NULL)
oops("argv[1]");
bcopy((void *)hp->h_addr,(void *)&servadd.sin_addr,hp->h_length);
servadd.sin_port=htons(PORT);
servadd.sin_family=AF_INET;
if(connect(sock_id,(struct sockaddr *)&servadd,sizeof(servadd))!=0)
oops("connect");

messlen=write(sock_id,argv[2],strlen(argv[2]));
if(messlen==-1)
oops("write dirname");
if(write(sock_id,"\n",1)==-1)
oops("write \n");
while((messlen=read(sock_id,buf,BUFSIZ))>0)
if(write(1,buf,messlen)!=messlen)
oops("write");

printf("messlen=%d\n",messlen);
close(sock_id);

return 0;
}

在自己机器上使用的命令是./rls a-desktop /media
回复