socket超简单实例

Posted liqiang0728

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了socket超简单实例相关的知识,希望对你有一定的参考价值。

 一个超级简单的socket实现通信

SocketServer

技术图片
public class SocketServer 

    public static void main(String[] args) 
        makeSocketSerer();
    
    
    public static void makeSocketSerer()
        
        ServerSocket serverSocket = null;
        try
            //启动一个服务,指定端口
            serverSocket = new ServerSocket(8888);
            while(true)
                //等待一个接收请求
                Socket socket = serverSocket.accept();
                //lamda表达式
                new Thread(()->
                    try 
                        //读取客户端带来的数据
                        BufferedReader bufferReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        //返回给客户端数据
                        PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
                        
                        while(true)
                            String getClientDate = bufferReader.readLine();
                            if(getClientDate == null)
                                break;
                            
                            System.out.println("server端接收到的消息内容: "+getClientDate);
                            
                            printWriter.println("Hello socket Client!!!");
                            printWriter.flush();
                        
                        printWriter.close();
                        bufferReader.close();
                     catch (Exception e) 
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    
                ).start();
            
        catch(Exception e)
            e.printStackTrace();
        finally
            if(serverSocket != null)
                try 
                    serverSocket.close();
                 catch (IOException e) 
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                
            
        
        
    
View Code

技术图片

 

 

SocketClient

技术图片
public class SocketClient 
    
    public static void main(String[] args) 
        makeSocketClient();
    
    
    public static void makeSocketClient()
        try 
            //定一个ip和端口的socket连接
            Socket socket = new Socket("localhost", 8888);
            
            //接收服务端返回的消息
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            
            //往服务端发送消息
            PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
            printWriter.println("Hello socket Server !!!");
            
            while(true)
                String getServerData = bufferedReader.readLine();
                if(getServerData == null)
                    break;
                
                System.out.println("Client接收到的数据: "+getServerData);
            
            
            printWriter.close();
            socket.close();
         catch (IOException e) 
            // TODO Auto-generated catch block
            e.printStackTrace();
        
    
View Code

技术图片

 

以上是关于socket超简单实例的主要内容,如果未能解决你的问题,请参考以下文章

Python socket通信~简单实例

超详细的Socket通信原理和实例讲解

Python学习之路:socket简单实例

@server,clients简单实例

Linux C Socket编程原理及简单实例

运维开发:Python有趣小程序,Socket通讯简单实例