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

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

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

    Java世界

    學習筆記

    常用鏈接

    統(tǒng)計

    積分與排名

    天籟村

    新華網

    雅虎

    最新評論

    海運項目: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();
    ?? ?//創(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();
    ?? ? }

    }

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


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


    網站導航:
     
    主站蜘蛛池模板: 久久久青草青青国产亚洲免观| 亚洲精品字幕在线观看| 一区二区三区在线免费| 久久综合图区亚洲综合图区| 在线视频免费观看高清| 美女被爆羞羞网站免费| 精品日韩亚洲AV无码| 国产成人精品免费视频大全五级| 手机看片国产免费永久| 激情内射亚洲一区二区三区爱妻| 亚洲av高清在线观看一区二区 | 亚洲人成免费电影| 久久亚洲国产成人影院网站| 巨波霸乳在线永久免费视频| 免费国产高清毛不卡片基地 | 国产乱子伦片免费观看中字| 久久综合给合久久国产免费| 美女免费视频一区二区| 亚洲国产精品日韩在线| 亚洲Av无码乱码在线播放| 香港经典a毛片免费观看看| 亚洲精品视频在线免费| 永久免费视频网站在线观看| 亚洲综合色区中文字幕| 日韩高清免费观看| 国产午夜免费高清久久影院| 伊人久久亚洲综合影院首页| 亚洲人成影院在线无码按摩店| 波多野结衣中文字幕免费视频| 暖暖免费中文在线日本| 亚洲精品视频在线免费| 亚洲第一成人影院| 亚洲国产精品免费在线观看| 视频一区在线免费观看| 亚洲精品国产成人中文| 成人免费福利电影| 免费看搞黄视频网站| 亚洲最大无码中文字幕| 亚洲午夜福利精品久久| 久久久久久国产精品免费免费| 国产午夜不卡AV免费|