Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2222950
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类:

2011-08-18 15:40:21

原文地址:JAVA TCP 服务端 程序代码 作者:jhluroom

package com.linux.vntcenter;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class VNTCenter {
    /**
     * @param args
     */

    public static final int SERVER_PORT=2010;
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("VNTCenter.main()===TCP SERVER==============");
        try{
            ServerSocket vntServer= new ServerSocket(SERVER_PORT);
            System.out.println("Listening Port is "+vntServer.getLocalPort()+"...");
            while(true){
                Socket vntClient=vntServer.accept();
                System.out.println("have a client entering , the IP is "+vntClient.getInetAddress()+"...");
                System.out.println("have a client entering , the Port is "+vntClient.getPort()+"...");
                new GetGpsThreadFun(vntClient).start();
            }
            //vntClient.close();

        }catch (Exception e) {
            // TODO: handle exception

            e.printStackTrace();
        }
    }
}

class GetGpsThreadFun extends Thread{
    private Socket vntThreadClient;
    public GetGpsThreadFun(Socket vntThreadSocket){
        vntThreadClient=vntThreadSocket;
    }
    public void run(){
        try{
            BufferedReader vntThreadIS=new BufferedReader(new InputStreamReader(vntThreadClient.getInputStream()));
            while(true){
                String vntReceiveString=vntThreadIS.readLine();
                if (vntReceiveString!=null){
                    System.out.println(vntThreadClient.getInetAddress()+":"+vntThreadClient.getPort()+":"+vntReceiveString);
                }
                if("shutdown".equals(vntReceiveString)){
                    break;
                }    
            }
            vntThreadIS.close();
            vntThreadClient.close();
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}


阅读(975) | 评论(0) | 转发(0) |
0

上一篇:设计模式 源码

下一篇:网络协议学习

给主人留下些什么吧!~~