开发者社区> 问答> 正文

liunx下,文件的上传与下载?报错


@中山野鬼

你好,想跟你请教个问题 这个:


务器:
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>    // for sockaddr_in
#include <sys/types.h>    // for socket
#include <sys/socket.h>    // for socket
#include <stdio.h>        // for printf
#include <stdlib.h>        // for exit
#include <string.h>        // for bzero
#include <time.h>                //for time_t and time
 
#define LISTEN_QUEUE 20
#define SERVICE_PORT 1234

int service_sockfd;
int ret;
int sc;
static char buf[1024]="";
 FILE *stream;
int main(int argc,char *argv[])

{
  struct sockaddr_in service_addr;
	service_addr.sin_family =AF_INET;
	service_addr.sin_port =htons(SERVICE_PORT);
	service_addr.sin_addr.s_addr =htonl(INADDR_ANY);
	bzero(&(service_addr),sizeof(service_addr));
	
	service_sockfd =socket(AF_INET,SOCK_STREAM,0);
	if(-1 == service_sockfd)
	{
	 perror("sosck failed\n");
	 exit(-1);
	}

	if(bind(service_sockfd,(struct sockaddr*)&service_addr,sizeof(service_addr))==0)
	{
	 printf("bind failed。。。。。。。。。。。。。。。。\n");
	 exit(-1);
	}
	if( -1 ==listen(service_sockfd,10))
	{
	 printf("listen: %s\n",hstrerror(h_errno));
	 exit(-1);
	}
	printf("listening...................\n");
	while(1)
{
	
	 socklen_t leng = sizeof(service_addr);
	 struct sockaddr_in client_addr;
	 sc =accept(service_sockfd,(struct sockaddr*)&client_addr,&(leng));
	 if(-1 == sc)
	 {
	  printf("accept failed\n");
	  break;
	 }
	 else
	 {
	  printf("%s:%d\n",inet_ntoa(client_addr.sin_addr),client_addr.sin_port);
	 }
	
	 bzero(buf,1024);
	 strcpy(buf,"hello world 从服务器上来!!");
	 if(ret =send(sc,buf,strlen(buf)+1,0)==-1)
	 {
	   perror("send failed\n");
	   exit(-1);
	 }
	 printf("the service sending!!!\n");
	  ret =recv(sc,buf,1024,0);
	 if(-1 ==ret)
	 {
	  perror("recv failed\n");
		 	  exit(-1);
	 }
	 
	 printf("recv:[%d] - %s\n",ret,buf);
	
	if(fopen("file1","w+")==NULL)
	 {
	  printf("the file open error\n");
	 	  exit(-1);
	 }
	else
	{
		 printf("the file open successful\n");
	}
	 printf("Send %s\n",buf);
	 int lengsize = 0;
	 if(lengsize =fread(buf,1,1024,stream)>0)
	 {
	    printf("lengsize = %d\n",lengsize);
	    if(send(sc,buf,lengsize,0)<0)
	    {
			 printf("Send File is Failed\n");
			 break;
	    }
      bzero(buf, 1024);
	  }
 
   if(fclose(stream))
   {
   printf("The file 'data' was not closed! \n");
   exit(1);  
	 } 
	 close(sc);     
} 
	close(service_sockfd);
	return 0;
	
}





客户端:

#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>    // for sockaddr_in
#include <sys/types.h>    // for socket
#include <sys/socket.h>    // for socket
#include <stdio.h>        // for printf
#include <stdlib.h>        // for exit
#include <string.h>        // for bzero
#include <time.h>                //for time_t and time


#define CLIENT_PORT 1234
#define MAX 1024


char *IP="127.0.0.1";
int client_socket;
int ret;
int my;
char buf[MAX]="hello 服务器";
FILE *stream;


int main()
{
  client_socket =socket(AF_INET,SOCK_STREAM,0);
  if(-1 == client_socket)
{
perror("sosck failed\n");
exit(-1);
}
struct sockaddr_in client_addr;
client_addr.sin_family =AF_INET;
client_addr.sin_port =htons(CLIENT_PORT);
client_addr.sin_addr.s_addr = inet_addr(IP);


ret =connect(client_socket, (struct sockaddr *)&client_addr, sizeof(struct sockaddr_in));
if(-1 == ret)
{
printf("connetct failed\n");
exit(-1);
}

while(1)
{
    strcat(buf,"发送请求");


 if(-1 == send(client_socket,buf,sizeof(buf)+1,0))
 {
   perror("send failed\n");
   exit(-1);
 }
ret =recv(client_socket,buf,1024,0);
 if(-1 ==ret)
  {
   perror("recv failed\n");
      exit(-1);
  }
  if(fopen("file2","w+") == NULL)
   {
    printf("the file open error\n");
   exit(-1);
   }
  else
  {
  printf("the file open successful\n");
 }
 printf("Send %s\n",buf);
   int lengsize = 0;
   if(lengsize=fwrite(buf,1,1024,stream) == -1)
   {
     printf("lengsize = %d\n",lengsize);
     if(send(client_socket,buf,lengsize,0)<0)
     {
  printf("Send File is Failed\n");
  break;
 } 
   }
 }
    bzero(buf, 1024);
    if(fclose(stream))
      {
       printf("The file 'data' was not closed! \n");
       exit(1);  
   } 
 
 close(client_socket);    
    return 0;  
}




我还是新手,代码写的不好,请见谅
1.先运行服务器,正常,再运行客户端,为什么总是拒绝呢?connect报错???,
为什么?
2.代码写的还有不好的地方,还请大神多指点啦!


展开
收起
爱吃鱼的程序员 2020-06-14 21:54:38 639 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    哈,没细看,我把我这边server和client端的代码贴给你,你参考一下,看是不是有什么步骤遗漏了。。。

    _Isetup_server(_shostname,_Iport,_SERVER_FUNCprf,_SERVER_FUNCpwf,_Inum){_SOCKETIDsockfd,new_fd;_Ire=0;_Ii;structsockaddr_inclient_addr;socklen_taddr_size=sizeof(structsockaddr_in);_i32chk;structsockaddr_inservaddr;structtimevaltv;fd_setfdsr;_i32lastminute,maxsock,flag;signal(SIGPIPE,sig_send_err);ms_serverid=0;sockfd=socket(AF_INET,SOCK_STREAM,ms_potol);_error(sockfd==-1,_setup_server_ERR,"createsocketerror:%s(errno:%d)\n",strerror(errno),errno);chk=setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&ms_socket_reuse_opt,sizeof(ms_socket_reuse_opt));_error(chk==-1,_setup_server_close_ERR,"setsockoptSO_REUSEADDRerror:%s(errno:%d)\n",strerror(errno),errno);memset(&servaddr,0,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_port=htons(port);chk=inet_pton(AF_INET,hostname,&servaddr.sin_addr);_error(chk<=0,_setup_server_close_ERR,"inet_ptonerrorfor%s",hostname);chk=bind(sockfd,(structsockaddr*)&servaddr,sizeof(servaddr));_error(chk==-1,_setup_server_close_ERR,"bindsocketerror:%s(errno:%d)",strerror(errno),errno);chk=listen(sockfd,ms_backlog);_error(chk==-1,_setup_server_close_ERR,"listensocketerror:%s(errno:%d)",strerror(errno),errno);lastminute=get_current_min();maxsock=sockfd;_log("maxsock%d",maxsock);printf("======waitingforclient'srequest======\n");ms_conn_num=0;



    以上server,以下client

    _Isetup_client(_shostname,_Iport,_CLIENT_FUNCprf,_CLIENT_FUNCpwf){_SOCKETIDsockfd;_i32chk;_Ire=0;_It;structsockaddr_inservaddr;signal(SIGPIPE,sig_send_err);_error((prf==0)&&(pwf==0),_setup_client_ERR,"norece,nosenddowhat?");sockfd=socket(AF_INET,SOCK_STREAM,0);_error(sockfd<0,_setup_client_ERR,"createsocketerror:%s(errno:%d)\n",strerror(errno),errno);memset(&servaddr,0,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_port=htons(port);chk=inet_pton(AF_INET,hostname,&servaddr.sin_addr);_error(chk<=0,_setup_client_close_ERR,"inet_ptonerrorfor%s\n",hostname);chk=connect(sockfd,(structsockaddr*)&servaddr,sizeof(servaddr));_error(chk<0,_setup_client_close_ERR,"connecterror:%s(errno:%d)\n",strerror(errno),errno);



    其他的类型定义你不用去理解,都是我自己的类型定义,你跟踪两个变量就ok了。_shostname和_Iport;“_s"是我自己定义字符串类型等于char*,_I可以等于unsignedint类型,上面两端代码是我测试过的。。你对应一下。

    另外我可不大神。哈。



    看来没时间 ,不过,还要谢谢

    你的server代码有明显的错误啊。

    第35行修改为==-1不是==0:

    if(bind(service_sockfd,(structsockaddr*)&service_addr,sizeof(service_addr))==-1)



    把第26行去掉:

    bzero(&(service_addr),sizeof(service_addr));



    我测试了一下server,在1234端口已经监听成功了,后面的下载你再继续测试看看~


    回复 @徐永强:回答问题对楼主有帮助,也没评上个最佳答案啥的,没积分,就不能赢开源内裤,没内裤只好裸奔了~T.T1、也许多数人都不喜欢读别人代码。2、这层“答的基本合格”,呵呵。耐心阅读楼主的代码、编译、测试,值得学习提倡。3、楼主新代码可以边写边测试观察,像监听不上的问题,netstat就可以发现。为什么要去掉bzero(&(service_addr),sizeof(service_addr));

    第26行为什么去掉,你先要想一下这第26行为什么加呢?

    bzero函数的用处:Thebzero()functionsetsthefirstnbytesofthebyteareastartingatstozero(bytescontaining’\0’).

    你前面对server_addr的各种赋值,一个bzero把它全搞没了,后面还怎么玩?

    所以这一行要么去掉,要么放在service_addr定义之后属性赋值之前,要么直接修改为:

    bzero(&(service_addr.sin_zero),8);



    sin_zero是保留的空字节,设不设置对你这个程序没有影响~



    回复 @tb_engineer:哥们,既然认为回答的不错,好歹给个积分啥的,让俺也有机会赢个开源内裤穿穿~~T.T好的,明白了,万分感谢
    2020-06-15 10:48:16
    赞同 展开评论 打赏
问答分类:
C++
问答地址:
问答排行榜
最热
最新

相关电子书

更多
附件下载测试 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载