Java----网络编程

目录

网络通信要素概述:

同信要素一:IP和端口

 InterAddress中一些方法的实现:

通信要素二:网络通信协议

TCP的网络编程:

客户端的实现:

服务端的实现:

例子1:客户端发送信息给服务器,服务器将数据显示在控制台上

例题二:客户端发送文件给服务端,服务端将文件保存在本地

例题三:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功” 给客户端。并关闭相应的连接。

客户端——服务端

客户端:

服务器:

UDP网络编程:

URL类:




网络通信要素概述:

1、通信双方地址:

        > IP

        > 端口号

2、一定的规则(即:网络通信协议。有两套参考模型)

        > OSI参考模型:模型过于理性化,未能在因特网上进行广泛的推广

        > TCP/IP参考模型(或TCP/IP协议):事实上的国际标准

 

  

同信要素一:IP和端口

 

 

 InterAddress中一些方法的实现:

package Java1;

import java.net.InetAddress;
import java.net.UnknownHostException;

/*
*
* */

public class InetAddressTest {
    public static void main(String[] args) {
        //File file = new File("hello.txt");
        try {
            InetAddress inst1 = InetAddress.getByName("192.168.10.14");
            System.out.println(inst1);
            //域名
            InetAddress inet2 = InetAddress.getByName("www.bilibili.com");
            System.out.println(inet2);

            //本地的地址:   127.0.0.1   对应着:localhost
            InetAddress inet3 = InetAddress.getByName("127.0.0.1");
            System.out.println(inet3);

            InetAddress inet4 = InetAddress.getLocalHost();
            System.out.println(inet4);

            //getHostName()
            System.out.println(inet4.getHostName());
            //getHostAddress()
            System.out.println(inet4.getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

通信要素二:网络通信协议

 

 

三次握手:

 四次挥手:



TCP的网络编程:

客户端的实现:

1、创建Socket对象,指明服务器端的ip和端口号

2、获取一个输出流,用于输出数据

3、写出数据的操作

4、资源的关闭

服务端的实现:

1、创建服务器端的ServerSocket,指明自己的端口号

2、调用accept()表示接收来自于客户端的socket

3、获取输入流

4、读取输入流中的数据

5、关闭资源


例子1:客户端发送信息给服务器,服务器将数据显示在控制台上

package Java1;

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;
import java.net.UnknownHostException;

/*
* 实现TCP的网络编程
* 例子1:客户端发送信息给服务器,服务器将数据显示在控制台上
* */

public class TCPTest1 {

    //客户端
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        try {
            //1、创建Socket对象,指明服务器端的ip和端口号
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet,8899);
            //2、获取一个输出流,用于输出数据
            os = socket.getOutputStream();
            //3、写出数据的操作
            os.write("ysb".getBytes());

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4、资源的关闭
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


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

        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //1、创建服务器端的ServerSocket,指明自己的端口号
            ss = new ServerSocket(8899);
            //2、调用accept()表示接收来自于客户端的socket
            socket = ss.accept();
            //3、获取输入流
            is = socket.getInputStream();

        /*
        *不建议这么写,容易出现乱码     中文字节为3,传输时易出错
        *
        byte[] bytes = new byte[200];
        int len ;
        while ((len = is.read(bytes)) != -1) {
            String str = new String(bytes,0,len);
            System.out.print(str);
        }
        */
            //4、读取输入流中的数据
            baos = new ByteArrayOutputStream();
            byte[] bytes = new byte[5];
            int len;
            while ((len = is.read(bytes)) != -1) {
                baos.write(bytes,0,len);
            }

            System.out.println(baos.toString());

            System.out.println("收到了来自于:" + socket.getInetAddress().getHostAddress() + " 的数据");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            //5、关闭资源
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

例题二:客户端发送文件给服务端,服务端将文件保存在本地

package Java1;

import org.junit.Test;

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

/*
* 例题二:客户端发送文件给服务端,服务端将文件保存在本地
*
* 这里涉及到的异常,应该使用try-catch-finally处理
* */

public class TCPTest2 {
    @Test
    public void client() throws IOException {

        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);

        OutputStream os = socket.getOutputStream();

        FileInputStream fis = new FileInputStream(new File("C:\\Users\\Test\\src\\FileTest\\sy.jpg"));

        byte[] bytes = new byte[1024];
        int len;
        while ((len = fis.read(bytes)) != -1) {
            os.write(bytes,0,len);
        }

        fis.close();

        os.close();

        socket.close();
    }

    @Test
    public void server() throws IOException {

        ServerSocket ss = new ServerSocket(9090);

        Socket socket = ss.accept();

        InputStream is = socket.getInputStream();

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

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

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

例题三:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功” 给客户端。并关闭相应的连接。

package Java1;
/*
* 例题三:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”
*        给客户端。并关闭相应的连接。
*
*
* */

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 {

        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);

        OutputStream os = socket.getOutputStream();

        FileInputStream fis = new FileInputStream(new File("C:\\Users\\Test\\src\\FileTest\\sy.jpg"));

        byte[] bytes = new byte[1024];
        int len;
        while ((len = fis.read(bytes)) != -1) {
            os.write(bytes,0,len);
        }
        //关闭数据的输出
        socket.shutdownOutput();

        //接收来自于服务端的数据,并显示到控制台上
        InputStream iss = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] bytes1 = new byte[20];
        int len1;
        while ((len1 = iss.read(bytes1)) != -1) {
            baos.write(bytes1,0,len1);
        }

        System.out.println(baos.toString());

        fis.close();

        os.close();

        socket.close();
        baos.close();
        iss.close();
    }

    @Test
    public void server() throws IOException {

        ServerSocket ss = new ServerSocket(9090);

        Socket socket = ss.accept();

        InputStream is = socket.getInputStream();

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

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

        System.out.println("图片传输完成");
        //服务器端给予客户端反馈
        OutputStream os = socket.getOutputStream();
        os.write("ysb你真棒!".getBytes());

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

客户端——服务端

客户端:

        >自定义

        >浏览器

服务器:

        >自定义

        >Tomcat服务器

UDP网络编程:

 尝试:

package Java1;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.net.*;

public class UDPTest {
    //发送端
    @Test
    public void sender() throws IOException {

        DatagramSocket socket = new DatagramSocket();

        String str = "我是UDP";
        byte[] data = str.getBytes();
        InetAddress inet = InetAddress.getLocalHost();
        DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);

        socket.send(packet);

        socket.close();
    }

    //接收端
    @Test
    public void receiver() throws IOException {
        DatagramSocket socket = new DatagramSocket(9090);

        byte[] bytes = new byte[100];
        DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);

        socket.receive(packet);

        System.out.println(new String(packet.getData(),0,packet.getLength()));

        socket.close();
    }
}

URL类:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kukudeYSB

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值