码迷,mamicode.com
首页 > 编程语言 > 详细

java中TCP传输协议

时间:2015-08-13 14:25:45      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:java   tcp   

class TcpClient {

    public static void main(String[] args) throws Exception {
        //创建客户端的socket服务,指定目的主机和端口
        Socket s = new Socket("192.168.1.10",10003);
        //为了发送数据,获取socket流中的输出流
        java.io.OutputStream out = s.getOutputStream();
        out.write("hello tcp".getBytes());
        s.close();
    }
}

class TcpServer {

    public static void main(String[] args) throws Exception {
        //建立服务端socket服务,并监听一个端口
        ServerSocket ss = new ServerSocket(10003);
        //通过accept方法获取连接过来的客户端对象
        Socket s = ss.accept();
        String ip = s.getInetAddress().getHostAddress();
        //获取客户端发送过来的数据,使用客户端对象的读取流来读取数据
        InputStream in = s.getInputStream();

        byte[] buf = new byte[1024];
        int len = in.read(buf);
        System.out.println(new String(buf,0,len));
        //关闭客户端
        s.close();

    }
}


class TcpClient2 {

    public static void main(String[] args) throws Exception {
        //建立socket服务,指定连接的主机和端口
        Socket s = new Socket("192.168.1.10",10004);
        //获取socket流中的输出流。将数据写入该流,通过网络传送给服务端
        OutputStream out = (OutputStream) s.getOutputStream();
        out.write("hello Tcp".getBytes());
        //获取socket流中的输入流,将服务端反馈的数据获取到,并打印
        InputStream in = s.getInputStream();
        byte[] buf = new byte[1024];
        int len = in.read(buf);
        System.out.println(new String(buf,0,len));
        s.close();

    }
}

class TcpServer2 {

    public static void main(String[] args) throws Exception {

        ServerSocket ss = new ServerSocket(10004);
        Socket s = ss.accept();
        //服务端收信息
        InputStream in = s.getInputStream();
        String ip = s.getInetAddress().getHostAddress();

        byte[] buf = new byte[1024];
        int len = in.read(buf);
        System.out.println(new String(buf,0,len));

        //服务端发信息
        OutputStream out = (OutputStream) s.getOutputStream();
        out.write("have receive".getBytes());
        s.close();
        ss.close();

    }
}


/*需求:建立一个文本转换服务器
客户端给服务端发送文本,服务端会将文本转成大写再返还给客户端*/

class TcpClient3 {

    public static void main(String[] args) throws Exception {
        Socket s = new Socket("192.168.1.4",10005);
        //定义读取键盘数据的流对象,读取键盘录入文本
        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

        //获取socket流中的输出流,通过网络传送到服务端
        BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

        //定义一个socket读取流,读取服务端返回的大写信息
        BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));

        String line = null;
        while((line=bufr.readLine())!=null) {
            if("over".equals(line))
                break;
            bufOut.write(line);
            bufOut.newLine();//结束标记
            bufOut.flush();

            String str = bufIn.readLine();
            System.out.println("server:"+str);
        }
        bufr.close();
        s.close();
        }
}

class TcpServer3 {

    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(10005);
        Socket s = ss.accept();
        //读取socket读取流中的数据
        BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));

        //socket输出流,将大写文本写入到socket输出流,并发送给客户端 
        BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

        String line = null;
        while((line=bufIn.readLine())!=null) {
            bufOut.write(line.toUpperCase());
            bufOut.newLine();//结束标记
            bufOut.flush();
        }
        s.close();
        ss.close();
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

java中TCP传输协议

标签:java   tcp   

原文地址:http://blog.csdn.net/sjtu_chenchen/article/details/47611121

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!