62,628
社区成员
发帖
与我相关
我的任务
分享
/***********************************************************/
package cn.kylin;
public class ProgramEntry {
public static void main(String[] args) {
MainFrame main = new MainFrame();
// LoginGui login = new LoginGui();
}
}
/***********************************************************/
package cn.kylin;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MainFrame extends JFrame implements ActionListener {
JTextArea area;
JTextField text;
JTextField with;
JButton button;
JPanel panel;
Socket socket = null;
DataInputStream in = null;
DataOutputStream out = null;
public MainFrame() {
setVisible(true);
setTitle("Kylin");
setBounds(50, 50, 400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
}
private void init() {
area = new JTextArea(6,20);
text = new JTextField(10);
with = new JTextField(10);
button = new JButton("发送");
panel = new JPanel();
panel.setLayout(new GridLayout(10, 1));
area.setEditable(false);
area.setForeground(Color.red);
area.setFont(new Font("华文行楷", Font.BOLD, 15));
add(new JScrollPane(area), BorderLayout.CENTER);
add(text, BorderLayout.NORTH);
add(panel, BorderLayout.EAST);
validate();
try {
socket = new Socket("192.168.1.106", 2010);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
while(true) {
try {
String str = in.readUTF();
if(str.contains(",")) {
initPanel(str);
} else {
area.append(str + "\r\n");
}
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
private void initPanel(String str) {
panel.removeAll();
String s = str.substring(1);
String[] arr = s.split(",");
for(int i = 0; i < arr.length; i++) {
JButton btn = new JButton(arr[i]);
btn.addActionListener(this);
panel.add(btn);
}
validate();
}
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton) e.getSource();
String str = text.getText();
System.out.println(str);
try {
String ip = btn.getText();
String myIp = InetAddress.getLocalHost().getHostAddress() + ":";
out.writeUTF(ip + ":v:" + myIp + str);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
/********************************************************************//
package cn.kylin;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
public class KylinServer {
static Map<String, Socket> socketMap = new HashMap<String, Socket>();
public static void main(String[] args) {
SocketThread thead;
ServerSocket serversocket = null;
try {
serversocket = new ServerSocket(2010);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("服务器已启动");
while(true) {
try {
Socket clientSocket = serversocket.accept();
if(clientSocket != null) {
String ip = clientSocket.getInetAddress().getHostAddress();
socketMap.put(ip, clientSocket);
new SocketThread(clientSocket).start();
}
} catch (Exception e) {
System.out.println(e);
return;
}
}
}
}
class SocketThread extends Thread {
String str = "";
Socket socket;
DataInputStream in = null;
DataOutputStream out = null;
public SocketThread(Socket t) {
socket = t;
try {
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
System.out.println(socket.getInetAddress().getHostAddress() + ":连接服务器");
while(true) {
DataOutputStream dos = null;
try {
String keyStr = "";
for(String key : KylinServer.socketMap.keySet() ){
keyStr = keyStr + "," + key;
}
out.writeUTF(keyStr);//每次接收信息发socketMap信息
str = in.readUTF();
String[] strarr = str.split(":v:");
Socket s = KylinServer.socketMap.get(strarr[0]);
if(s != null) {
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(strarr[1]);
out.writeUTF(strarr[1]);
} else {
out.writeUTF("对方不存在");
}
} catch (Exception e) {
System.out.println("客户端已断开");
KylinServer.socketMap.remove(socket.getInetAddress().getHostAddress());
try {
if(dos != null){
dos.close();
}
} catch (Exception e1) {
e1.printStackTrace();
System.exit(0);
};
return;
}
}
}
}