2012-03-26 13 views
0

私のプロジェクトは、どのユーザーがファイルを送信できるアプリケーションを使用して構成されています。ファイル共有コードはlocalhost上で動作し、ネットワーク上では失敗します

基本単位として、私はFileServerとFileClientプログラムを開発しました。

このコードは、localhostでは正常に動作しますが、ネットワーク(wifi)で失敗します。

予想される問題は、受信中にクライアントソケットが閉じているか、一定の時間が経過しても応答していないことです。しかし、私は本当に問題が何であるか、問題に近づく方法を理解することはできません。

エラーが表示されるのは、SocketExceptionです。ソフトウェアによって接続が中止されました。ソケット書き込みエラー。

ファイルサーバCODE:

import java.net.*; 
    import java.io.*; 

    public class FileServerPC { 

    static String remoteIPaddress = "192.168.43.95"; 
    static String filename; 
    String path; 
    static File selectedfile = null; 
    static ServerSocket servsock = null; 
    static Socket sock = null; 
    static FileInputStream fis; 
    static BufferedInputStream bis; 
    static DatagramSocket theSocket = null; 
    static DatagramPacket theOutput; 
    static OutputStream os; 
    byte[] mybytearray; 
    static double nosofpackets; 
    static int packetsize = 1024; 

    public FileServerPC() { 
     try { 
      servsock = new ServerSocket(1500); 
      theSocket = new DatagramSocket(9999); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

    public static void main(String[] args) throws IOException { 
     FileServerPC fs = new FileServerPC(); 

     selectedfile = new File("C:\\flower.jpg"); 
     filename = selectedfile.getName(); 


     Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 

     try { 
      System.out.println("Waiting..."); 

      sock = servsock.accept(); 
      System.out.println("Accepted connection : " + sock); 
      fis = new FileInputStream(selectedfile); 
      System.out.println((int) selectedfile.length()); 
      long length = selectedfile.length(); 
      System.out.println("LENGTH: " + length); 
      nosofpackets = Math 
        .ceil(((int) selectedfile.length())/packetsize); 
      // progressBar.setValue(50); 
      bis = new BufferedInputStream(fis); 
      String message = length + "`~`" + filename; 
      byte[] data = message.getBytes(); 
      theOutput = new DatagramPacket(data, data.length, 
        InetAddress.getByName(remoteIPaddress),8888); 
      theSocket.send(theOutput); 
      byte[] newbytearray = new byte[packetsize]; 

      int dur = (int) (selectedfile.length()); 
      int dur1 = dur/100; 
      int counter = 0; 

      System.out.println("duration: " + dur + "nosofpackets: " 
        + nosofpackets + "dur1: " + dur1); 
      int val = 0; 
      for (int i = 0; i <= nosofpackets ; i++) { 
       os = sock.getOutputStream(); 
       bis.read(newbytearray, 0, newbytearray.length); 
       os.write(newbytearray, 0, newbytearray.length); 
       counter = counter + newbytearray.length; 

       val = counter/dur1; 
       System.out.println(val); 
       os.flush(); 
      } 
      System.out.println(val); 
      os.flush(); 
      os.close(); 
      sock.close(); 
      theSocket.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
      } 
     }; 

     new Thread(runnable).start(); 
    } 

} 


FileClient CODE: 


import java.net.*; 
import java.io.*; 

public class FileClientPC 
{ 
    public static void main(String[] args) throws IOException 
    { 
     long start = System.currentTimeMillis(); 

     String remoteip="192.168.43.237"; 

     Socket sock = new Socket(remoteip, 1500); 
     System.out.println("Connecting..."); 

     InputStream is = sock.getInputStream(); 
     DatagramSocket ds = new DatagramSocket(8888); 

     byte[] buffer = new byte[65507]; 
     DatagramPacket dp = new DatagramPacket(buffer, buffer.length); 

     try { 
      ds.receive(dp); 
     } catch (IOException e) { 
      System.err.println("chat error2 " + e); 
     } 

     String s = new String(dp.getData(), 0, dp.getLength()); 

     String temp[]=s.split("`~`"); 

     String size = temp[0]; 
     String name = temp[1]; 

     System.out.println(size +" "+ name); 
     long sizeint = Integer.parseInt(size); 
     System.out.println("Filename: " + name); 

     FileOutputStream fos = new FileOutputStream("D:\\"+name); 
     BufferedOutputStream bos = new BufferedOutputStream(fos); 

     int packetsize = 1024; 

     double nosofpackets = Math.ceil(sizeint/packetsize); 

     System.out.println("No of packets: "+Double.toString(nosofpackets)); 

     byte newbytearray[]; 
     for (int i = 0; i <= nosofpackets; i++) { 
      is = sock.getInputStream(); 
      newbytearray = new byte[packetsize]; 
      int bytesRead = is.read(newbytearray, 0, newbytearray.length); 
      System.out.println("Packet:" + (i + 1)); 
      bos.write(newbytearray, 0, newbytearray.length); 
     } 

     long end = System.currentTimeMillis(); 
     bos.close(); 
     sock.close(); 
     ds.close(); 
    } 
} 
+0

ファイアウォールは有効になっていますか? –

+0

がファイアウォールを無効にしようとしましたが、問題は解決しません。私は今、自分の論理に問題があると思っています。 –

+0

時々ルータはUDP –

答えて

0

あなたが聴きたいどのインターフェイスを指定する必要があります。デフォルトはローカルインタフェースです。 (おそらくOS依存)。 APIServerSocketは、これがどのように機能するかを説明します(コンストラクタとbind()関数など)。

関連する問題