Java 发送Socket

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class Main {
    public static void main(String[] args) throws IOException {
        //定义服务器的IP
        String host="10.10.10.10";
        //定义服务器的端口
        int Port=9095;

        String body="";
        String jsonstr=" " + body;
        Socket socket = null;
        try {
            socket = new Socket(host,Port);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            OutputStream outputStream=socket.getOutputStream();
            outputStream.write(jsonstr.getBytes());
            outputStream.flush();
            socket.shutdownOutput();
        }catch (UnknownHostException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        InputStream inputStream=socket.getInputStream();
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
        String result=null;
        while ((result=bufferedReader.readLine())!=null){
            System.out.println(result);
        }
        inputStream.close();
    }
}

 

posted @ 2018-01-29 16:48  Curious1986  阅读(308)  评论(0编辑  收藏  举报