Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370078
  • 博文数量: 132
  • 博客积分: 3066
  • 博客等级: 中校
  • 技术积分: 781
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-14 16:19
文章分类

全部博文(132)

文章存档

2012年(1)

2010年(50)

2009年(81)

我的朋友

分类: Java

2010-06-10 11:51:49

JAVA TCP 服务端 程序代码


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();
        }
    }
}

阅读(869) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~