首页>代码>30个java常用工具类分享>/[工具类] 通信服务端simpleServer.java
001package com.common.network;
002 
003import java.net.*;
004import java.io.*;
005 
006/**
007 *
008 * 功能描述:<br />
009 * socket是网络应用程序的核心,在服务器端或客户端网络应用程序中,socket皆为不可缺少的要素
010 * 在服务器常见的应用有:FTP服务器,MAIL服务器(SMTP,POP3,IMAP4协议),WEB(HTTP协议)。
011 *
012 * 建立服务器端SOCKET的应用程序步骤如下:<br />
013 *
014 * 1、建立服务器端的SOCKET,并且以此侦听客户端的连接请求 <br />
015 *
016 * 2、当服务器端侦测到来自客户端的连接请求时则接收此请求并建立客户端的SOCKET,该SOCEKT将作为
017 * 客户端连接及后续处理发送及接收数据的依据,至此则完成服务器端与客户端的SOCKET通信链接
018 *
019 * 3、处理来自客户端的信息,一般称为请求,可视为客户端的指令需求。例如HTTP通信协议的URL请求,<br />
020 * 或FTP通信协议的FTP命令(如GET,PUT)等;<br />
021 *
022 * 4、根据客户端传来的请求服务器端需经过程序逻辑处理之后,发送回相对应的招待结果或错误信息至
023 * 客户端如HTTP服务器须发送回HTML网页内容,而FTP服务器则发送回FTP指令的结果 <br />
024 *
025 * 5、当程序完成数据或命令的处理之后,便关闭SOCKET通信链接
026 *
027 * @author Administrator
028 * @Date Jul 19, 2008
029 * @Time 9:45:54 AM
030 * @version 1.0
031 */
032public class simpleServer {
033 
034    private static ServerSocket serverSocket;
035 
036    public static void main(String[] args) throws Exception {
037        int port;
038 
039        if (args.length == 0) {
040            System.out.println("Usage:java simpleServer [port]");
041            System.exit(1);
042        }
043        port = Integer.parseInt(args[0]);
044        startServer(port);
045    }
046 
047    /**
048     * 功能描述:启动服务器
049     *
050     * @param port
051     *            服务器端口
052     * @throws Exception
053     */
054    public static void startServer(int port) throws Exception {
055        try {
056            serverSocket = new ServerSocket(port);
057            Thread thread = new Thread(new ListenClient(serverSocket));
058            thread.start();
059        } catch (IOException ex) {
060            ex.printStackTrace();
061        }
062    }
063}
064 
065/**
066 *
067 * 功能描述:监听程序
068 *
069 * @author FangHewei
070 * @Date Jul 19, 2008
071 * @Time 9:57:53 AM
072 * @version 1.0
073 *
074 */
075class ListenClient implements Runnable {
076    private ServerSocket serverSocket;
077    private Socket clientSocket;
078 
079    DataInputStream in;
080    DataOutputStream out;
081 
082    public ListenClient(ServerSocket serverSocket) throws Exception {
083        this.serverSocket = serverSocket;
084    }
085 
086    @SuppressWarnings("static-access")
087    public void run() {
088        try {
089            while (true) {
090                clientSocket = serverSocket.accept();
091                System.out.println("Connection from "
092                        + clientSocket.getInetAddress().getHostAddress());
093                in = new DataInputStream(clientSocket.getInputStream());
094                out = new DataOutputStream(clientSocket.getOutputStream());
095 
096                String lineSep = System.getProperty("line.separator");// 行分隔符,即回车换行
097                @SuppressWarnings("unused")
098                InetAddress addr = serverSocket.getInetAddress().getLocalHost();
099 
100                String outData = "Server Information: " + lineSep
101                        + "  Local Host: "
102                        + serverSocket.getInetAddress().getLocalHost()
103                        + lineSep + " port  :" + serverSocket.getLocalPort();
104                byte[] outByte = outData.getBytes();
105                out.write(outByte, 0, outByte.length);
106            }
107        } catch (Exception ex) {
108            ex.printStackTrace();
109        }
110    }
111};
最近下载更多
ljt289917726  LV3 2022年9月5日
zhy1989wz  LV7 2022年3月11日
wxhky159159  LV1 2022年1月4日
duqiangedu  LV3 2021年12月16日
LanQian111111  LV1 2021年8月18日
落后就要挨打  LV26 2021年6月16日
耀眼的星星  LV3 2021年4月17日
2469095052  LV8 2021年3月3日
花椒谢霆锋  LV8 2021年3月3日
gaoyangzhi  LV1 2021年1月25日
最近浏览更多
wenpeng182013  LV7 1月6日
Hachi6  LV13 2024年6月27日
3334004690  LV10 2024年6月6日
lee123321  LV22 2023年12月19日
szf123  LV12 2023年5月31日
浪里格朗  LV4 2023年1月31日
二进制2  LV3 2023年1月6日
jjfskldjf  LV2 2022年11月27日
11220011  LV5 2022年11月7日
香菇肉饼汤  LV8 2022年10月30日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友