使用Socket模拟一个简单的Webservice调用

webservice是对socket的一个封装,让远程调用调用变得更加简单,那么使用socket究竟有多么麻烦呢?来看看。
做一个简单的天气查询:
服务端:

public class SocketServer {

    public static void main(String[] args) {
        DataInputStream dataInputStream = null;
        DataOutputStream dataOutputStream = null;
        try {
            // 启动socket服务端
            ServerSocket serverSocket = new ServerSocket(8888);
            /**
             * 加上while(true),让服务端一直保持运行,负责当有一个客户端连接成功以后,服务端就停止运行了。
             */
            while (true) {
                    // 监听客户端连接,accept()方法是阻塞方法,如果没有客户端连接,其后的代码不会执行
                    Socket socket = serverSocket.accept();
                    dataInputStream = new DataInputStream(
                            socket.getInputStream());
                    dataOutputStream = new DataOutputStream(
                            socket.getOutputStream());
                    //获得客户端传来的城市名称
                    String cityName = dataInputStream.readUTF();
                    System.out.println("from client..." + cityName);

                    //查询天气并返回查询结果
                    String result = "晴天";
                    dataOutputStream.writeUTF(result);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(dataOutputStream!=null)
                    dataOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(dataInputStream!=null)
                    dataInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

客户端:

public class SocketClient {
    public static void main(String[] args) {
        Socket socket = null;
        DataOutputStream dataOutputStream = null;
        DataInputStream dataInputStream = null;
        try {
            //创建Socket连接
            socket = new Socket("127.0.01", 8888);
            //得到输出流
            dataOutputStream = new DataOutputStream(socket.getOutputStream());
            //发送数据
            dataOutputStream.writeUTF("海口");
            //接收数据
            dataInputStream = new DataInputStream(socket.getInputStream());
            //得到输入流
            String result = dataInputStream.readUTF();
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(socket!=null)
                    socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(dataInputStream!=null)
                    dataInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(dataOutputStream!=null)
                    dataOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

使用socket都是用流来传输数据,要防止编码问题,要打开流,关闭流,而如果使用webservice则完全不必考虑这些。

原文地址:https://www.cnblogs.com/lenve/p/4517984.html