首页 > 其他 > 详细

socket server

时间:2020-09-11 23:16:42      阅读:55      评论:0      收藏:0      [点我收藏+]

 

public static void main(String[] args) {
    String addressIP="localhost";
    int addressPort=8899;
    
    startServerSocket(addressIP,addressPort);
}

public static void startServerSocket(String addressIP,int addressPort) {
    try {
        serverSocket = new ServerSocket(addressPort);
        Socket acceptSocket = serverSocket.accept();
        
        DataInputStream dis = new DataInputStream(acceptSocket.getInputStream());
        DataOutputStream dos = new DataOutputStream(acceptSocket.getOutputStream());
        
        while(true) {
            //boolean isClosed = isServerClose(acceptSocket);//判断是否断开
            
            boolean isClosed = acceptSocket.isClosed();
            if(!isClosed) {
                InetAddress spcketAddress = acceptSocket.getInetAddress();
                String hostName = spcketAddress.getHostAddress();
                String receiveMsg = dis.readUTF();
                
                System.out.println("host name:"+hostName+"  msg:"+receiveMsg);
                
                sendData(dos);
            }
            
        }
        
        
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    
    
}

 

socket server

原文:https://www.cnblogs.com/herd/p/13653942.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!