栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

TCP的网络编程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

TCP的网络编程

package Internet;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;


public class TCPTest {

//    客户端
    @Test
    public void client() throws IOException {
        Socket socket = null;
        OutputStream os = null;
        try {
            InetAddress inet = InetAddress.getByName("192.168.1.104");
            socket = new Socket(inet,8899);
            os = socket.getOutputStream();
            os.write("你好呀我是客户端gg".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //        资源关闭
           if (os != null){
               os.close();
           }
           if (socket != null){
               socket.close();
           }
        }


    }

//    服务端
    @Test
    public void server() throws IOException {

        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            ss = new ServerSocket(8899);

            socket = ss.accept();

            is = socket.getInputStream();

//            若构建的数组太小,可能会有乱码
//       byte[] buffer = new byte[1024];
//       int len;
//       while ((len=is.read(buffer))!=-1){
//           String str = new String(buffer,0,len);
//           System.out.println(str);
//       }

//        在 baos中有一个可扩容的数组,会自动存取所有字符
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[5];
            int len ;
            while ((len = is.read(buffer))!=-1){

                baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(baos!=null){
                baos.close();
            }
            if(is!=null){
                is.close();
            }
            if(socket!=null){
                socket.close();
            }
            if(ss!=null){
                ss.close();
            }
        }


    }
}

二:

package Internet;

import org.junit.Test;

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

public class TCPTest2 {
    @Test
    public void client() throws IOException {
//        1.
        Socket socket = new Socket("127.0.0.1",8080);
//        2.
        OutputStream os = socket.getOutputStream();
//        3.
        FileInputStream fis = new FileInputStream(new File("1.jpg"));
//        4.
        byte[] buffer = new byte[5];
        int len ;
        while ((len = fis.read(buffer))!=-1){
            os.write(buffer,0,len);
        }
//        5.
        fis.close();
        os.close();
        socket.close();
    }



    @Test
    public void server() throws IOException {

        ServerSocket ss = new ServerSocket(8080);

        Socket socket = ss.accept();

        InputStream is = socket.getInputStream();

        FileOutputStream fos = new FileOutputStream(new File("2.jpg"));

        byte[] buffer = new byte[5];
        int len ;
        while ((len = is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }

        fos.close();
        is.close();
        socket.close();
        ss.close();


    }
}

三:

package Internet;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;


public class TCPTest3 {

    @Test
    public void client() throws IOException {
//          1.
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),8080);
//          2.
        OutputStream os = socket.getOutputStream();
//          3.
        FileInputStream fis = new FileInputStream(new File("1.jpg"));
//          4.
        byte[] buffer = new byte[5];
        int len ;
        while ((len = fis.read(buffer))!=-1){
            os.write(buffer,0,len);
        }
//       图片传完了不在输出数据
        socket.shutdownOutput();

//          5.接受服务器端的数据并显示到控制台上
        InputStream is = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer1 = new byte[5];
        int len1 ;
        while ((len = is.read(buffer1))!=-1){
            baos.write(buffer1,0,len);
        }
        System.out.println(baos.toString());

//          6,
        fis.close();
        os.close();
        socket.close();
        is.close();
        baos.close();

    }

    @Test
    public void server() throws IOException {
//        1.
        ServerSocket ss = new ServerSocket(8080);
//        2.
        Socket socket = ss.accept();
//        3.
        InputStream is = socket.getInputStream();
//        4.
        FileOutputStream fos = new FileOutputStream(new File("3.jpg"));
//        5.
        byte[] buffer = new byte[5];
        int len ;
//        由于read时阻塞式的方法,没有明确告诉就不会结束循环。因此需要明确的指示结束(29行)
        while ((len = is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }

//        6.服务器端给与客户端反馈
        OutputStream os = socket.getOutputStream();
    //        字符需要转换成字节流
        os.write("照片已收到!很漂亮!".getBytes());

//        7.
        fos.close();
        is.close();
        socket.close();
        ss.close();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/737193.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号