部分代码:
Server:
package Serverdefault; public class FileServerMain{ public static void main(String[] args){ ServerOn so=new ServerOn(9527); Thread t=new Thread(so); t.start(); } }
package Serverdefault; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Vector; import javax.swing.JOptionPane; public class ServerOn implements Runnable{ private ServerSocket serverSocket; private Vector<ClientList> clientList; private HashMap clientMap; public ServerOn(int Port){ clientList=new Vector<ClientList>(); clientMap=new HashMap(); try { serverSocket = new ServerSocket(Port); } catch (IOException e) { // TODO Auto-generated catch block Log("At ServerOn.java ServerOn();"+"IOException error;"+e); e.printStackTrace(); } Log("At ServerOn.java ServerOn();"+"启动服务成功;"); } //记录日志函数 位置 bin/ServerLog.txt protected void Log(String content){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); BufferedOutputStream bufferedOutputStream = null; File file=new File("bin/ServerLog.txt"); String Log="\r\n"+(df.format(new Date())).toString()+" ----> "; Log+=content+"\r\n"; try { bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file,true)); bufferedOutputStream.write(Log.getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch(IOException e ){ e.printStackTrace(); }finally{ try { bufferedOutputStream.close(); } catch (IOException e) { JOptionPane.showMessageDialog(null,e,"错误信息",JOptionPane.ERROR_MESSAGE); } } } public void run() { // TODO Auto-generated method stub XMLFileListener xfl=new XMLFileListener(clientList,"FileSend",1000); xfl.Listen(); while(true){ try { Socket c=serverSocket.accept(); //map没用到,Connection CSocket = new Connection(c,clientList,clientMap); Thread t=new Thread(CSocket); t.start(); } catch (IOException e) { // TODO Auto-generated catch block Log("At ServerOn.java run();"+"IOException error;"+e); e.printStackTrace(); } } } public static void main(String[] str){ ServerOn so=new ServerOn(9527); Thread t=new Thread(so); t.start(); } }
完整代码去我的资源页
原文链接:https://www.f2er.com/xml/299666.html