2012-12-14 18 views
0

使用Socket類當我試圖建立使用Swing的客戶端服務器聊天應用程序得到錯誤。在Swing

在,當我啓動客戶端我送,我可以接收在客戶現場,但是當我按下服務器站點一個按鈕,它不會打開一個新的幀(也就是在我的情況下,錯誤在這裏)的消息。

我該如何解決?任何人都能以其他方式暗示我嗎?

我的代碼是(服務器站點):

import java.net.*; 
import java.io.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 
import java.sql.*; 
class Cli extends JFrame 
{ 
    ServerSocket ss; 
    Socket soc; 
    InputStream in; 
    OutputStream out; 

    DataInputStream sin; 
    DataOutputStream sout; 

    JLabel l1; 
    JTextField txt1; 
    JButton addd; 
    Cli() throws Exception 
    { 
Class.forName("com.mysql.jdbc.Driver"); 
Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/nirav","root","root"); 
JPanel pan=new JPanel(new GridLayout(2,1)); 
ss=new ServerSocket(8888); 
//ss.setSOLimit(10000); 
l1=new JLabel("waiting for client"); 
txt1=new JTextField(40); 
addd=new JButton("click here to send"); 
addd.addActionListener(new ActionListener() 
     { 
    public void actionPerformed(ActionEvent ae) 
     { try{ 
       JOptionPane.showMessageDialog(null,"Data is successfully inserted into the database.");  
       l1.setText("waiting"); 
       Recieve c=new Recieve(); 
       c.setSize(300,300); 
       c.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 
       c.setVisible(true); 

       }catch(Exception e) 
       { 
       System.out.println(e.toString()); 
       } 
      }        
     }); 
soc=ss.accept(); 
in=soc.getInputStream(); 
out=soc.getOutputStream(); 
sin=new DataInputStream(in); 
sout=new DataOutputStream(out); 
String s=""; 
try{ 
    s=sin.readUTF();   
    }catch(Exception e) 
    { 
    } 
    l1.setText("client :"+s); 
pan.add(l1); 
pan.add(txt1); 
pan.add(addd); 
add(pan); 
} 
class Recieve extends JFrame 
{ 
JLabel msg; 
JTextField t1; 
JButton a1; 
Recieve() throws Exception 
{ 
JPanel pan=new JPanel(new GridLayout(2,1)); 
t1=new JTextField(40); 
a1=new JButton("click here to send"); 
//msg=new JLabel("waiting..."); 
a1.addActionListener(new ActionListener() 
{ public void actionPerformed(ActionEvent ae) 
     { try{ 
       String s; 
       String str="Server :"; 
       sout.writeUTF(t1.getText()); 
       //msg.setText(str); 
       sout.flush(); 
      }catch(Exception e) 
{ 
System.out.println(e.toString()); 
} 
}}); 
pan.add(t1); 
pan.add(a1); 
add(pan); 
    } 
    } 
    } 
class Server 
{ 
    public static void main(String args[]) throws Exception 
{ 
try{ 

Cli c=new Cli(); 
c.setSize(300,300); 
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
c.setVisible(true); 
} 
catch(Exception e) 
{ 
System.out.println(e.toString()); 
} 

} 
    } 
+2

1)請使用一致的和邏輯的縮進代碼塊。 2)*「在我的情況錯誤在這裏」*什麼錯誤?複製/粘貼它。但是首先改變'catch(Exception e) {System.out.println(e.toString());''catch(Exception e) {e.printStackTrace();'這不僅更簡單,但提供了更多有用的信息。 –

+0

粘貼錯誤代碼 –

+0

在此代碼當我運行它...它會告訴我通過客戶端發送消息,但是當我按下按鈕ADDD然後它不會打開一個新的JFrame(收到C ...),但它打印上面可用的消息。 – nik

回答

0

你是如何捕捉從服務器發來的消息?你需要實現的東西,這將使你的UI聽衆的使用提出了更新。只要應用程序從服務器接收數據的聊天窗口。

我只是做了一個快速的研究,發現這個項目中,嘗試使用自己的代碼作爲參考來實現你的程序: https://github.com/rafaelsakurai/socket-exemplo-chat-swing

+0

謝謝你...你鏈接的幫助,讓我的解決方案。 – nik