文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>JAVA TCP 服务端 程序代码

JAVA TCP 服务端 程序代码

时间:2010-06-10  来源:lzn_sc

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

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载