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

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

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

    Java世界

    學習筆記

    常用鏈接

    統(tǒng)計

    積分與排名

    天籟村

    新華網(wǎ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;
    ?? ?//用于航次計數(shù)
    ?? ?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();
    ?? ?//構(gòu)建服務器端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) {
    ?? ??????? ??? ? //計數(shù)多少個客戶端
    ?? ??????? ??? ? counter++;
    ?? ??????? ??? ? //設置JTextArea不能被更改
    ?? ??????? ??? ? display.setEditable(false);
    ?? ???????????? // 第二步:等待一個連接
    ?? ???????????? waitForConnection();
    ?? ???????????? // 第三步:獲取接受數(shù)據(jù)
    ?? ???????????? 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());
    ?? ? }
    ?? ??? // 獲取接受數(shù)據(jù)
    ?? ? 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("成功從服務器端接收處理后的數(shù)據(jù):"+"\n\n");
    ?? ???????????? //遍歷集合
    ?? ???????????? Iterator i = exe.list.iterator();
    ?? ??? ??? ??? ?while (i.hasNext()) {
    ?? ??? ??? ??? ??? ?//將遍歷出來的對象Object轉(zhuǎn)成Exporter類型
    ?? ??? ??? ??? ??? ?exe = (Exporter) i.next();
    ?? ??? ??? ??? ??? ?//變量來累計航行次數(shù)
    ?? ??? ??? ??? ??? ?count++;
    //?? ??? ??? ??? ??? ?在集合中放入箱型參數(shù)
    ?? ??? ??? ??? ??? ?list.add(exe.getCnttype());
    ?? ??? ??? ??? ??? ?//發(fā)送字符數(shù)據(jù)給客戶端
    ?? ??? ??? ??? ??? ?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("經(jīng)紀人:"+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 閱讀(406) 評論(0)  編輯  收藏


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


    網(wǎng)站導航:
     
    主站蜘蛛池模板: 日本道免费精品一区二区| 亚洲视频免费在线播放| 亚洲av无码精品网站| 114级毛片免费观看| 亚洲成a∨人片在无码2023| 亚洲精品麻豆av| 久久久久久精品免费看SSS | 亚洲色婷婷综合开心网| 久久中文字幕免费视频| 亚洲国产无线乱码在线观看| 国产精品亚洲mnbav网站 | 久久久受www免费人成| 亚洲综合久久久久久中文字幕| 国产片免费在线观看| 一区二区三区四区免费视频 | 亚洲xxxxxx| 国产综合亚洲专区在线| 免费看黄视频网站| 大片免费观看92在线视频线视频 | 少妇中文字幕乱码亚洲影视| 日日操夜夜操免费视频| 久9久9精品免费观看| 国产亚洲视频在线观看网址| 中文字幕亚洲色图| 国产国拍亚洲精品福利| 我要看WWW免费看插插视频| 中文无码成人免费视频在线观看| 亚洲人成色99999在线观看| 亚洲av日韩av不卡在线观看| 大胆亚洲人体视频| 国产一精品一AV一免费孕妇| 久久aⅴ免费观看| jizz免费在线观看| 国产精品无码亚洲一区二区三区| 亚洲欧洲校园自拍都市| 国产成人综合亚洲AV第一页| 日本二区免费一片黄2019| 无码人妻精品中文字幕免费东京热| 久久久受www免费人成| 视频一区二区三区免费观看| 国产亚洲sss在线播放|