主题:chat问题
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import java.io.*;
public class ChatClient extends Frame {
TextArea ta = new TextArea();
BufferedWriter bw;
TextField tf = new TextField();
Socket s;
public void launchFrame() {
setBounds(30, 40, 300, 400);
add(ta, BorderLayout.NORTH);
add(tf, BorderLayout.SOUTH);
pack();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
disConnect();
System.exit(0);
}
});
ta.setEditable(false);
tf.addActionListener(new TfListener());
setVisible(true);
connect();
}
private class TfListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String st = tf.getText().trim();
ta.append("client:" + st + "\n");
tf.setText("");
try {
bw.write(st);
bw.flush();
//connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public void connect(){
try {
s = new Socket("localhost",9999);
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
System.out.println("Conncet");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void disConnect(){
try {
bw.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new ChatClient().launchFrame();
}
}
import java.io.IOException;
import java.net.*;
import java.io.*;
public class ChatServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(9999);
while(true){
Socket s = ss.accept();
System.out.println("A client has connected");
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String s1 = br.readLine();
System.out.println(s1);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
问题:怎么我写的只有client端关闭后,服务器端才可以打印
郁闷,请赐教,谢谢!
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import java.io.*;
public class ChatClient extends Frame {
TextArea ta = new TextArea();
BufferedWriter bw;
TextField tf = new TextField();
Socket s;
public void launchFrame() {
setBounds(30, 40, 300, 400);
add(ta, BorderLayout.NORTH);
add(tf, BorderLayout.SOUTH);
pack();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
disConnect();
System.exit(0);
}
});
ta.setEditable(false);
tf.addActionListener(new TfListener());
setVisible(true);
connect();
}
private class TfListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String st = tf.getText().trim();
ta.append("client:" + st + "\n");
tf.setText("");
try {
bw.write(st);
bw.flush();
//connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public void connect(){
try {
s = new Socket("localhost",9999);
bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
System.out.println("Conncet");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void disConnect(){
try {
bw.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new ChatClient().launchFrame();
}
}
import java.io.IOException;
import java.net.*;
import java.io.*;
public class ChatServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(9999);
while(true){
Socket s = ss.accept();
System.out.println("A client has connected");
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String s1 = br.readLine();
System.out.println(s1);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
问题:怎么我写的只有client端关闭后,服务器端才可以打印
郁闷,请赐教,谢谢!