?? 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 + -