import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestClient {
private static JPanel p=new JPanel();
private static JTextField jtf=new JTextField();
private static JTextArea jta=new JTextArea();
private static DataInputStream fromserver;
private static DataOutputStream toserver;
private static JButton jb=new JButton("发送");
public static void main(String[] args) {
JFrame f=new JFrame("客户端");
p.setLayout(new BorderLayout());
JPanel p2=new JPanel();
p2.setLayout(new BorderLayout());
p2.add(jtf,BorderLayout.CENTER);
p2.add(jb,BorderLayout.EAST);
p.add(p2,BorderLayout.SOUTH);
p.add(jta,BorderLayout.CENTER);
f.add(p);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(100, 100, 400, 300);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str=jtf.getText().toString();
try {
toserver.writeUTF(str);
toserver.flush();
} catch (IOException e1) {
} finally{
jtf.setText("");
}
}
});
Socket client;
try {
client=new Socket("localhost",1314);
fromserver=new DataInputStream(client.getInputStream());
toserver=new DataOutputStream(client.getOutputStream());
while(true){
String msg=fromserver.readUTF();
if(msg!=null){
jta.append(msg+"\n");
}
}
} catch (UnknownHostException e1) {
} catch (IOException e1) {
}
}
}
-----服务端-----
package com.xinbo;
import java.awt.BorderLayout;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TestServer {
private static JTextArea jta=new JTextArea();
private static ServerSocket server;
private static ArrayList
private static ExecutorService exec;
static class Talk implements Runnable{
private Socket client;
private DataInputStream fromclient;
private DataOutputStream toclient;
String msg;//说的信息;
public Talk(Socket client){
this.client=client;
try {
fromclient=new DataInputStream(client.getInputStream());
} catch (IOException e) {
}
}
@Override
public void run() {
try {
while((msg=fromclient.readUTF())!=null){
msg="["+client.getPort()+"]说:"+msg;
jta.append(msg+"\n");
for(Socket client:list){
try {
toclient=new DataOutputStream(client.getOutputStream());
toclient.writeUTF(msg);
} catch (IOException e) {
}
}
}
} catch (IOException e) {
}
}
}
public static void main(String[] args) {
JFrame f=new JFrame("服务端");
f.add(new JScrollPane(jta), BorderLayout.CENTER);
f.setBounds(100, 100, 400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
try {
server=new ServerSocket(1314);
exec=Executors.newCachedThreadPool();
jta.append("服务器已开启\n");
Socket client =null;
while(true){
client=server.accept();
list.add(client);
exec.execute(new Talk(client));
}
} catch (IOException e) {
}
}
}