2013-02-03 104 views
-1

讓我解釋我的工作。TCP讀取文件併發送到java中的客戶端

我必須從服務器發送文件到客戶端。在服務器中,我接受了來自客戶端的連接,但未關閉連接。現在我必須將數據作爲流發送給客戶端。

現在我已經選擇了一個內容,並在方法中寫入了一個文件。

我有發送方法,我必須將輸入流中的文件發送到客戶端。如何發送?

 public static void main(String args[]) 
{ 



    int port=5000; 
    while(true) 
    { 
     try 

    { 
    ServerSocket ser=new ServerSocket(port+10); 

    System.out.println("CLIENT A IS CONNECTED"); 

    ser.close(); 

    } 
    catch(Exception e) 
    { 
     System.out.println(e); 
    } 
     try 
    { 
    ServerSocket ser1=new ServerSocket(port+20); 

    ser1.accept(); 

    System.out.println("CLIENT B IS CONNECTED"); 
    } 
    catch(Exception e) 
    { 
     System.out.println(e); 
    } 
     try 
    { 
    ServerSocket ser2=new ServerSocket(port+30); 

    ser2.accept(); 

    System.out.println("CLIENT C IS CONNECTED"); 
    } 
    catch(Exception e) 
    { 
     System.out.println(e); 
    } 
     try 
    { 
    ServerSocket ser3=new ServerSocket(port+40); 

    ser3.accept(); 

    System.out.println("CLIENT D IS CONNECTED"); 
    } 
    catch(Exception e) 
    { 
     System.out.println(e); 
    } 

    } 
} 

在主要方法我接受所有的客戶端請求。

 private void jButton1ActionPerformed1(java.awt.event.ActionEvent evt) //sendbutton 

    { 
    try 
    { 
    FileReader buf=new FileReader("e:\\input.txt"); 

    int port= // the port of client which I selected to send the data 
    try 
    { 

     @SuppressWarnings("resource") 
     ServerSocket ser=new ServerSocket(port); 
     Socket soc=ser.accept(); 
     BufferedReader toclient=new BufferedReader(buf); 
     DataOutputStream dos=new DataOutputStream(soc.getOutputStream()); 
     System.out.println(dos.toString()); 

     dos.flush(); 
     dos.close(); 




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

    } 

現在我的問題是我已經打開了端口,並在主要方法中建立連接。我必須通過選擇哪個客戶端應該以sendbutton方法接收數據來將數據從服務器發送到客戶端。我很困惑如何檢查或傳遞套接字對象serv1到發送方法?

下面
+3

你不清楚你想完成什麼。你提供的短代碼甚至不會編譯('Socket'中沒有'accept'方法;它屬於'ServerSocket')。看起來你只需要一個簡單的服務器 - 客戶端通信就可以了,對此,有大量的教程:https://www.google.com/search?q=java+socket+communication – gcvt

+0

我編輯了代碼。 @Dejan。請幫忙 – Ameer

+2

這段代碼是無稽之談。調用accept()而不保存結果是無稽之談。這個問題是無稽之談。閱讀網絡教程並重試。 – EJP

回答

0

是樣本服務器代碼(請添加相應的import語句和你的邏輯)

class SampleServer 
{ 
    private int port; 
    private ArrayList clientList; 

    public SampleServer() 
    { 
     this.port = 4444; 
    } 

    public SampleServer(int port) 
    { 
     this.port = port; 
    } 

    public void startServer() 
    { 
     ServerSocket ss = new ServerSocket(this.port); 
     Socket soc; 
     while(true) 
     { 
      soc = ss.accept(); 
      clientList.add(soc); 
     } 
    } 

    public ArrayList getClientList() 
    { 
     return clientList; 
    } 
} 

下面是示例主要方法(以此爲參考,並創建自己的代碼)

public class Main 
{ 
    public static void main(String[] args) throws Exception 
    { 
     //Create a server 
     //Server has to be only one and multiple clients can connect it 
     SampleServer server = new SampleServer(5555); 
     server.startServer(); 

     //Creating one client 
     Socket soc = new Socket("localhost", 5555); 

     //Similarly create n-number of clients and connect to the same port as that of server 

     //server.getClientList(); will help you get the list of the clients connected to the server 
    } 
} 
相關問題