chatServer.java
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.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class chatServer extends JFrame {
private static JTextArea jta = new JTextArea();
private ServerSocket server;
private static List<Socket> list = new ArrayList<Socket>();
private ExecutorService exec;
public chatServer() {
setLayout(new BorderLayout());
add(new JScrollPane(jta), BorderLayout.CENTER);
setTitle("聊天室消息接收器");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try {
server = new ServerSocket(4322);
exec = Executors.newCachedThreadPool();
jta.append("服务器已经启动");
Socket client = null;
while (true) {
client = server.accept();
list.add(client);
exec.execute(new Task(client));
}
} catch (Exception e) {
}
}
static class Task implements Runnable {
private Socket client;
private DataInputStream fromclient;
private DataOutputStream toclient;
String msg;
public Task(Socket client) throws IOException {
this.client = client;
fromclient = new DataInputStream(client.getInputStream());
}
public void run() {
try {
while ((msg = fromclient.readUTF()) != null) {
msg = "[" + client.getPort() + "]说:" + msg;
jta.append(msg + '\n');
sendMessage();
}
} catch (Exception e) {
}
}
public void sendMessage() throws IOException {
for (Socket client : list) {
toclient = new DataOutputStream(client.getOutputStream());
toclient.writeUTF(msg);
}
}
}
public static void main(String[] args) {
new chatServer();
}
}
newClient.java
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.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class newClient extends JFrame {
private JPanel p = new JPanel();
private JTextField jtf1 = new JTextField();
private JTextArea jta1 = new JTextArea();
private DataOutputStream toserver;
private DataInputStream fromserver;
private JButton button = new JButton("发送");
// private Socket client =null;
public newClient() {
p.setLayout(new BorderLayout());
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.add(jtf1, BorderLayout.CENTER);
p2.add((new JLabel("NND 你一定不要忘了曾经的承诺和坚持")), BorderLayout.SOUTH);
p2.add(button, BorderLayout.EAST);
p.add(p2, BorderLayout.SOUTH);
p.add(jta1, BorderLayout.CENTER);
this.setTitle("最简单的聊天室-----antking");
this.add(p);
this.setSize(500, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
button.addActionListener(new buttonListener());
try {
Socket client = new Socket("115.159.180.185", 4322);
toserver = new DataOutputStream(client.getOutputStream());
fromserver = new DataInputStream(client.getInputStream());
while (true) {
String msg = fromserver.readUTF();
if (msg != null)
jta1.append(msg + '\n');
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private class buttonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = jtf1.getText().toString();
try {
// toserver = new DataOutputStream(client.getOutputStream());
// fromserver = new DataInputStream(client.getInputStream());
//
toserver.writeUTF(str);
toserver.flush();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
new newClient();
}
}
java客户端发送消息到服务端
最新推荐文章于 2023-09-06 19:00:29 发布