java关于编写客户端和服务器聊天
//客户端import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class MyClient extends WindowAdapter implements ActionListener
{
Frame f;
TextField tf;
TextArea ta;
Button send;
Button close;
Button con;
BufferedReader in;
Socket s;
PrintStream out;
public static void main(String args[]) throws IOException
{
MyClient mc =new MyClient();
mc.go();
}
public void go()throws IOException
{
f=new Frame("客户端");
f.setLayout(null);
f.setSize(500,450);
f.setVisible(true);
f.setBackground(Color.yellow);
f.addWindowListener(this);
ta=new TextArea(20,250);
ta.setBounds(50,50,400,250);
f.add(ta);
tf=new TextField();
tf.setBounds(110,320,340,25);
f.add(tf);
Label l=new Label("发送消息:");
l.setBounds(50,320,50,25);
f.add(l);
con=new Button("连接");
con.setBounds(60,365,60,25);
con.addActionListener(this);
f.add(con);
send=new Button("发送");
send.setBounds(200,365,60,25);
send.addActionListener(this);
f.add(send);
close = new Button("关闭");
close.setBounds(360,365,60,25);
close.addActionListener(this);
f.add(close);
try
{
while(true)
{
String strs=in.readLine();
ta.append("来自服务器信息:"+strs+"\n");
}
}
catch(IOException e)
{}
}
public void actionPerformed(ActionEvent ae)
{
//单击发送按钮事件
if(ae.getSource() == send)
{
String strc=tf.getText();
ta.append("客户端信息:"+strc+"\n");
out.println(strc);
tf.setText(" ");
}
//单击关闭按钮事件
if(ae.getSource() == close)
{
try
{
System.exit(0);
out.close();
in.close();
s.close();
}catch(IOException e){}
}
if(ae.getSource() == con)
{
try
{
s=new Socket("127.0.0.1",5432);
out=new PrintStream(s.getOutputStream());
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
ta.append("已成功连接到服务器\n");
con.setEnabled(false);
}catch(IOException e)
{
ta.append("连接服务器失败\n");
}
}
}
public void windowClosing(WindowEvent ev)
{
System.exit(0);
}
}
//服务器端
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class MyServer extends WindowAdapter implements ActionListener
{
Frame f;
TextField tf;
TextArea ta;
Button send;
Button close;
ServerSocket ss;
Socket s;
PrintStream out;
BufferedReader in;
InetAddress clientIP;
public static void main(String args[])throws IOException
{
MyServer ms =new MyServer();
ms.go();
}
public void go()throws IOException
{
f=new Frame("服务器端");
f.setLayout(null);
f.setSize(500,450);
f.setVisible(true);
f.setBackground(Color.yellow);
f.addWindowListener(this);
ta=new TextArea(20,250);
ta.setBounds(50,50,400,250);
f.add(ta);
tf=new TextField();
tf.setBounds(110,320,340,25);
f.add(tf);
Label l=new Label("发送消息:");
l.setBounds(50,320,50,25);
f.add(l);
send=new Button("发送");
send.setBounds(70,365,60,25);
send.addActionListener(this);
f.add(send);
close = new Button("关闭");
close.setBounds(300,365,60,25);
close.addActionListener(this);
f.add(close);
try
{
ss=new ServerSocket(5432);
s=ss.accept();
out=new PrintStream(s.getOutputStream());
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
clientIP=s.getInetAddress();
ta.append("客户端以连接,IP地址为:"+clientIP+"\n");
while(true)
{
String strc=in.readLine();
ta.append("来自客户端信息:"+strc+"\n");
}
}catch(IOException e)
{
}
}
public void actionPerformed(ActionEvent ae)
{
//单击发送按钮事件
if(ae.getSource() == send)
{
String strs=tf.getText();
ta.append("服务器端信息:"+strs+"\n");
out.println(strs);
tf.setText(" ");
}
//单击关闭按钮事件
if(ae.getSource() == close)
{
try
{
System.exit(0);
out.close();
in.close();
s.close();
}catch(IOException e){}
}
}
public void windowClosing(WindowEvent ev)
{
System.exit(0);
}
}
小弟最近在学java,这是今天写的一客户端和服务器聊天小程序,现在所遇到的问题是:
为什么在连接成功后客户端看不到服务器端发来的信息,而服务器端却可以看到客户端发来的信息,
当我不将事件写到“连接”按钮而写到客户端的第一个try语句中时又可以正常通信,
请各位高人指点指点!