socket 开发一

Posted zhuanzhuan

tags:

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

其实web 开发的底层就是socket 编程

数据是通过流读取

服务端

public class WeatherServer {
    public static void main(String[] args) throws IOException {
        // 创建socket 服务,应用服务端口建议在1万以上
        ServerSocket server = null;
        Socket socket = null;
        DataInputStream di=null ;
        DataOutputStream ds=null ;
        try {
            server = new ServerSocket(12345);
            // 按受客户端链接
            socket = server.accept();
            //为了好处理字符串
            // 接受客户端请求
            di=new DataInputStream(socket.getInputStream());
            // 向服务端发送请求
            ds=new DataOutputStream(socket.getOutputStream());
            //客户端向服务端发送的天气所在城市
            String cityName =di.readUTF();
            System.out.println("from client...."+cityName);
            //服务端向客户端返回天气情况
            String result ="晴";
            ds.writeUTF("晴");
            System.out.println("to client...."+result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            di.close();
            ds.close();
            //服务端一般是不关闭资源的,由客户端关闭
            //socket.close();
        }
        
        
    }

}

客户端代码:

public class WeatherClient {
    public static void main(String[] args) throws UnknownHostException, IOException {
        Socket client = null;
        DataInputStream di = null;
        DataOutputStream ds = null;
        client = new Socket("127.0.0.1", 12345);
        try {
            
            // 接受客户端请求
            di = new DataInputStream(client.getInputStream());
            // 向服务端发送请求
            ds = new DataOutputStream(client.getOutputStream());
            String cityName ="北京";
            ds.writeUTF(cityName);
            String result =di.readUTF();
            System.out.println("接受服务端.."+result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            di.close() ;
            ds.close();
            client.close();
        }
        
    }

}

 其实服务端开发要有可持续能力,保证线程一直在运行,那么上面的代码就可以改成这样:

public class WeatherServerV2 {
    public static void main(String[] args) throws IOException {
        // 创建socket 服务,应用服务端口建议在1万以上
        ServerSocket server = null;
        Socket socket = null;
        DataInputStream di=null ;
        DataOutputStream ds=null ;
        while(true){
            try {
                server = new ServerSocket(12345);
                // 按受客户端链接
                socket = server.accept();
                //为了好处理字符串
                // 接受客户端请求
                di=new DataInputStream(socket.getInputStream());
                // 向服务端发送请求
                ds=new DataOutputStream(socket.getOutputStream());
                //客户端向服务端发送的天气所在城市
                String cityName =di.readUTF();
                System.out.println("from client...."+cityName);
                //服务端向客户端返回天气情况
                String result ="晴";
                ds.writeUTF("晴");
                System.out.println("to client...."+result);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                di.close();
                ds.close();
                //服务端一般是不关闭资源的,由客户端关闭
                //socket.close();
            }
        }
    
        
        
    }

}

同样的道理,服务端不仅要有持续运行能力,还要有处理并发的能力,我们可以把客户端也改成死循环

public class WeatherClientVersion2 {
    public static void main(String[] args) throws UnknownHostException, IOException {
        Socket client = null;
        DataInputStream di = null;
        DataOutputStream ds = null;
        client = new Socket("127.0.0.1", 12345);
        while(true){
            try {
                
                // 接受客户端请求
                di = new DataInputStream(client.getInputStream());
                // 向服务端发送请求
                ds = new DataOutputStream(client.getOutputStream());
                String cityName ="北京";
                ds.writeUTF(cityName);
                String result =di.readUTF();
                System.out.println("接受服务端.."+result);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                di.close() ;
                ds.close();
                client.close();
            }
            
        }
        }
    

}

 

以上是关于socket 开发一的主要内容,如果未能解决你的问题,请参考以下文章

下一代Dex编译器现已进入预览阶段

动态SQL基础概念复习(Javaweb作业5)

高效Web开发的10个jQuery代码片段

一步一图一代码,一定要让你真正彻底明白红黑树

使用 Socket.IO 开发聊天室

miniui后台无法接收到input传值