代码如下:
客户端:import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;public class ChatClient extends Frame {
Socket s = null;
DataOutputStream dos = null;
TextField tfTxt = new TextField();
TextArea tfContent = new TextArea(); public static void main(String[] args) {
new ChatClient().launchFrame(); }
public void launchFrame() {
setBounds(300, 300, 400, 400);
add(tfTxt, BorderLayout.SOUTH);
add(tfContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter(){ @Override
public void windowClosing(WindowEvent e) {
disConnect();
System.exit(0);
}
}
);
tfTxt.addActionListener(new TFListener());
setVisible(true);
connect();
}
public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
System.out.println("connected");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public void disConnect() {
try {
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener { @Override
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
tfContent.setText(str);
tfTxt.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch(IOException ae) {
ae.printStackTrace();
}
}
}
}
服务器端:
import java.io.*;
import java.net.*;public class ChatServer {
public static void main(String[] args) {
boolean started = false;
try {
ServerSocket ss = new ServerSocket(8888);
started = true;
while(started) {
boolean bconnected = false;
Socket s = ss.accept();
System.out.println("a client content");
bconnected = true;
DataInputStream dis = new DataInputStream(s.getInputStream());
while(bconnected) {
String str = dis.readUTF();
System.out.println(str);
}
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端:import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;public class ChatClient extends Frame {
Socket s = null;
DataOutputStream dos = null;
TextField tfTxt = new TextField();
TextArea tfContent = new TextArea(); public static void main(String[] args) {
new ChatClient().launchFrame(); }
public void launchFrame() {
setBounds(300, 300, 400, 400);
add(tfTxt, BorderLayout.SOUTH);
add(tfContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter(){ @Override
public void windowClosing(WindowEvent e) {
disConnect();
System.exit(0);
}
}
);
tfTxt.addActionListener(new TFListener());
setVisible(true);
connect();
}
public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
System.out.println("connected");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public void disConnect() {
try {
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener { @Override
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
tfContent.setText(str);
tfTxt.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch(IOException ae) {
ae.printStackTrace();
}
}
}
}
服务器端:
import java.io.*;
import java.net.*;public class ChatServer {
public static void main(String[] args) {
boolean started = false;
try {
ServerSocket ss = new ServerSocket(8888);
started = true;
while(started) {
boolean bconnected = false;
Socket s = ss.accept();
System.out.println("a client content");
bconnected = true;
DataInputStream dis = new DataInputStream(s.getInputStream());
while(bconnected) {
String str = dis.readUTF();
System.out.println(str);
}
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void connect()
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
System.out.println("connected");} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}改为
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF("Hello,kitty!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
客户端:import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;public class ChatClient extends Frame {Socket s = null;
DataOutputStream dos = null;TextField tfTxt = new TextField();
TextArea tfContent = new TextArea();public static void main(String[] args) {
new ChatClient().launchFrame();}public void launchFrame() {
setBounds(300, 300, 400, 400);
add(tfTxt, BorderLayout.SOUTH);
add(tfContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter(){@Override
public void windowClosing(WindowEvent e) {
disConnect();
System.exit(0);
}}
);
tfTxt.addActionListener(new TFListener());
setVisible(true);
connect();
}public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
System.out.println("connected");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}public void disConnect() {
try {
dos.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}} private class TFListener implements ActionListener {@Override
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
tfContent.setText(str);
tfTxt.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch(IOException ae) {
ae.printStackTrace();
在这里已经写了啊,
System.out.println("connected");