2011-11-19 46 views
0

我正在做一個任務,即建立兩個java代碼通過套接字彼此進行通信。當我開始構建兩個基本代碼(一次通信)時,它運行良好,但是當我試圖讓它們保持通信時,兩者都發生了堵塞!它似乎是死鎖..兩個插座java代碼

請原諒我缺乏經驗,並幫助我..

我的服務器代碼,

/***************************************************************************************** 
* 
* 
* 
***************************************************************************************/ 

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

public class COMP_3270_Server implements Runnable { 
    public static void main(String[] args) { 

     try { 
      ServerSocket myServer = new ServerSocket(3270); 

      JOptionPane.showMessageDialog(null, "A server built over " + 
        InetAddress.getLocalHost().getHostAddress() + " : " + 
        myServer.getLocalPort() + "\nWaiting for masseges...", 
        "succeed", JOptionPane.INFORMATION_MESSAGE); 

      Socket channel = myServer.accept(); 
      BufferedReader incomes = new BufferedReader(
        new InputStreamReader(channel.getInputStream())); 
      PrintStream outgoes = new PrintStream(channel.getOutputStream()); 

      String mssg = null; 

      do { 
       mssg = JOptionPane.showInputDialog("New Message: " + incomes.readLine()); 
       outgoes.print(mssg); 
      } while (mssg != null); 

      JOptionPane.showMessageDialog(null, "The server will be closed now", 
        "Finish", JOptionPane.INFORMATION_MESSAGE); 
      channel.close(); 
      myServer.close(); 
     } catch (IOException e) { 
      System.out.println("Ops!, some thing went wrong. Please contect provider"); 
     } 
    } 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 

    } 
} 

和我的客戶端代碼,

/************************************************************* 
* 
* 
* 
* ***********************************************************/ 

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

import javax.swing.JOptionPane; 

public class COMP_3270_Client implements Runnable { 
    public static void main(String[] args) { 

     try { 
      Socket channel = new Socket("localhost", 3270); 
      BufferedReader incomes = new BufferedReader(
        new InputStreamReader(channel.getInputStream())); 
      PrintStream outgoes = new PrintStream(channel.getOutputStream()); 

      JOptionPane.showMessageDialog(null, "Connected to: " + 
        channel.getInetAddress().getHostAddress() + " : " + 
        channel.getPort(), "succeed", JOptionPane.INFORMATION_MESSAGE); 

      String mssg = "New client: " + channel.getLocalAddress().getHostName(); 
      outgoes.print(mssg); 
      System.out.println("222"); 

      do { 
       mssg = JOptionPane.showInputDialog("New Message: " + incomes.readLine()); 
       outgoes.print(mssg); 
      } while (mssg != null); 

      JOptionPane.showMessageDialog(null, "The channel will be closed now", 
        "Finish", JOptionPane.INFORMATION_MESSAGE); 
      channel.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      System.out.println("Ops!, some thing went wrong. Please contect provider"); 
     } 
    } 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
    } 
} 

它的幫助!!!!我幾乎完成了我的任務。 對於Socket問題,你看起來不錯!所以,我想採取一些好處;)

你有任何想法如何檢測消息是否拋出緩衝區爲空?我嘗試了幾種方法,但沒有任何工作!

如果u希望看到我的代碼後,你(也許有些人會需要它,

/***************************************************************************************** 
* 
***************************************************************************************/ 

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

public class COMP_3270_Server 
{ 

public static void main(String[] args) 
{ 
    try 
    { 
    ServerSocket myServer = new ServerSocket(3270); 
    JOptionPane.showMessageDialog (null, "A server built over " + 
     InetAddress.getLocalHost().getHostAddress() + " : " + 
     myServer.getLocalPort() + "\nWaiting for a client...", "succeed", JOptionPane.INFORMATION_MESSAGE); 
    Socket channel = myServer.accept();  
    BufferedReader income = new BufferedReader(new InputStreamReader(channel.getInputStream())); 
    PrintStream outgoes = new PrintStream(channel.getOutputStream()); 

    String resp = " "; 
    String mssg = " "; 
    do 
    { 
     if (income.ready()) 
     { 
      if(mssg.intern() == null) 
      { 
       int response = JOptionPane.showConfirmDialog(null, 
         "Client left sever, keep server alive?", "Client left", 
         JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 
       if (response == JOptionPane.NO_OPTION) 
       { 
        resp = null; 
       } 
       else if (response == JOptionPane.CLOSED_OPTION) 
       { 
        resp = null; 
       } 
       else if (response == JOptionPane.YES_OPTION) 
       { 
       } 
      } 
      else 
      { 
       resp = JOptionPane.showInputDialog("New client message: " + mssg); 
       outgoes.println(resp); 
      } 
     } 
    }while (resp != null && mssg != null); 

     JOptionPane.showMessageDialog (null, "The server will be closed now", "Finish", JOptionPane.INFORMATION_MESSAGE); 
     channel.close(); 
     myServer.close(); 

    } 
    catch (HeadlessException e) 
    { 
     JOptionPane.showMessageDialog (null, "A string not supported", "Error", JOptionPane.INFORMATION_MESSAGE); 
    } 
    catch (UnknownHostException e) 
    { 
     JOptionPane.showMessageDialog (null, "IP address not available", "Error", JOptionPane.INFORMATION_MESSAGE); 
    } 
    catch (IOException e) 
    { 
     JOptionPane.showMessageDialog (null, "Failed or interrupted I/O", "Error", JOptionPane.INFORMATION_MESSAGE); 
    } 
    } 
} 

/************************************************************* 
* 
* 
* 
* References : http://www.youtube.com/watch?v=aEDV0WlwXTs 
* ***********************************************************/ 

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

public class COMP_3270_Client 
{ 
    public static void main (String [] args) 
    { 
     try 
     { 
      String ip = JOptionPane.showInputDialog("Enter the IP you want to connect to: "); 
      Socket channel = new Socket(ip, 3270); 
      BufferedReader income = new BufferedReader(new InputStreamReader(channel.getInputStream())); 
      PrintStream outgoes = new PrintStream(channel.getOutputStream()); 

      String strt = JOptionPane.showInputDialog("Succeed! Connected. You can say something: "); 
      outgoes.println(strt); 

      String resp = " "; 
      String mssg = " "; 
      do 
      { 
       if (income.ready()) 
       { 
        mssg = income.readLine(); 

        if(mssg.intern() == null) 
        { 
         JOptionPane.showMessageDialog (null, "Server closed!", "Session end", 
           JOptionPane.INFORMATION_MESSAGE); 
         resp = null; 
         outgoes.println(resp); 
        } 
        else 
        { 
         resp = JOptionPane.showInputDialog("New server message: " + mssg); 
         outgoes.println(resp);     
        } 
       } 
      }while (resp != null && mssg != null); 

      JOptionPane.showMessageDialog (null, "The channel will be closed now", "Finish", JOptionPane.INFORMATION_MESSAGE);; 
      channel.close(); 
     } 
     catch (UnknownHostException e) 
     { 
      JOptionPane.showMessageDialog (null, "Wrong IP address or server reject connection", "Error", JOptionPane.INFORMATION_MESSAGE); 
     } 
     catch (IOException e) 
     { 
      JOptionPane.showMessageDialog (null, "Failed or interrupted I/O", "Error", JOptionPane.INFORMATION_MESSAGE); 
     } 
    } 
} 
+3

歡迎來到編程世界。第一個教訓是,你不要說'建立兩個Java代碼',而是'建立兩個Java類'或'兩個Java應用程序'或'兩個Java程序'。 – cherouvim

+0

讓我猜 - 你的客戶端確實發送了一個消息到服務器,之後,這兩個應用程序(客戶端+服務器)終止? – home

回答

0

從您使用readLine方法插槽讀取數據時,它會阻塞,直到一個完整的行已被讀取,更多讀取here。這從來沒有發生因爲當寫入插座print被使用。

嘗試使用println而不是print寫入套接字時。

此外,爲什麼您的課程實施Runnable?這不會添加任何內容,但會讓您的代碼難以閱讀。

請檢查您的拼寫 - 它被稱爲「消息」而不是「masseges」。什麼是「競爭提供者」?

+0

感謝vidstige! – krayyem

+0

@ user1055069感謝我接受答案,如果它幫助你。 :) – vidstige

+0

它的幫助!我幾乎完成了我的任務;) – krayyem