2012-05-13 43 views
1

我試圖通過套接字實現send-receive示例,但效果不佳。發送方成功發送數據,Receiver接收數據並在控制檯中顯示,但我想將這些數據保存在一個文件中,但我不能。當我注意到接收器保持listeninig而不結束while循環。那麼誰能幫我解決這個問題?在java中通過套接字連接接收數據時無法寫入文件

發件人模塊

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


public class UDPSend { 


public static void main(String[] args) throws IOException 
    { 
     FileInputStream fstream = new FileInputStream("T.txt"); 
     DataInputStream in = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 

     File file = new File("T.txt"); 
     FileInputStream fis = new FileInputStream(file); 
     byte[] fsize = new byte[(int) file.length()]; 
     int size = fis.read(fsize); 
       System.out.println("Size = " + size); 



     InetAddress addr = InetAddress.getByName("localhost"); 
     byte[] buf = new byte[10000]; 

     String DataLine; 
     while ((DataLine = br.readLine()) != null) 
     { 
       DatagramPacket packet =new DatagramPacket(DataLine.getBytes(),   DataLine.length() , addr, 4555); 
      System.out.println (DataLine); 
      DatagramSocket socket = new DatagramSocket(); 
      socket.send(packet); 

     }//end while loop 


    }//end main method 

} 

接收器模塊

import java.io.*; 
import java.net.*; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 

public class UDPRecieve { 



public static void main(String args[]) throws Exception 
     { 

     FileWriter fw = new FileWriter(new File("D:/JavaPrograms/Multimedia-proj-2/src/outtt.txt")); 
     fw.write("hi"); 
     try{ 
     //DatagramSocket serverSocket = new DatagramSocket(4555); 
     DatagramSocket Socket = new DatagramSocket(4555); 
     byte[] receiveData = new byte[1000000];  
    // byte[] sendData = new byte[1024]; 
     //while(true) 
     while(receiveData !=null) 
     { 
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 
      Socket.receive(receivePacket); 
      String sentence = new String(receivePacket.getData()); 
      fw.write(sentence); 
      fw.flush(); 
      System.out.printf("RECEIVED: %s " , new String(receivePacket.getData())); 
      //System.out.println("Done"); 
      //InetAddress IPAddress = receivePacket.getAddress(); 
      //int port = receivePacket.getPort(); 
      //String capitalizedSentence = sentence.toUpperCase(); 
      /* sendData = capitalizedSentence.getBytes(); 
      DatagramPacket sendPacket = 
      new DatagramPacket(sendData, sendData.length, IPAddress, port); 
      serverSocket.send(sendPacket);*/ 
     } 


     fw.flush(); 
     fw.close(); 

     } catch (Exception e) { 
      System.err.println(e); 
     } 

    } 
} 

預先感謝。

+1

當你說 「我想要保存在文件中這個數據,我不能」 你是什麼意思?你會得到一個例外?該文件只是空的? – shem

+0

這是一個邏輯錯誤。我可以在命令行上接收數據,但沒有任何內容打破了while循環。 –

+0

這就是我的代碼需要使用UDP協議的問題 – keyser

回答

1

您可以通過以下代碼更改來實現它。Receiver類在以下循環中進行更改。

while (receiveData != null) { 
       DatagramPacket receivePacket = new DatagramPacket(receiveData, 
         receiveData.length); 
       Socket.receive(receivePacket); 
       String sentence = new String(receivePacket.getData()); 
       fw.write(sentence.trim()); 
       fw.flush(); 
       System.out.printf("RECEIVED: %s ", new String(receivePacket 
         .getData())); 
       // System.out.println("Done"); 
       // InetAddress IPAddress = receivePacket.getAddress(); 
       // int port = receivePacket.getPort(); 
       // String capitalizedSentence = sentence.toUpperCase(); 
       /* 
       * sendData = capitalizedSentence.getBytes(); DatagramPacket 
       * sendPacket = new DatagramPacket(sendData, sendData.length, 
       * IPAddress, port); serverSocket.send(sendPacket); 
       */ 
      } 

編輯其成功運行程序的

完整代碼。

發件人

import java.io.BufferedReader; 
import java.io.DataInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetAddress; 


public class UDPSend { 


public static void main(String[] args) throws IOException 
    { 
     FileInputStream fstream = new FileInputStream("D:/T.txt"); 
     DataInputStream in = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 

     File file = new File("D:/T.txt"); 
     FileInputStream fis = new FileInputStream(file); 
     byte[] fsize = new byte[(int) file.length()]; 
     int size = fis.read(fsize); 
       System.out.println("Size = " + size); 



     InetAddress addr = InetAddress.getByName("localhost"); 
     byte[] buf = new byte[10000]; 

     String DataLine; 
     while ((DataLine = br.readLine()) != null) 
     { 
       DatagramPacket packet =new DatagramPacket(DataLine.getBytes(),   DataLine.length() , addr, 4555); 
      System.out.println (DataLine); 
      DatagramSocket socket = new DatagramSocket(); 
      socket.send(packet); 

     }//end while loop 


    }//end main method 

} 

接收機

import java.io.File; 
import java.io.FileWriter; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 

public class UDPReceive { 

    public static void main(String args[]) throws Exception { 

     FileWriter fw = new FileWriter(new File(
       "D:/outtt.txt")); 
     fw.write("hi"); 
     try { 
      // DatagramSocket serverSocket = new DatagramSocket(4555); 
      DatagramSocket Socket = new DatagramSocket(4555); 
      byte[] receiveData = new byte[1000000]; 
      // byte[] sendData = new byte[1024]; 
      // while(true) 
      while (receiveData != null) { 
       DatagramPacket receivePacket = new DatagramPacket(receiveData, 
         receiveData.length); 
       Socket.receive(receivePacket); 
       String sentence = new String(receivePacket.getData()); 
       fw.write(sentence.trim()); 
       fw.flush(); 
       System.out.printf("RECEIVED: %s ", new String(receivePacket 
         .getData())); 
       // System.out.println("Done"); 
       // InetAddress IPAddress = receivePacket.getAddress(); 
       // int port = receivePacket.getPort(); 
       // String capitalizedSentence = sentence.toUpperCase(); 
       /* 
       * sendData = capitalizedSentence.getBytes(); DatagramPacket 
       * sendPacket = new DatagramPacket(sendData, sendData.length, 
       * IPAddress, port); serverSocket.send(sendPacket); 
       */ 
      } 

      fw.flush(); 
      fw.close(); 

     } catch (Exception e) { 
      System.err.println(e); 
     } 

    } 
} 
+0

我測試了它,並且遇到同樣的問題。 –

+0

@SaharNadi我已經更新了在我的機器上成功運行的整個程序。 –

+0

非常感謝,現在工作順利。 –

0

看看你的接收線圈:

while(receiveData != null) 
{ 
    DatagramPacket receivePacket = new DatagramPacket(receiveData, 
                 receiveData.length); 
    Socket.receive(receivePacket); 
    String sentence = new String(receivePacket.getData()); 
    fw.write(sentence); 
    fw.flush(); 
    System.out.printf("RECEIVED: %s " , new String(receivePacket.getData())); 
} 

撇開各種字符編碼問題等等,你會怎麼曾經預計receiveData成爲該循環null?如何可以循環結束?

假設你正在使用UDP,而不是TCP,你真的沒有連接,所以你怎麼指望當作家已停止寫信告訴?另外,您應該考慮數據包無序到達的可能性。

+0

中的那種信息。那麼如何檢查收到的數據在循環中變爲空? –

+0

@SaharNadi:收到的數據*不會變爲空。它不像TCP那樣有一個連接會被關閉 - 你需要在協議中建立某種「完成」數據包。你也應該意識到接收數據包的可能性。我懷疑你應該閱讀UDP - 你正在嘗試使用它,就好像你有一個基於流的協議。 –

+0

非常感謝您的信息。 –

相關問題