📄 server_10.26.txt
字号:
//////////////////////////////////////////////////
//Server.java
//071026 11:40 hejia
//////////////////////////////////////////////////
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.swing.Timer;
public class Server /*++Timer++*/implements ActionListener
{
public static void main(String args[])
{
ServerSocket server=null;
Server_thread thread;
Socket you=null;
int clientNumber=0;
Timer time;
Server_thread t[];
//String temp1=null;
//StringBuffer Content= new StringBuffer("");
FileWindows win=new FileWindows();
//File Store _ADD
while(true)
{
try{
server=new ServerSocket(4331);
}
catch(IOException e1)
{
System.out.println("正在监听"); //ServerSocket对象不能重复创建
}
try{
System.out.println(" 等待客户呼叫");
you=server.accept(); //程序将在此等候客户端的连接
clientNumber++; //记录客户数目
System.out.println("客户的地址:"+you.getInetAddress());
}
catch (IOException e)
{
System.out.println("正在等待客户");
}
if(you!=null)
{
/*Old_Editon*/
// new Server_thread(you,clientNumber/*,temp1*/).start();
//为每个客户启动一个专门的线程
/*New_Editon*/
Server_thread t[clientNumber]= new Server_thread(you,clientNumber/*,temp1*/);
Server_thread t[clientNumber].start();
}
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==time)
{
// Server_thread t[clientNumber].send();
//每隔一定时间,向所有客户端发送更新的数据
//相应的在Server_thread中应添加 public void send();
}
}
}
class Server_thread extends Thread
{
Socket socket;
int clientNumber;
DataOutputStream out=null;
DataInputStream in=null;
static String temp=null;
static StringBuffer Content= new StringBuffer("");
Server_thread(Socket t,int clientNum/*,String temp1*/)
{
socket=t;
this.clientNumber=clientNum;
//temp=temp1;
try {
out=new DataOutputStream(socket.getOutputStream());
in=new DataInputStream(socket.getInputStream());
}
catch (IOException e)
{}
}
public /*static*/ synchronized void save(String s)
{
// int num1=this.clientNumber;
// String num=" "+(String)num1;
Content.append(s/*+num*/+"\n");
temp=Content.toString();
//保存聊天记录于user.dat
try{
File f=new File("user.dat");
FileWriter out1=new FileWriter(f);
char a[]=temp.toCharArray();
out1.write(a,0,a.length);
out1.close();
}
catch(IOException e)
{ System.out.println("File read Error");
}
}
public /*static*/ String read()
{
return temp;
}
public void run()
{
String s=null;
while(true)
{
try{
s=in.readUTF();//堵塞状态,除非读取到信息
//Content.append(s+"\n");
save(s);
out.writeUTF(read());
/* quesion=false;
StringTokenizer fenxi=new StringTokenizer(s," ,");
while(fenxi.hasMoreTokens())
{ String temp=fenxi.nextToken();
try{ a[i]=Double.valueOf(temp).doubleValue();i++;
}
catch(NumberFormatException e)
{ out.writeUTF("请输入数字字符");
quesion=true;
}
}
if(quesion==false)
{ double p=(a[0]+a[1]+a[2])/2.0;
out.writeUTF(" "+Math.sqrt(p*(p-a[0])*(p-a[1])*(p-a[2])));
}
*/
}
catch (IOException e)
{ System.out.println("客户离开");
return;
}
}
}
}
/*
public class Example10_9
{ public static void main(String args[])
{ FileWindows win=new FileWindows();
}
}
*/
class FileWindows extends Frame implements ActionListener
{
FileDialog filedialog_save,filedialog_load;//声明2个文件对话筐
MenuBar menubar;
Menu menu,menu1;
MenuItem itemOpen,itemSave,itemShow;
TextArea text;
BufferedReader in;
FileReader file_reader;
BufferedWriter out;
FileWriter tofile;
FileWindows()
{ super("服务器端控制台");
//setSize(260,270);
//setVisible(true);
menubar=new MenuBar();
menu=new Menu("文件");
menu1=new Menu("查看聊天记录");
itemShow=new MenuItem("查看");
itemOpen=new MenuItem("打开文件");
itemSave=new MenuItem("保存文件");
itemShow.addActionListener(this);
itemOpen.addActionListener(this);
itemSave.addActionListener(this);
//ADD MenuItem
menu1.add(itemShow);
menu.add(itemOpen);
menu.add(itemSave);
menubar.add(menu);
menubar.add(menu1);
setMenuBar(menubar);
setBounds(250,250,260,270);
filedialog_save=new FileDialog(this,"保存文件话框",FileDialog.SAVE);
filedialog_load=new FileDialog(this,"打开文件话框",FileDialog.LOAD);
filedialog_save.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{ filedialog_save.setVisible(false);
}
});
filedialog_load.addWindowListener(new WindowAdapter()//对话框增加适配器
{public void windowClosing(WindowEvent e)
{ filedialog_load.setVisible(false);
}
});
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{ System.exit(0);}
});
text=new TextArea(10,10);
add(text,BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int b;
char tom[]=new char[25];
String ss=null,temp="";
if(e.getSource()==itemOpen)
{
filedialog_load.setVisible(true);
text.setText(null);
String s;
if(filedialog_load.getFile()!=null)
{
try{
File file= new
File(filedialog_load.getDirectory(),filedialog_load.getFile());
file_reader=new FileReader(file);
in=new BufferedReader(file_reader);
while((s=in.readLine())!=null)
text.append(s+'\n');
in.close();
file_reader.close();
}
catch(IOException e2){}
}
}
else if(e.getSource()==itemSave)
{
filedialog_save.setVisible(true);
if(filedialog_save.getFile()!=null)
{
try {
File file=new
File(filedialog_save.getDirectory(),filedialog_save.getFile());
tofile=new FileWriter(file);
out=new BufferedWriter(tofile);
out.write(text.getText(),0,(text.getText()).length());
out.close();
tofile.close();
}
catch(IOException e2){}
}
}
else if(e.getSource()==itemShow)
{
try{
//File f=new File("Client.txt");
//FileInputStream in =new FileInputStream(f);
FileReader in = new FileReader("user.dat");
while((b=in.read(tom,0,25))!=-1)
{
ss=new String (tom,0,b);
temp+=ss;
//text.appendText(ss+"\n");
//System.out.print(s);
}
in.close();
text.setText(temp);
}
catch(IOException ee)
{ System.out.println("File read Error"+ee);
}
//text.appendText(ss+"\n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -