2013-10-27 143 views
0

我是相對較新的客戶端服務器編程,我試圖讓客戶端和服務器之間的簡單通信小程序gui,它現在工作,但我得到一個例外當我運行客戶端客戶端與服務器之間的小程序通信

修改後的代碼:

客戶:

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

import javax.swing.*; 


public class Client extends JApplet{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private JTextArea displayArea; 
    private Socket socket = null; 
    private DataInputStream input; 
    private DataOutputStream output; 


    public void init() 
    { 

     Container container = getContentPane(); 

     // set up JTextArea to display 
     displayArea = new JTextArea(); 
     displayArea.setEditable(false); 
     container.add(new JScrollPane(displayArea), BorderLayout.SOUTH); 

     try 
     { 
      // make connection 
      socket = new Socket(getCodeBase().getHost(), 5423); 

      // get streams 
      input = new DataInputStream(socket.getInputStream()); 
      output = new DataOutputStream(socket.getOutputStream()); 


      while(true) 
      { 
       output.writeUTF("hello from client"); //send message to server 
       output.flush(); 

       processMessage(input.readUTF()); //process data from server  
      } 
     } 
     // catch problems setting up connection and streams 
     catch (IOException ioException) { 
      ioException.printStackTrace(); 
     } 

     try 
     { 
      input.close(); 
      output.close(); 
     } 
     catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 


    // process message received by client 
    private void processMessage(String message) 
    { 
     if (message.equals("hello")) 
     { 
      displayMessage("Connected to server"); 
     } 
    } 


    private void displayMessage(final String messageToDisplay) 
    { 

     SwingUtilities.invokeLater(
       new Runnable() { 

        public void run() 
        { 
         displayArea.append(messageToDisplay); 
         displayArea.setCaretPosition(displayArea.getText().length()); 
        } 
       } 
      ); 
    } 

} 

服務器:

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

import javax.swing.*; 


public class Server extends JFrame{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private ServerSocket serverSocket; 
    private JTextArea outputArea; 
    private Socket socket = null; 
    private DataInputStream input; 
    private DataOutputStream output; 


    public Server() 
    { 

     super("Server"); 

     try 
     { 
      serverSocket = new ServerSocket(5423, 1); 
     } 
     catch(IOException ioException) 
     { 
      ioException.printStackTrace(); 
      System.exit(1); 
     } 


     outputArea = new JTextArea(); 
     getContentPane().add(outputArea, BorderLayout.CENTER); 
     outputArea.setText("Server awaiting connections\n"); 


     setSize(300, 300); 
     setVisible(true); 
    } 


    private void doThis() throws IOException 
    { 
     try 
     { 
      while(true) 
      { 

       socket = serverSocket.accept(); 

       input = new DataInputStream(socket.getInputStream()); 
       output = new DataOutputStream(socket.getOutputStream()); 

       output.writeUTF("hello"); 
       outputArea.append(input.readUTF()); //receive data from client 
       output.flush();  
      } 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 
     finally 
     { 
      output.close(); 
      input.close(); 
      socket.close(); //close connection to client 
     } 
    } 


    public static void main(String[] args) throws IOException 
    { 
     Server server = new Server(); 
     server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     server.doThis();  
    } 
} 

異常讀取:

java.net.SocketException異常:在在 java.net.SocketInputStream.read(未知來源) java.net.SocketInputStream.read(未知來源)在 連接重置java.net .SocketInputStream.read(Unknown Source)at java.io.DataInputStream.readUnsignedShort(Unknown Source)at java.io.DataInputStream.readUTF(Unknown Source)at java.io.DataInputStream.readUTF(Unknown Source)at Client .init(Client.java:45)at sun.applet.AppletPanel.run(Unknown Source)at java.lang.Thread.run(Unknown Source)

+1

什麼是例外? –

+0

嗨,我編輯了主帖,包括例外 – Conor

+0

看看你的代碼在第41行。那裏會發生什麼? –

回答

0

現在您的服務器出現故障。
將ServerSocket應創建一次(因爲它是在你的代碼),
然後ServerSocket的偵聽器應該等待接受新的連接 - 每個客戶端之一,當有新的客戶端嘗試連接時纔會發生,
當新的客戶端連接接受()一個新的線程應該處理每個客戶端,並且在該線程中你應該打開IO流ONCE,然後創建一個循環讀/寫/處理數據,IO流應該只在數據處理循環完成後關閉一次。

+0

我注意到,我不應該關閉客戶端上的流,因爲服務器正在請求一個字符串從它已經改變了,所以流在服務器上關閉了,我也拿走了「while true」循環,因爲它在這裏沒有用處,所以現在我沒有例外。過了一段時間,我會嘗試將自己的完成答案發布給我自己的問題,如果您認爲我仍然在做一些奇怪的事情,請將您的答覆發佈給我,我將不勝感激。 – Conor

0

試圖採取張貼的建議,沒有例外出現這個時候

客戶:

import java.awt.*; 
import java.net.*; 
import java.io.*; 
import javax.swing.*; 

public class Client extends JApplet{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private JTextArea displayArea; 
    private Socket socket = null; 
    private DataInputStream input; 
    private DataOutputStream output; 

    @Override 
    public void init() 
    { 
     Container container = getContentPane(); 

     // set up JTextArea to display 
     displayArea = new JTextArea(); 
     displayArea.setEditable(false); 
     container.add(new JScrollPane(displayArea), BorderLayout.SOUTH); 

     try 
     { 
      // make connection 
      socket = new Socket(getCodeBase().getHost(), 8020); 

      // get streams 
      input = new DataInputStream(socket.getInputStream()); 
      output = new DataOutputStream(socket.getOutputStream()); 

      output.writeUTF("hello from client"); //send message to server 

      processMessage(input.readUTF()); //process data from server  
     } 
     // catch problems setting up connection and streams 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 


    // process message received by client 
    private void processMessage(String message) throws IOException 
    { 
     if (message.equals("hello")) 
     { 
      displayMessage("Connected to server");  
     } 
    } 

    private void displayMessage(final String messageToDisplay) 
    { 

     SwingUtilities.invokeLater(
       new Runnable() { 

        public void run() 
        { 
         displayArea.append(messageToDisplay); 
         displayArea.setCaretPosition(displayArea.getText().length()); 
        } 
       } 
      ); 
    } 

} 

服務器:

import java.awt.*; 
import java.net.*; 
import java.io.*; 
import javax.swing.*; 


public class Server extends JFrame{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private JTextArea outputArea; 
    private DataInputStream input; 
    private DataOutputStream output; 


    public Server() 
    { 
     super("Server");  

     outputArea = new JTextArea(); 
     getContentPane().add(outputArea, BorderLayout.CENTER); 

     SwingUtilities.invokeLater(
       new Runnable() { 

        public void run() 
        { 
         outputArea.setText("Server awaiting connections\n"); 
        } 
       } 
      ); 


     setSize(300, 300); 
     setVisible(true); 
    } 


    private void doThis() throws IOException 
    { 
     ServerSocket serverSocket = null; 
     Socket socket = null; 

     try 
     { 
      serverSocket = new ServerSocket(8020, 1); 
     } 
     catch(IOException ioException) 
     { 
      ioException.printStackTrace(); 
      System.exit(1); 
     } 


     try 
     { 
      while(true) 
      { 
       socket = serverSocket.accept(); 

       input = new DataInputStream(socket.getInputStream()); 
       output = new DataOutputStream(socket.getOutputStream()); 

       output.writeUTF("hello"); //send a string to client 
       outputArea.append(input.readUTF()); //receive data from client and append the string to the text area 


      } 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 

     finally 
     { 
      socket.close(); 
     } 
    } 


    public static void main(String[] args) 
    { 
     new Thread(new Runnable() 
     { 
      @Override 
      public void run() 
      { 

       Server server = new Server(); 
       server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       try 
       { 
        server.doThis();   
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
       } 

      } 
     }).start(); 
    } 
} 
相關問題