这是我过去写过的简单聊天程序,基本实现功能。先运行Server1作为启动服务器,再运行Client1,在文本框里输入内容后按回车即可把消息传送到对方。你自己修改一下Client1的IP地址,也就把我写的localhost改成你运行Server1程序的机子的IP。
Server1.java:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server1 extends JFrame{
private JTextField enter;
private JTextArea display;
// PrintStream output;
// DataInputStream input;
BufferedReader input;
PrintWriter output;
public Server1(){
super("Server");
Container c=getContentPane();
enter=new JTextField();
enter.setEnabled(false);
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sendData(enter.getText());
}
});
c.add(enter,BorderLayout.NORTH);
display=new JTextArea();
c.add(new JScrollPane(display),BorderLayout.CENTER);
setSize(300,150);
setVisible(true);
}
private void sendData(String s){
try{
output.println("Server:"+s);
output.flush();
enter.setText("");
}catch(Exception e){
display.append("\nError writing object");
}
}
public void connect(){
ServerSocket server;
Socket socket;
int counter=1;
try{
server=new ServerSocket(8000,10);
while(true){
display.setText("等待客户端的请求\n");
socket=server.accept();
display.append("连接"+counter+"来自:"+socket.getInetAddress().getHostName());
// output=new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
output=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
input=new BufferedReader(new InputStreamReader(socket.getInputStream()));
output.flush();
// input=new DataInputStream(new BufferedInputStream(socket.getInputStream()));
String message="Server Connection successful!";
output.println(message);
output.flush();
enter.setEnabled(true);
do{
try{
message=(String)input.readLine();
display.append("\n"+message);
} catch(IOException e){
display.append("Data Error");
}
} while(!message.equals("Client:end"));
display.append("\n关门连接");
enter.setEnabled(false);
output.close();
input.close();
socket.close();
++counter;
}
}catch(EOFException eof){
System.out.println("Client terminated connection");
} catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args){
Server1 app=new Server1();
app.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
app.connect();
}
}
//-------------------
Client1.java:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client1 extends JFrame{
private JTextField enter;
private JTextArea display;
// PrintStream output;
// DataInputStream input;
BufferedReader input;
PrintWriter output;
String message="";
public Client1(){
super("Client1");
Container c=getContentPane();
enter=new JTextField();
enter.setEnabled(false);
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sendData(enter.getText());
}
});
c.add(enter,BorderLayout.NORTH);
display=new JTextArea();
c.add(new JScrollPane(display),BorderLayout.CENTER);
setSize(300,150);
setVisible(true);
}
private void connect(){
Socket socket;
try{
display.setText("准备连接...\n");
socket=new Socket("localhost",8000);
display.append("连接到:"+socket.getInetAddress().getHostName());
display.append("\n主机IP为:"+socket.getInetAddress().toString());
output=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
input=new BufferedReader(new InputStreamReader(socket.getInputStream()));
// output=new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
output.flush();
// input=new DataInputStream(new BufferedInputStream(socket.getInputStream()));
enter.setEnabled(true);
do{
try{
message=(String)input.readLine();
display.append("\n"+message);
} catch(IOException e){
display.append("\n无法获取信息");
}
} while(!message.equals("Server:end"));
display.append("\n关门连接");
enter.setEnabled(false);
output.close();
input.close();
socket.close();
}
catch(EOFException eof){
System.out.println("服务器连接中断");
} catch(IOException e){
e.printStackTrace();
}
}
private void sendData(String s){
try{
message=s;
output.println("Client:"+s);
output.flush();
enter.setText("");
} catch(Exception e){
display.append("\n数据传输错误!");
}
}
public static void main(String[] args){
Client1 app=new Client1();
app.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
app.connect();
}
}