标题:请教linux 下的socket编程如何实现发送指定文件长度以及接收完全问题 贴代码 ...
只看楼主
zzshibi
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2011-10-21
结帖率:0
 问题点数:0 回复次数:1 
请教linux 下的socket编程如何实现发送指定文件长度以及接收完全问题 贴代码 新手求解
结果
server端:
文件打印完全
1757
1757
file has been uploaded successfully!
段错误
client端
文件第一遍打印完全 之后打印后半部分 并且一直处于接受状态
GDB调试时接收端打印接收完整之后等待接收此时发送端已经发送完毕。当发送端执行关闭发送端口时程序执行完毕,接收端却不止尽的接受程序后半段。。
但是查看文件接收的文件夹数据又是完整的 ,并无多也无少。
代码如下:
Server.c

#include <sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include<unistd.h>
#include <stdlib.h>
#include <string.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include <time.h>

#define PORT 3333
#define MAXDATASIZE 1024
#define REMOTE_IP "192.168.1.101"

main()
{
  int sock_fd,csock_fd;
  int i,k,t,byte;
  char buf[1024];
  struct sockaddr_in server_address;
  struct sockaddr_in client_address;
  int server_len,client_len;
  char *filename;
  struct stat buf1;

  if((sock_fd=socket(AF_INET,SOCK_STREAM,0))<0)
    {
      perror("socket created .\n");
      exit(1);
    }
    else
    {
    printf("socket created .\n");
    printf("socked id:%d \n",sock_fd);
    }
  server_address.sin_family=AF_INET;
  server_address.sin_port=htons(PORT);
  server_address.sin_addr.s_addr=INADDR_ANY;
  server_len=sizeof(server_address);
  client_len=sizeof(client_address);

   if(bind(sock_fd,(const struct sockaddr*)&server_address,server_len)<0)
    {
      perror("connect");
      exit(1);
    }
   else
    {
    printf("connected.\n");
    printf("local PORT:%d \n",PORT);
    }
   if(listen(sock_fd,3)<0)
    {
      perror("listen");
      exit(1);
    }
   else
    {
    printf("listenning.....\n");
    }
    if((csock_fd=accept(sock_fd,(struct sockaddr*)&client_address,(socklen_t*)&client_len))<0)
    {
      perror("accept");
    }
   else
    {
    printf("connect from %d\n",inet_ntoa(REMOTE_IP));
    }
    filename="/home/zshibi/once/filesocket/ntp.c";
   t=stat(filename,&buf1);
if ((byte=open(filename,O_RDONLY))==-1)  //打开所要发送的文件
 {
  perror("open will upload file");
  exit(1);
 }
 memset(buf,0,sizeof(buf));
     //将所要发送的内容写入buf
   
while(1)
{
        int i;
      
        if((k=read(byte,buf,buf1.st_size))==-1)
 {
  perror("read will upload file");
  exit(1);
 }
 printf("%s\n",buf);
if((i=send(csock_fd,buf,k,0))==-1)
 {
  perror("send");
  exit(1);
 }
       if(i==buf1.st_size)break;
}
 printf("file has been uploaded successfully!\n");
     

 close(byte);
 close(csock_fd);
}




client.c
#include <sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<netinet/in.h>
#include<unistd.h>
#include <stdlib.h>
#include <string.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/stat.h>

#define MAXDATASIZE 1024
#define PORT 3333
#define REMOTE_IP "192.168.1.101"

main()
{
  int sock_fd;
  int byte,i,result,k,t,length;
  struct sockaddr_in address;
  char buf[1024];
  int address_len=sizeof(address);
  char *filename;

  if((sock_fd=socket(AF_INET,SOCK_STREAM,0))<0)
    {
      perror("socket created .\n");
      exit(1);
    }
    else
    {
    printf("socket created! .\n");
    printf("socked id:%d \n",sock_fd);
    }
    bzero((struct sockaddr*)&address,address_len);
    address.sin_family=AF_INET;
    address.sin_port=htons(PORT);
    address.sin_addr.s_addr=htons(REMOTE_IP);
   
    if(result=connect(sock_fd,(struct sockaddr*)&address,address_len)<0)
    {
      perror("connect ok!\n");
      exit(1);
    }
    else
    {
    printf("connected ok!\n");
    printf("remote ip:%s \n",REMOTE_IP);
    printf("remote port:%d\n",PORT);
    }
filename="/home/zshibi/hello/hello/bindc1.c";
    if((byte=open(filename,O_CREAT|O_TRUNC|O_WRONLY,0644))==-1)
 {
  perror("open localhost file");
  exit(1);
 }
 memset(buf,0,sizeof(buf));  //客户2接收从服务器转发过来的客户1的文件内容
     printf("%d\n",byte);
while(1)
 {
if( (i=recv(sock_fd,buf,sizeof(buf),0))==-1 )
 {
  perror("recv");
  exit(1);
 }
 printf("buf:%s\n",buf);
 //客户2讲接收到的内容写入新建文件中
 if( k=write(byte,buf,i)==-1 )
  {
  perror("writting to file error");
  exit(1);
 }
printf("%d\n",k);
 if(buf=="/0")break;
 }
 printf("file has been uploaded successfully!\n");
 close(byte);
 close(sock_fd);
}
 

[ 本帖最后由 zzshibi 于 2011-10-21 17:05 编辑 ]
搜索更多相关主题的帖子: server linux include 编程 文件夹 
2011-10-21 17:02
zzshibi
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2011-10-21
得分:0 
高手们都忙啊‵‵‵‵
2011-10-31 16:07



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-353109-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.136730 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved