【问题标题】:Can't send file from server to client over LAN无法通过 LAN 将文件从服务器发送到客户端
【发布时间】:2014-01-25 11:13:51
【问题描述】:

这是一个服务器端和客户端的代码sn-p,用户可以通过它从服务器请求文件。服务器将发送文件。

有两个问题:

  • 服务器端发送空文件。
  • 尝试在局域网中运行代码时出现 ioexception

我不明白为什么服务器发送空文件,请帮助。

服务器端代码:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
 package ftpserverclient.FileClientServer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Arnab
 */
public class FileServer {

public static void main(String args[])throws IOException
         { 
             ServerSocket ss=null;
             try
             {  
                 ss=new ServerSocket(8085);
             }
             catch(IOException e)
             { 
                 System.out.println("couldn't listen");
                 System.exit(0);
             }
             Socket cs=null;
             try
             { 
                 cs=ss.accept();
                 System.out.println("Connection established"+cs);
             }
             catch(Exception e)
             { 
                 System.out.println("Accept failed");
                 System.exit(1);
             } 
             PrintWriter put=new PrintWriter(cs.getOutputStream(),true);
             BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream()));
             String s=st.readLine();


             String path = s ; 
             System.out.println("The requested file is path: "+path);
             System.out.println("The requested file is : "+s);
             File f=new File(path);
             if(f.isFile())
             { 
                 BufferedReader d=new BufferedReader(new FileReader(f));
                 String line;
                 while((line=d.readLine())!=null)
                 {
                     put.write(line);
                     put.flush();
                 }
                 d.close();
                 System.out.println("File transfered");
                 cs.close();
                 ss.close();
             } 
         }

}

客户端代码:

package ftpserverclient.FileClientServer;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 *
 * @author Arnab
 */
public class FileClient {

public static void main(String srgs[])throws IOException
{
    Socket s=null;
    BufferedReader get=null;
    PrintWriter put=null;
    try
    { 
        s=new Socket("127.0.0.1",8085);
        get=new BufferedReader(new InputStreamReader(s.getInputStream()));
        put=new PrintWriter(s.getOutputStream(),true);

        String u,f;
        System.out.println("Enter the file name to transfer from server:");
        DataInputStream dis=new DataInputStream(System.in);
        f=dis.readLine();
        put.println(f);

        File f1=new File(f);



        FileOutputStream  fs=new FileOutputStream(new File("C:\\PictureDestination\\a.jpg"));


        while((u=get.readLine())!=null)
        { 
            System.out.println(u);
            byte jj[]=u.getBytes();
            fs.write(jj);
        } 
        fs.close();
        System.out.println("File received");
        s.close();
    }catch(Exception e)
    {
        e.printStackTrace();
        System.exit(0);
    }
}       

}

【问题讨论】:

    标签: java sockets lan


    【解决方案1】:

    在客户端,您在向服务器发送文件名时忘记在put.println(f); 之后写put.fflush();。

    【讨论】:

      【解决方案2】:

      最后我发现这个东西有效。

      这里是变化。

      将String u,f; 更改为String f; int u; 和while((u=get.readLine())!=null) { System.out.println(u); byte jj[]=u.getBytes(); fs.write(jj); } 进入 while((u=get.read(jj,0,1024))!=-1) { fs.write(jj,0,u); } fs.close();

      但仍然存在 1 个问题。它不能在 LAN 上运行。

      【讨论】:

      • 您是如何查找服务器 IP 地址的?
      • 我在服务器机器的cmd中运行ipconfig命令,它显示了机器的IP地址
      • 实际上服务器的IP地址是192.168.0.2,我从IP为192.168.0.3的机器上运行客户端程序(将客户端代码从s=new Socket("127.0.0.1",8085);更改为s=new Socket("192.168.0.2",8085);)然后发生错误.. 即服务器端代码抛出 ioexception 即打印 couldn't listen.
      猜你喜欢
      • 2012-06-27
      • 1970-01-01
      • 2015-05-02
      • 2015-06-25
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多