程序有一个客户端一个服务器端2个界面,用的 Socket写的
我想问的是 当客户端发信息过去的时候,服务器怎么能马上就知道呢?最好马上能在服务器端的文本域里显示,有没类似的监听器?不然我只能在按钮代码里写 in.readLine()来读数据,而且当客户端没发消息的时候,会死机请问下是什么原因?我的代码写的稀烂,不好意思,刚学:
Client:
public class Client extends JFrame implements ActionListener
{
Socket so=null;
JTextField text=null;
JTextArea textarea=null;
JButton sendButton=null;
JButton conButton=null;
BufferedReader in=null;
PrintWriter out=null;
public Client()
{
super.setTitle("客户端");
text=new JTextField(25);
textarea=new JTextArea(10,25);
sendButton=new JButton("发送");
sendButton.setActionCommand("send");
sendButton.addActionListener(this);
conButton=new JButton("连接");
conButton.setActionCommand("con");
conButton.addActionListener(this);
JPanel p=new JPanel();
p.setLayout(new BorderLayout());
p.add(text,BorderLayout.NORTH);
p.add(textarea,BorderLayout.CENTER);
p.add(sendButton,BorderLayout.PAGE_END);
p.add(conButton,BorderLayout.EAST);
this.getContentPane().add(p);
this.pack();
this.setVisible(true);
}
public static void main(String[] args)
{
//JFrame.setDefaultLookAndFeelDecorated(true);
Client c=new Client();
}
public void actionPerformed(ActionEvent e)
{
if("send".equals(e.getActionCommand()))
{
try{
out.println(text.getText());
// if(in.readLine()!=null)
textarea.setText(textarea.getText()+"\n"+in.readLine());
}
catch(Exception ae)
{
}
}
if("con".equals(e.getActionCommand()))
{
try{
so=new Socket(InetAddress.getLocalHost().getHostAddress(),8800);
in=new BufferedReader(new InputStreamReader(so.getInputStream()));
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(so.getOutputStream())),true);
textarea.setText(in.readLine());
}
catch(Exception e1)
{
System.out.println ("Client"+e1.toString());
}
}
}
}Server:
public class Server extends JFrame implements ActionListener
{
ServerSocket ss=null;
Socket so=null;
JTextField text=null;
JTextArea textarea=null;
JButton sendButton=null;
BufferedReader in=null;
PrintWriter out=null;
public Server()
{
super.setTitle("服务器端");
text=new JTextField(25);
textarea=new JTextArea(10,25);
sendButton=new JButton("发送");
sendButton.setActionCommand("send");
sendButton.addActionListener(this);
JPanel p=new JPanel();
p.setLayout(new BorderLayout());
p.add(text,BorderLayout.NORTH);
p.add(textarea,BorderLayout.CENTER);
p.add(sendButton,BorderLayout.PAGE_END);
this.getContentPane().add(p);
this.pack();
this.setVisible(true);
try{
ss=new ServerSocket(8800);
so=ss.accept();
System.out.println ("accp");
in=new BufferedReader(new InputStreamReader(so.getInputStream()));
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(so.getOutputStream())),true);
out.println("连接成功");
}
catch(Exception e1)
{
System.out.println ("Server"+e1.toString());
} }
public static void main(String[] args)
{
//JFrame.setDefaultLookAndFeelDecorated(true);
Server c=new Server();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("send"))
{
try
{
textarea.setText(textarea.getText()+"\n"+in.readLine());
out.println(text.getText());
}
catch(Exception ae)
{
ae.printStackTrace();
}
}
}
}
我想问的是 当客户端发信息过去的时候,服务器怎么能马上就知道呢?最好马上能在服务器端的文本域里显示,有没类似的监听器?不然我只能在按钮代码里写 in.readLine()来读数据,而且当客户端没发消息的时候,会死机请问下是什么原因?我的代码写的稀烂,不好意思,刚学:
Client:
public class Client extends JFrame implements ActionListener
{
Socket so=null;
JTextField text=null;
JTextArea textarea=null;
JButton sendButton=null;
JButton conButton=null;
BufferedReader in=null;
PrintWriter out=null;
public Client()
{
super.setTitle("客户端");
text=new JTextField(25);
textarea=new JTextArea(10,25);
sendButton=new JButton("发送");
sendButton.setActionCommand("send");
sendButton.addActionListener(this);
conButton=new JButton("连接");
conButton.setActionCommand("con");
conButton.addActionListener(this);
JPanel p=new JPanel();
p.setLayout(new BorderLayout());
p.add(text,BorderLayout.NORTH);
p.add(textarea,BorderLayout.CENTER);
p.add(sendButton,BorderLayout.PAGE_END);
p.add(conButton,BorderLayout.EAST);
this.getContentPane().add(p);
this.pack();
this.setVisible(true);
}
public static void main(String[] args)
{
//JFrame.setDefaultLookAndFeelDecorated(true);
Client c=new Client();
}
public void actionPerformed(ActionEvent e)
{
if("send".equals(e.getActionCommand()))
{
try{
out.println(text.getText());
// if(in.readLine()!=null)
textarea.setText(textarea.getText()+"\n"+in.readLine());
}
catch(Exception ae)
{
}
}
if("con".equals(e.getActionCommand()))
{
try{
so=new Socket(InetAddress.getLocalHost().getHostAddress(),8800);
in=new BufferedReader(new InputStreamReader(so.getInputStream()));
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(so.getOutputStream())),true);
textarea.setText(in.readLine());
}
catch(Exception e1)
{
System.out.println ("Client"+e1.toString());
}
}
}
}Server:
public class Server extends JFrame implements ActionListener
{
ServerSocket ss=null;
Socket so=null;
JTextField text=null;
JTextArea textarea=null;
JButton sendButton=null;
BufferedReader in=null;
PrintWriter out=null;
public Server()
{
super.setTitle("服务器端");
text=new JTextField(25);
textarea=new JTextArea(10,25);
sendButton=new JButton("发送");
sendButton.setActionCommand("send");
sendButton.addActionListener(this);
JPanel p=new JPanel();
p.setLayout(new BorderLayout());
p.add(text,BorderLayout.NORTH);
p.add(textarea,BorderLayout.CENTER);
p.add(sendButton,BorderLayout.PAGE_END);
this.getContentPane().add(p);
this.pack();
this.setVisible(true);
try{
ss=new ServerSocket(8800);
so=ss.accept();
System.out.println ("accp");
in=new BufferedReader(new InputStreamReader(so.getInputStream()));
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(so.getOutputStream())),true);
out.println("连接成功");
}
catch(Exception e1)
{
System.out.println ("Server"+e1.toString());
} }
public static void main(String[] args)
{
//JFrame.setDefaultLookAndFeelDecorated(true);
Server c=new Server();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("send"))
{
try
{
textarea.setText(textarea.getText()+"\n"+in.readLine());
out.println(text.getText());
}
catch(Exception ae)
{
ae.printStackTrace();
}
}
}
}
public class ListeningThread{
ServerSocket ss=null;
public void run(){
String ip="127.0.0.1";
int port=9999;
ss=new ServerSocket(ip,port);
while(true){
Socket s=ss.accept();
ServiceThread st=new ServiceThread(s);
st.start();
}
}
}//ServiceThread.java
public class ServiceThread{
Socket s=null; public ServiceThread(Socket s){
this.s=s;
} public void run(){
//做readLine()等事情
}
}
BufferedReader in = new BufferedReader(new InputStreamReader(ss.getInput()));while(in.readLine()!=null){
String data = in.readLine();
}