Java世界

          學習筆記

          常用鏈接

          統計

          積分與排名

          天籟村

          新華網

          雅虎

          最新評論

          海運項目:ServerSort類

          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();
          ?? ?//創建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 {
          ?? ????????? // 第一步:創建一個ServerSocket
          ?? ????????? ser = new ServerSocket(5000);
          ?? ????????? while(true) {
          ?? ??????? ??? ? //計數多少個客戶端
          ?? ??????? ??? ? counter++;
          ?? ??????? ??? ? //設置JTextArea不能被更改
          ?? ??????? ??? ? display.setEditable(false);
          ?? ???????????? // 第二步:等待一個連接
          ?? ???????????? waitForConnection();
          ?? ???????????? // 第三步:獲取接受數據
          ?? ???????????? getStreams();
          ?? ???????????? // 第四步:連接處理
          ?? ???????????? processConnection();
          ?? ???????????? // 第五步:關閉連接
          ?? ???????????? closeConnection();
          ?? ????????? }
          ?? ?????? }catch (IOException e) {
          ?? ????????? e.printStackTrace();
          ?? ?????? }
          ?? ??? }
          ?? ?
          ?? ?//向客戶端發送信息
          ?? ?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類采用工廠設計模式,有三個靜態工廠方法,如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());
          ?? ??? ??? ??? ??? ?//發送字符數據給客戶端
          ?? ??? ??? ??? ??? ?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()+"; 業務量: "+exe.getCntqnt()+"; 箱型: "+exe.getCnttype()+"\n");
          ?? ??? ??? ??? ??? ?sendData("--------------------------------------------------\n");
          ?? ??? ??? ??? ?}
          ?? ??? ??? ??? ?sendData("--------------------------------------------------\n");
          ?? ??? ??? ??? ?sendData("一共有:" + count + "航次"+"\n");
          ?? ??? ??? ??? ?sendData(list.size()+" 個箱型 :"+list+"\n");
          ?? ??? ??? ??? ?display.append("文件處理完成,已發送至客戶端!"+"\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();
          ?? ? }

          }

          posted on 2007-11-16 14:04 Rabbit 閱讀(404) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 西乡县| 老河口市| 随州市| 富川| 伽师县| 泾阳县| 五峰| 镇康县| 承德县| 乃东县| 永清县| 泊头市| 怀安县| 迁西县| 张北县| 沙雅县| 班玛县| 秦安县| 金川县| 德兴市| 达拉特旗| 长治县| 若羌县| 渑池县| 博客| 黄石市| 邮箱| 繁昌县| 长葛市| 乡城县| 黎城县| 安丘市| 古蔺县| 芒康县| 灵石县| 晋城| 天气| 呼图壁县| 报价| 弥渡县| 岚皋县|