<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(406) 評論(0)  編輯  收藏


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


    網站導航:
     
    主站蜘蛛池模板: 日韩免费a级在线观看| 久草免费手机视频| 国产高清免费的视频| 亚洲AV无码一区二区三区人| 永久看日本大片免费35分钟| 亚洲第一页在线视频| 18以下岁毛片在免费播放| 77777_亚洲午夜久久多人| 最近免费中文字幕高清大全| 亚洲欧洲春色校园另类小说| 中文毛片无遮挡高潮免费| 亚洲AV无码专区在线亚| 免费无遮挡无码视频网站| 国产成人亚洲精品蜜芽影院| 亚洲狠狠爱综合影院婷婷| 中文字幕免费在线播放| 亚洲天天做日日做天天看| 国产麻豆免费观看91| 亚洲另类无码专区首页| 亚洲精品尤物yw在线影院| 男人天堂免费视频| 亚洲精品国产情侣av在线| 久久久久久国产精品免费免费| 鲁死你资源站亚洲av| 亚洲午夜精品久久久久久浪潮| 国产麻豆一精品一AV一免费| 亚洲国产综合精品| 免费国产成人高清在线观看麻豆| 久久国产精品免费一区| 亚洲成年人电影网站| 国产成人高清精品免费软件 | 亚洲综合激情另类专区| 永久免费A∨片在线观看| 精品久久久久久亚洲精品| 国产乱子伦片免费观看中字| 91亚洲一区二区在线观看不卡| 久久综合AV免费观看| 中文字幕免费在线看电影大全| 亚洲午夜精品一区二区公牛电影院| 国产免费牲交视频| 99在线观看视频免费|