package demo;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Server extends JFrame implements ActionListener,Runnable{
JButton jb_create;
JButton jb_add;
JTextArea area;
JTextField jt_text;
JButton jb_send;
ServerSocket serverSocket;
Socket socket;
String ip;
String port;
String name;
String friendName;
InputStream input;
OutputStream output;
public Server(){
this.setLayout(new BorderLayout());
JPanel jp_button=new JPanel();
JPanel jp_send=new JPanel();
jp_button.setLayout(new GridLayout(2,1));
jb_create=new JButton("创建房间");
jb_add=new JButton("加入房间");
jb_send=new JButton("发送");
jt_text=new JTextField(25);
jb_create.addActionListener(this);
jb_add.addActionListener(this);
jb_send.addActionListener(this);
area=new JTextArea();
JScrollPane js_area=new JScrollPane(area);
js_area.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
area.setLineWrap(true);
area.setWrapStyleWord(true);
jp_button.add(jb_create);
jp_button.add(jb_add);
jp_send.add(jt_text);
jp_send.add(jb_send);
this.add(jp_button,BorderLayout.EAST);
this.add(js_area,BorderLayout.CENTER);
this.add(jp_send,BorderLayout.SOUTH);
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(jb_create)){
try {
ip=InetAddress.getLocalHost().getHostAddress();
port=JOptionPane.showInputDialog("输入要创建房间的端口号: ");
JOptionPane.showMessageDialog(this,"你要创建的房间ip是: "+ip+" 端口号是: "+port);
name=JOptionPane.showInputDialog("请输入你的名字");
serverSocket=new ServerSocket(Integer.parseInt(port));
socket=serverSocket.accept();
this.setTitle(name);
input=socket.getInputStream();
output=socket.getOutputStream();
//write=new BufferedWriter(new OutputStreamWriter(output));
//read=new BufferedReader(new InputStreamReader(input));
} catch (Exception e1) {
e1.printStackTrace();
}
}else if(e.getSource().equals(jb_add)){
try{
ip=JOptionPane.showInputDialog("输入要加入房间的ip");
port=JOptionPane.showInputDialog("输入创建房间的端口号");
name=JOptionPane.showInputDialog("输入你的名字");
socket=new Socket(ip,Integer.parseInt(port));
this.setTitle(name);
input=socket.getInputStream();
output=socket.getOutputStream();
//write=new BufferedWriter(new OutputStreamWriter(output));
//read=new BufferedReader(new InputStreamReader(input));
}catch(Exception e1){
System.out.println(e1.getMessage());
}
}else if(e.getSource().equals(jb_send)){
String content=jt_text.getText();
try {
content=name+" "+new Date()+"\n"+content+"\n";
if(output!=null){
output.write(content.getBytes());
}
System.out.println("content: "+content);
area.append(content);
jt_text.setText("");
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args){
Server server=new Server();
Thread t=new Thread(server);
t.start();
}
public void run() {
System.out.println("run");
String content=new String();
byte[] data=new byte[1024];
while(true){
try {
if(input!=null){
int i=input.read(data);
content=new String(data,0,i);
area.append(content);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
最近下载更多
fuyouou LV5
2023年6月29日
vip666 LV1
2023年3月20日
wwt-13 LV2
2022年6月6日
tomtom113 LV2
2022年5月12日
2310573421 LV7
2022年4月23日
666777111 LV2
2021年12月30日
yangchengshuai LV16
2021年11月22日
Elon_Edge LV1
2020年12月31日
zxp888 LV4
2020年12月18日
liuxie LV12
2020年12月13日
最近浏览更多
13133117021 LV5
2024年12月24日
Reach_hehe LV1
2024年12月6日
liamin LV2
2024年6月24日
周鸣郝 LV2
2024年5月26日
微信网友_7004855557083136 LV1
2024年5月22日
krispeng LV15
2024年4月16日
taoshen95 LV16
2024年1月19日
zeng1206 LV7
2023年12月21日
微信网友_6762641760833536
2023年12月4日
暂无贡献等级
yuchunxing LV1
2023年9月2日

