QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2432|回复: 6

【求助】

[复制链接]
发表于 2007-5-11 11:16:26 | 显示全部楼层 |阅读模式
有谁有可以指导下如何在不同PC的Linux的下实现TCP/IP SOCKET文件传输么?
发表于 2007-5-12 09:38:55 | 显示全部楼层
不同pc?
不同pc之间编程实现文件传输有什么区别么?
回复

使用道具 举报

 楼主| 发表于 2007-5-12 22:48:50 | 显示全部楼层
不好意思,可能说的不太明白,就是实现Linux的TCP或UDP文件传输,我现在只能实现简单的服务器/客户连接,发送简单的文本。现在就是想实现一下文件、图像、语音之类的传输。
回复

使用道具 举报

发表于 2007-5-13 09:11:48 | 显示全部楼层
我觉得你把你收到的内容写到本地文件中就可以了,传输协议不管你传的什么内容。
至于图像和语音,如果当成文件传过来,那还是一样。如果是动态显示,取一帧数据->对方压缩->传输->本地解压缩->播放。在传输层,都不区别数据是什么格式。要靠你的程序去解释
回复

使用道具 举报

发表于 2007-5-13 10:25:27 | 显示全部楼层
sagaeon, 他要的是怎么两个机器之间进行数据传送,之后获取的数据保存为文件…………
也就是需要三个过程:打开文件读取数据,发送/接收数据,写入文件数据。

我估计楼主想知道如何发送/接收数据,而且还要求是双向的协议。
回复

使用道具 举报

发表于 2007-5-13 13:33:15 | 显示全部楼层
查看nc的用法
回复

使用道具 举报

 楼主| 发表于 2007-5-13 21:24:41 | 显示全部楼层
谢谢各位版主 :-)  :-)  :-) ,我想实现的就是jiangtao9999说的两个机器之间进行数据传送,之后获取的数据保存为文件………… 也就是需要三个过程:打开文件读取数据,发送/接收数据,写入文件数据,还想问下,读取接受数据时要不要做格式转换,基本的流程我理解了,就是在编程实现上有些难度,在调试的时候,运行客户端时总是提示出现段错误,不知道怎么解决,下面是代码,可以帮我看下问题出现在哪儿吗?我是在一个系统下直接启动两个超级终端调试的。
服务器:
//*server.c*/
//*headfile*/
#include<stdio.h>
#include<sys/types.h>
#include<sys/fcntl.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<errno.h>
//*main function*/
main()
{
//*initialize*/
//  static char aaa[]="localhost.localdomain"; /*my hostname*/
  char c,buf[1024],file[30];
  int fromlen,source;
//*source:file descriptor*/
  register int k,s,ns;
//*integer register*/
  struct sockaddr_in sin;
//*datatype of socket*/
  struct hostent *hp;
//*adress info*/
  system("clear");
  printf("\n");
  printf("\n\n\t\tplease enter the file name:");
  scanf("%s",file);
//*open file*/
  if ((source=open(file,O_RDONLY))<0)
  {
  perror("open file error:");
  exit(1);
  }
  printf("\n\t\tsending file now,please wait....:");
//*get hostname*/
  hp=gethostbyname("localhost.localdomain");
  if (hp==NULL)
  {
  perror("return wrong server address!!!");
  exit(2);
  }
  perror("now sending data:...");
//*creat socket*/
  s=socket(AF_INET,SOCK_STREAM,0);
  if(s<0)
  {
  perror("failed to get the socket discriptor!!!");
  exit(3);
  }
/*socket datatype*/
  sin.sin_family=AF_INET;
  sin.sin_port=htons(1500);/*use port 1500*/
  bcopy(hp->h_addr,&sin.sin_addr,hp->h_length);
//*bind server address to socket*/
  if(bind(s,&sin,sizeof(sin))<0)
  {
  perror("could't bind hostaddress to socket!!!");
  close(s);
  exit(4);
  }
//*listen*/
  if(listen(s,5)<0)
  {
  perror("sever:listen");
  exit(5);
  }
//*accept connect*/
  while(1)
  {
  if((ns=accept(s,&sin,&fromlen))<0)
  {
  perror("sever:can not accept!");
  exit(6);
  }
  lseek(source,1,0);/*accept client connect,move the file index to object file */
  write(ns,file,sizeof(file));/*send file name*/
  while((k=read(source,buf,sizeof(buf)))>0)
  write(ns,buf,k);
  printf("\n\n\tsend over!\n");
  close(ns);
  }
    close(source);
    exit(0);
  }
客户端:
//*client.c*/
//*head files*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
//*main function*/
  main()
  {
//*initialize the variables*/
  char buf[1024],file[30];
  char *strs="\n\n\t\treceving file now....:";
  int target;
  register int k,s;
  struct sockaddr_in sin;
  struct hostent *hp;
  system("clear");
  printf("\n");
//*get the host name*/   
  hp=gethostbyname("localhost.localdomain");

  if(hp==NULL)
  {
  perror("return the wrong server address!!!");
  exit(1);
  }
  //*creat socket*/
  s=socket(AF_INET,SOCK_STREAM,0);
  if(s<0)
   {
  perror("failed to get the socket discriptor!!!");
  exit(2);
  }
  sin.sin_family=AF_INET;
  sin.sin_port=htons(1500);//*port address 1500*/
  bcopy(hp->h_addr,&sin.sin_addr,hp->h_length);
  printf("\n\n\t\tnow connecting with the server.....");
  if(connect(s,&sin,sizeof(sin))<0)
  {
  perror("can't connect with server!!!");
  exit(3);
  }

  while((k=read(s,file,sizeof(file)))<0)//*receive file name*/
  if((target=open(file,O_WRONLY|O_CREAT|O_TRUNC,0644))<0)
  {
  perror("could't open target file!!!");
   exit(4);
  }
  strcat(strs,file);
  strcat(strs,"please wait!!!");
  write(1,strs,strlen(strs));

  while((k=read(s,buf,sizeof(buf)))>0)
  write(target,buf,k);
  printf("\n\n\t\tsucceed receving file!!!\n");
  close(s);
  close(target);
  }
谢谢。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-4-20 14:39 , Processed in 0.082012 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表