初学socket(ServerThread.java)

/*
 * ServerThread.java
 *
 * Created on 13 November 2006, 16:07
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * The threads spun from the NewShop server to process client requests
 * @author dsi06dh Dave Hollowell
 * @version 0.1 23rd November 2006
 */
public class ServerThread implements Runnable
{
    /**
     * reference to the server GUI display
     */
    private ServerGui theDisplay;
    /**
     * Reference to the warehaouse instance to use
     */
    private Warehouse theWarehouse;
    /**
     * reference to the client socket
     */
    private Socket theSocket;
    /**
     * Reference to the client number
     */
    private int theClient;
    /**
     * The incoming object data stream
     */
    private ObjectInputStream in = null;
    /**
     * The outgoing object data stream
     */
    private ObjectOutputStream out = null;
    /**
     * Continue to process the requests
     */
    private boolean process = true;
    /**
     * the customer details
     */
    private Customer cust;
    /**
     * The branch details
     */
    private Branch theBranch;
           
    /**
     * End of line character
     */
    private static final String NEWLINE = System.getProperty("line.separator");
   
    /**
     * Creates a new instance of ServerThread
     * @param clientNo Numeric reference to the client
     * @param clientSocket Socket number for the client
     * @param aWarehouse Reference to the required warehouse instance
     * @param display Reference to the required server GUI display
     */
    public ServerThread(int clientNo, Socket clientSocket,
            Warehouse aWarehouse, ServerGui display)
    {
        theClient = clientNo;
        theSocket = clientSocket;
        theWarehouse = aWarehouse;
        theDisplay = display;
    }
   
    /**
     * Runnable implementation of the server thread
     */
    public void run()
    {
        theDisplay.statusMessage("Client " + theClient +
                        " Thread started" + NEWLINE);
        try
        {
            in = new ObjectInputStream((theSocket.getInputStream()));
        }
        catch (IOException e)
        {
            theDisplay.statusMessage("Client " + theClient +
                    " inbound connection failed" + NEWLINE);
            System.exit(-1);
        }
        try
        {
            out = new ObjectOutputStream((theSocket.getOutputStream()));
        }
        catch (IOException e)
        {
            theDisplay.statusMessage("Client " + theClient +
                    " outbound connection failed" + NEWLINE);
            System.exit(-1);
        }  
        while(process)
        {
            // get incoming details
            TransmissionData mess = null;
            try
            {
                mess = (TransmissionData) in.readObject();
            }
            catch (IOException e)
            {
                theDisplay.statusMessage("Client " + theClient +
                    " read failed" + NEWLINE);
                System.exit(-1);
            }
            catch (ClassNotFoundException e)
            {
                theDisplay.statusMessage(e.getMessage());
            }
            // procees the incoming message
            mess = processRequest(mess);
            if (process)
            {
                try
                {
                    out.writeObject((TransmissionData) mess);
                }
                catch (IOException e)
                {
                    theDisplay.statusMessage("Client " + theClient +
                        " send failed" + NEWLINE);
                    System.exit(-1);
                }
            }
        }
        theDisplay.statusMessage("Client " + theClient +
                        " Thread completed" + NEWLINE);
    }
   
    /**
     * Process requests from the transmission details
     * @param details Incoming transmission details
     * @return Outqoing transmission details
     */
    private TransmissionData processRequest(TransmissionData details)
    {
        TransmissionData input = details;
        TransmissionData output = null;
        // login requested
        if (input.getDataType() == TransmissionData.CLT_LOGIN)
        {
            output = processLogin(input);
        }
        // branch selected
        if (input.getDataType() == TransmissionData.CLT_BRANCH)
        {
            output = processBranch(input);
        }
        // client order completion requested
        if (input.getDataType() == TransmissionData.CLT_COMPLETE)
        {
            output = processOrder(input);
        }
        // client closedown requested
        if (input.getDataType() == TransmissionData.CLT_CLOSE)
        {
            processClose();
            output = null;
        }
        return output;
    }
   
    /**
     * Process a login request
     * @param details incoming transmission details
     * @return Outgoing transmission details
     */
    private TransmissionData processLogin(TransmissionData details)
    {
        TransmissionData input = details;
        TransmissionData output = null;
        // determine customer sent
        String custCode;
        custCode = (String) input.getData();
        theDisplay.statusMessage("Client " + theClient + " Login request "
                + custCode + NEWLINE);
        cust = theWarehouse.getCustomer(custCode);
        // customer exists?
        if (cust == null)
        {
            theDisplay.statusMessage("Client " + theClient +
                    " Customer refused" + NEWLINE);
            output = new TransmissionData(TransmissionData.SRV_ERROR,
                "Customer does not exist.");
        }
        else
        {
            String data1, data2;
            // send branch details
            String branchData = theWarehouse.getBranchCodes_Names();
            StringTokenizer st = new StringTokenizer(branchData, "*");           
            Vector<Branch> branchList = new Vector<Branch>();
            while (st.hasMoreTokens())
            {
                data1 = st.nextToken();
                if (!(data1.equals("END")))
                {
                    data2 = st.nextToken();
                    Branch tempBranch = new Branch(data1, data2);
                    branchList.addElement(tempBranch);
                }
            }
            theDisplay.statusMessage("Client " + theClient +
                    " Customer accepted" + NEWLINE);
            output = new TransmissionData(TransmissionData.SRV_BRANCHLIST,
                    branchList);
        }
        return output;
    }
   
    /**
     * Process a branch request
     * @param details incoming transmission details
     * @return Outgoing transmission details
     */
    private TransmissionData processBranch(TransmissionData details)
    {
        TransmissionData input = details;
        TransmissionData output = null;
        String branchRequest = (String) input.getData();
        theDisplay.statusMessage("Client " + theClient +
                    " Branch request " + branchRequest + NEWLINE);
        //theBranch = theWarehouse.getBranch(branchRequest);
        String stockData = theWarehouse.getBranchStockList(branchRequest);
        String data1, data2, data3;
        int data4;
        // send branch item details
        StringTokenizer st = new StringTokenizer(stockData, "*");           
        Vector<StockItem> stockList = new Vector<StockItem>();
        while (st.hasMoreTokens())
        {
            data1 = st.nextToken();
            if (!(data1.equals("END")))
            {
                data2 = st.nextToken();
                data3 = st.nextToken();
                data4 = Integer.parseInt(st.nextToken());
                StockItem tempItem = new StockItem(data1, data2, data3,
                        data4);
                stockList.addElement(tempItem);
            }
        }
        theDisplay.statusMessage("Client " + theClient +
                    " Branch accepted" + NEWLINE);
        output = new TransmissionData(TransmissionData.SRV_STOCKLIST,
                stockList);
        return output;
    }
   
    /**
     * Process a client complete order request
     * @param details incoming transmission details
     * @return outgoing transmission details
     */
    private TransmissionData processOrder(TransmissionData details)
    {
        theDisplay.statusMessage("Client " + theClient +
                " complete Order requested" + NEWLINE);
        TransmissionData input = details;
        TransmissionData output = null;
        String basketDetails;
        basketDetails = cust.getCustomerCode() + "*";
        // warning issued in compilation due to unchecked cast from
        // input.getData() returning an object - see Generics
        Vector<BasketItem> items = (Vector<BasketItem>) input.getData();
        Iterator it = items.iterator();
        while (it.hasNext())
        {
            BasketItem line = (BasketItem) it.next();
            basketDetails = basketDetails + line.getItemCode() + "*";
            basketDetails = basketDetails + line.getDescription() + "*";
            basketDetails = basketDetails + line.getPrice() + "*";
            basketDetails = basketDetails + line.getNumberOrdered() + "*";
        }
        basketDetails = basketDetails + "END*";
        String orderNo = theWarehouse.processBasket(basketDetails);
        output = new TransmissionData(TransmissionData.SRV_ORDER, orderNo);
        theDisplay.statusMessage("Client " + theClient +
                " Order " + orderNo + " completed" + NEWLINE);
        return output;
    }
    /**
     * Process a client closedown request
     */
    private void processClose()
    {
        theDisplay.statusMessage("Client " + theClient +
                " shutdown requested." + NEWLINE);
        try
        {
            theSocket.close();
        }
        catch (IOException e)
        {
            theDisplay.statusMessage("Client " + theClient +
                " shutdown exception " +e.getMessage() + NEWLINE);
        }
        try
        {
            in.close();
        }
        catch (IOException e)
        {
            theDisplay.statusMessage("Client " + theClient +
                " read close exception " +e.getMessage() + NEWLINE);
        }
        try
        {
            out.close();
        }
        catch (IOException e)
        {
            theDisplay.statusMessage("Client " + theClient +
                " write close exception " +e.getMessage() + NEWLINE);
        }
        process = false;
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
// 文件名:moreServer.java import java.io.*; import java.net.*; import java.util.*; /** * <p>Title: 多线程服务器</p> * <p>Description: 本实例使用多线程实现多服务功能。</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Filename: </p> * @author 杜江 * @version 1.0 */ class moreServer { public static void main (String [] args) throws IOException { System.out.println ("Server starting...\n"); //使用8000端口提供服务 ServerSocket server = new ServerSocket (8000); while (true) { //阻塞,直到有客户连接 Socket sk = server.accept (); System.out.println ("Accepting Connection...\n"); //启动服务线程 new ServerThread (sk).start (); } } } //使用线程,为多个客户端服务 class ServerThread extends Thread { private Socket sk; ServerThread (Socket sk) { this.sk = sk; } //线程运行实体 public void run () { BufferedReader in = null; PrintWriter out = null; try{ InputStreamReader isr; isr = new InputStreamReader (sk.getInputStream ()); in = new BufferedReader (isr); out = new PrintWriter ( new BufferedWriter( new OutputStreamWriter( sk.getOutputStream ())), true); while(true){ //接收来自客户端的请求,根据不同的命令返回不同的信息。 String cmd = in.readLine (); System.out.println(cmd); if (cmd == null) break; cmd = cmd.toUpperCase (); if (cmd.startsWith ("BYE")){ out.println ("BYE"); break; }else{ out.println ("你好,我是服务器!"); } } }catch (IOException e) { System.out.println (e.toString ()); } finally { System.out.println ("Closing Connection...\n"); //最后释放资源 try{ if (in != null) in.close (); if (out != null) out.close (); if (sk != null) sk.close (); } catch (IOException e) { System.out.println("close err"+e); } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值