⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fwq.c

📁 TCP/IP编程实现远程文件传输
💻 C
字号:
/*服务器源程序fwq.c*/ 
#include<stdio.h> 
#include<sys/types.h> 
#include<sys/fcntl.h> 
#include<sys/socket.h> 
#include<sys/netinet/in.h> 
#include<netdb.h> 
#include<errno.h> 
main() 
{ 
  char c,buf[1024],file[30]; 
  int fromlen,source; 
  register int k,s,ns; 
  struct sockaddr_in sin; 
  struct hostent *hp; 
  system(″clear″); 
  printf(″\n″); 
    
  printf(″\n\n\t\t输入要传输的文件名:″); 
  scanf(″%s″,file); 
  if ((source=open(file,O_RDONLY))<0){ 
   perror(″源文件打开出错″); 
   exit(1); 
  } 
  printf(″\n\t\t在传送文件,稍候…″); 
  hp=gethostbyname(″server″); 
  if (hp==NULL){ 
   perror(″返回主机地址信息错!!!″); 
   exit(2); 
  } 
  s=socket(AF_INET,SOCK_STREAM,0); 
  if(s<0){ 
   perror(″获取SOCKET号失败!!!″); 
   exit(3); 
  } 
  sin.sin_family=AF_INET; 
  sin.sin_port=htons(1500);/*使用端口1500*/ 
  bcopy(hp->h_addr,&sin.sin_addr,hp->h_length); 
  if(bind(s,&sin,sizeof(sin))<0){ 
   perror(″不能将服务器地址捆绑到SOCKET号上!!!″); 
   colse(s); 
   exit(4); 
  } 
  if(listen(s,5)<0{ 
   perror(″sever:listen″); 
   exit(5); 
  } 
while(1){ 
  if((ns=accept(s,&sin,&fromlen))<0){ 
   perror(″sever:accept″); 
   exit(6); 
  } 
  lseek(source,OL,0);/*每次接受客户机连接,应将用于读的源文件指针移到文件头*/ 
  write(ns,file,sizeof(file)); /*发送文件名*/ 
  while((k=read(source,buf,sizeof(buf)))>0) 
   write(ns,buf,k); 
  printf(″\n\n\t\t传输完毕!!!\n″); 
  close(ns); 
  close(source); 
  exit(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -