Java笔记(21)-Java网络通信常识(Socket的使用)

1.基础介绍

  1. InetAddress类的对象可以用来标识一个IP地址,但是这个类私有化了构造方法,不过可以通过一些它的静态方法进行创建对象;
    一些实例化方法:getByNane(String),获取含IP的对象;getLocalHost(),获取本地回环地址127.0.0.1;
  2. TCP协议,一个可靠的协议;有“三次握手”和“四次挥手”,
  3. 三次握手:首先客户端发送syn信息给服务端,然后服务端发送syn信息以及确认信息ack给客户端,再然后客户端再次发送消息,告诉服务端自己还在,然后就是连接完成;(我的理解)
  4. 四次挥手:首先客户端请求断开连接(发送FIN),然后就是服务端收到返回确认消息(ACK),在之后服务端断开与客户端的连接,并通知客户端(发送FIN),最后客户端发送消息给服务端,用来确定已经和服务端断开连接;(我的理解,详细,请百度)
  5. 自定义端口号要是5001-65535
  6. UDP的网络编程中,使用的主要类有DatagramSocket,发送数据使用DatagramSocket中的send方法;使用这个协议传输数据的时候,属于那种无连接传输,就是不管服务端是否开启,它只是负责传输;以下是UDP协议打包数据的所用到的类;
    DatagramPacket packet = new DatagramPacket(data, start length,data.length, InetAddress对象,port);
  7. URL:统一资源定位符;5个基本结构:传输协议,主机号,端口号,文件名,参数;
  8. URL的实例化可以是:
    URL url = new URL(String);

2.Socket 和ServerSocket 的基本用法

  1. 使用Socket 传输文件,并做到简单的交互(只能在不同的编译软件上尝试了,eclipse和IDEA,服务端放在IDEA,客户端放在eclipse发现可以,那么就算是跨机运行应该也只需要修改IP位置),这是TCP协议下的数据的传输;
public class FileServerce {

    public static void main(String[] args) {
        OutputStream outputStream = null;
        Socket sc = null;
        InputStream is = null;
        OutputStream out = null;
        try {
            //定义一个服务端口号;这个端口号先出来的;
            ServerSocket ss = new ServerSocket(9545);
            //这里定义一个sc表明从端口号7070客户端获得的;
            sc = ss.accept();
            //获得客户端的输出流;
            is = sc.getInputStream();
            ByteArrayOutputStream thisout = new ByteArrayOutputStream();

            out = new FileOutputStream(new File("text.txt"));
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = is.read(bytes)) != -1) {
                thisout.write(bytes, 0, len);
            }
            out.write(thisout.toByteArray());
            System.out.println(thisout.toString());
            System.out.println("文件已经存储");
            //完成接受后写出返回值;
            outputStream = sc.getOutputStream();
            outputStream.write("文件已经收到!".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //对各种流进行关闭;
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (sc != null) {
                try {
                    sc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

模拟的客户端:

public class FileSocketClient {
    public static void main(String[] args) {
        InetAddress inet;
        Socket sc=null;
        BufferedReader br = null;
        OutputStream os=null;
        InputStream is=null;
        try {
            //由于没有公开的构造方法,所以只能这样获得InetAddress的对象;
            inet = InetAddress.getLocalHost();
            sc = new Socket(inet,9545);
            os = sc.getOutputStream();
            //传输文件;
            br = new BufferedReader(new FileReader(new File("newfile.txt")));
            String length = null;
            while((length=br.readLine())!=null){
                os.write(length.getBytes());
            }
            //像服务器发出传输完毕的消息,防止服务器一直等待;
            sc.shutdownOutput();
            //然后是从服务端接收消息;
            is = sc.getInputStream();
            //这里我服务端就只是返回一个返回一个字符串;就直接存到字节数组,然后再控制台输出;
            ByteArrayOutputStream bto = new ByteArrayOutputStream();
            int len = -1;
            byte[] brr= new byte[1];
            while ((len=is.read(brr))!=-1){
                bto.write(brr,0,len);
            }
            System.out.println(bto.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //对各种流进行关闭;
            if(br!=null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(sc!=null){
                try {
                    sc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/dxsdcyy/article/details/106839448
今日推荐