實作的方式是Client0、Client1、Client2發送訊息Server把訊息轉發給其他人
Server:
package JChat;
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.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Server {
public static void main(String[] args) throws Exception {
JFrame f = new JFrame();
f.setTitle("Server");
f.setSize(400, 400);
f.setLocation(100, 200);
f.setLayout(null);
f.setResizable(false);
JButton b = new JButton("發送");
b.setBounds(300, 310, 80, 30);
f.add(b);
final JTextField tf = new JTextField();
tf.setBounds(10, 310, 280, 30);
f.add(tf);
final JTextArea ta = new JTextArea();
ta.setBounds(10,10, 300, 280);
f.add(ta);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
ServerSocket ss2 = new ServerSocket(8882);
System.out.println("listenning on port:8882");
final Socket s2 = ss2.accept();
ServerSocket ss1 = new ServerSocket(8881);
System.out.println("listenning on port:8881");
final Socket s1 = ss1.accept();
ServerSocket ss = new ServerSocket(8880);
System.out.println("listenning on port:8880");
final Socket s = ss.accept();
//接收c1
new Thread() {
public void run() {
while (true) {
try {
DataInputStream dis = new DataInputStream(
s1.getInputStream());
String text = dis.readUTF();
ta.append(text+"\r\n");
int l=text.length();
if(l<2) {
DataOutputStream dos = new DataOutputStream(
s.getOutputStream());
dos.writeUTF(text);
DataOutputStream dos2 = new DataOutputStream(
s2.getOutputStream());
dos2.writeUTF(text);
}
else {
if(text.charAt(l-2)=='@'&&text.charAt(l-1)=='0') {
DataOutputStream dos = new DataOutputStream(
s.getOutputStream());
dos.writeUTF(text);
}
else if(text.charAt(l-2)=='@'&&text.charAt(l-1)=='2') {
DataOutputStream dos2 = new DataOutputStream(
s2.getOutputStream());
dos2.writeUTF(text);
}
else {
DataOutputStream dos = new DataOutputStream(
s.getOutputStream());
dos.writeUTF(text);
DataOutputStream dos2 = new DataOutputStream(
s2.getOutputStream());
dos2.writeUTF(text);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
};
}
}
Client2(Client1和Client0類似的):
package JChat;
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.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Client2 {
public static void main(String[] args) throws Exception {
JFrame f = new JFrame();
f.setTitle("Client2");
f.setSize(400, 450);
f.setLocation(600, 200);
f.setLayout(null);
f.setResizable(false);
JButton b = new JButton("發送");
b.setBounds(300, 310, 80, 30);
f.add(b);
JButton c = new JButton("@client0");
c.setBounds(10, 350, 150, 30);
f.add(c);
JButton c1 = new JButton("@client1");
c1.setBounds(200, 350, 150, 30);
f.add(c1);
final JTextField tf = new JTextField();
tf.setBounds(10, 310, 280, 30);
f.add(tf);
final JTextArea ta = new JTextArea();
ta.setBounds(10,10, 300, 280);
f.add(ta);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
final Socket s2 = new Socket("127.0.0.3", 8882);
new Thread() {
public void run() {
while (true) {
try {
DataInputStream dis = new DataInputStream(
s2.getInputStream());
String text = dis.readUTF();
ta.append(text+"\r\n");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
ta.append(text+"\r\n");
try {
DataOutputStream dos = new DataOutputStream(
s2.getOutputStream());
dos.writeUTF(text);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
c1.addActionListener(new ActionListener() {
//@c1
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
text = text +"@1";
tf.setText(text);
}
});
c.addActionListener(new ActionListener() {
//@c0
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
text = text +"@0";
tf.setText(text);
}
});
}
}
uj5u.com熱心網友回復:
現在問題好像是服務端收不到客戶端訊息,不知道問題出在哪兒uj5u.com熱心網友回復:
客戶端地址錯誤uj5u.com熱心網友回復:
Client2類的52行 把ip地址寫死了uj5u.com熱心網友回復:
final Socket s2 = new Socket("127.0.0.3", 8882); 不同的電腦ip都不一樣 看看你收不到訊息的電腦ip多少改下轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/52198.html
標籤:Eclipse
