| 
 | 
 
 楼主 |
发表于 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); 
  } 
谢谢。 |   
 
 
 
 |