package com.sinojava.haiyun;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
//服務器端類
public class ServerSort {
?? ?private Frame frame;
?? ?private TextArea display;
?? ?//用于航次計數
?? ?private int count = 0;
?? ?private ObjectOutputStream output;
?? ?private ObjectInputStream input;
?? ?private ServerSocket ser;
?? ?private Socket soc;
?? ?//用于客戶端技術
?? ?private int counter = 0;
?? ?ArrayList list = new ArrayList();
?? ?//創(chuàng)建Exporter對象exe
?? ?Exporter exe = new Exporter();
?? ?//構建服務器端GUI
?? ?public ServerSort() {
?? ??? ?frame = new Frame("服務器端處理");
?? ??? ?display = new TextArea();
?? ??? ?frame.add(display);
?? ??? ?frame.setSize(500,500);
?? ??? ?frame.setLocation(200, 200);
?? ??? ?frame.setVisible(true);
?? ??? ?//退出程序
?? ??? ?frame.addWindowListener(new WindowAdapter() {
?? ??? ??? ?public void windowClosing(WindowEvent e) {
?? ??? ??? ??? ?System.exit(0);
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?//運行服務器,連接客戶端
?? ?public void runServer() {?? ?
?? ?????? try {
?? ????????? // 第一步:創(chuàng)建一個ServerSocket
?? ????????? ser = new ServerSocket(5000);
?? ????????? while(true) {
?? ??????? ??? ? //計數多少個客戶端
?? ??????? ??? ? counter++;
?? ??????? ??? ? //設置JTextArea不能被更改
?? ??????? ??? ? display.setEditable(false);
?? ???????????? // 第二步:等待一個連接
?? ???????????? waitForConnection();
?? ???????????? // 第三步:獲取接受數據
?? ???????????? getStreams();
?? ???????????? // 第四步:連接處理
?? ???????????? processConnection();
?? ???????????? // 第五步:關閉連接
?? ???????????? closeConnection();
?? ????????? }
?? ?????? }catch (IOException e) {
?? ????????? e.printStackTrace();
?? ?????? }
?? ??? }
?? ?
?? ?//向客戶端發(fā)送信息
?? ?private void sendData(String message) {
?? ?????? try {
?? ????????? output.writeObject(message);
?? ????????? output.flush();
?? ?????? }
?? ?????? catch (IOException e) {
?? ????????? display.append("\n錯誤寫入對象");
?? ?????? }
?? ??? }
?? ?//第二步:等待一個連接
?? ?private void waitForConnection() throws IOException {
?? ??? ? display.setText("等待連接\n");
?? ????? soc = ser.accept();?? ?
?? ????? //InetAddress類采用工廠設計模式,有三個靜態(tài)工廠方法,如getHostName or getLocalHost。
?? ????? display.append("Socket "+counter+" 接收來至:"+soc.getInetAddress().getHostName());
?? ? }
?? ??? // 獲取接受數據
?? ? private void getStreams() throws IOException {
?? ????? output = new ObjectOutputStream(soc.getOutputStream());
?? ????? output.flush();
?? ????? input = new ObjectInputStream(soc.getInputStream());
?? ????? display.append("\n獲得I/O流\n\n\n");
?? ? }
?? ??? // 連接處理
?? ? private void processConnection() throws IOException {
?? ????? String message ="服務器: 連接成功\n\n\n";
?? ????? output.writeObject(message);
?? ????? output.flush();
?? ????? do {
?? ????????? try {
?? ???????????? message = (String)input.readObject();
?? ???????????? //解析字符串
?? ???????????? exe.splitText(message);
?? ???????????? sendData("成功從服務器端接收處理后的數據:"+"\n\n");
?? ???????????? //遍歷集合
?? ???????????? Iterator i = exe.list.iterator();
?? ??? ??? ??? ?while (i.hasNext()) {
?? ??? ??? ??? ??? ?//將遍歷出來的對象Object轉成Exporter類型
?? ??? ??? ??? ??? ?exe = (Exporter) i.next();
?? ??? ??? ??? ??? ?//變量來累計航行次數
?? ??? ??? ??? ??? ?count++;
//?? ??? ??? ??? ??? ?在集合中放入箱型參數
?? ??? ??? ??? ??? ?list.add(exe.getCnttype());
?? ??? ??? ??? ??? ?//發(fā)送字符數據給客戶端
?? ??? ??? ??? ??? ?sendData("船名:"+exe.getShipname()+"\n");
?? ??? ??? ??? ??? ?sendData("航次:"+exe.getVoyage()+"\n");
?? ??? ??? ??? ??? ?sendData("提單號:"+exe.getBlno()+"\n");
?? ??? ??? ??? ??? ?sendData("目的港:"+exe.getDestination()+"\n");
?? ??? ??? ??? ??? ?sendData("集裝箱尺寸:"+exe.getCntsize()+"\n");
?? ??? ??? ??? ??? ?sendData("箱型:"+exe.getCnttype()+"\n");
?? ??? ??? ??? ??? ?sendData("箱量:"+exe.getCntqnt()+"\n");
?? ??? ??? ??? ??? ?sendData("經紀人:"+exe.getCntoperator()+"\n");
?? ??? ??? ??? ??? ?sendData("備注:"+exe.getRemark()+"\n");
?? ??? ??? ??? ??? ?sendData("本航次的船名: "+exe.getShipname()+"; 航次: "+exe.getVoyage()+"; 業(yè)務量: "+exe.getCntqnt()+"; 箱型: "+exe.getCnttype()+"\n");
?? ??? ??? ??? ??? ?sendData("--------------------------------------------------\n");
?? ??? ??? ??? ?}
?? ??? ??? ??? ?sendData("--------------------------------------------------\n");
?? ??? ??? ??? ?sendData("一共有:" + count + "航次"+"\n");
?? ??? ??? ??? ?sendData(list.size()+" 個箱型 :"+list+"\n");
?? ??? ??? ??? ?display.append("文件處理完成,已發(fā)送至客戶端!"+"\n");
?? ???????????? display.setCaretPosition(display.getText().length());
?? ??????????? ?
?? ????????? }
?? ????????? catch(ClassNotFoundException e) {
?? ???????????? display.append("\n未知對象類型接收");
?? ????????? }
?? ?????? } while(true);
?? ??? }
?? ? //關閉連接
?? ? private void closeConnection() throws IOException {
?? ????? display.append("\n用戶終端連接");
?? ????? output.close();
?? ????? input.close();
?? ????? soc.close();
?? ? }
?? ? //main方法
?? ? public static void main(String args[]) {
?? ??? ? ServerSort ss = new ServerSort();
?? ??? ? //運行服務器
?? ??? ? ss.runServer();
?? ? }
}