JAVA----网络编程

package java1;

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

/**
 *
 * 1.IP:唯一标识Internet上的计算机
 * 2.在java中使用InetAddress类代表IP
 * 3.域名:www.baidu.com
 * 4.如何实例化InetAddress:两个方法:getByName(String host)、getLocalHost()
 *      两个常用方法:getHostName()、getHostAddress()
 * 5.端口号:标识正在计算机上运行的进程。
 * 6.端口号与ip地址的组合为网络套接字:socket
 *
 *
 * @create 2022-04-13 21:45
 */
public class InetAddressTest {

    public static void main(String[] args) throws UnknownHostException {
        InetAddress inet1= InetAddress.getByName("192.168.1.1");
        System.out.println(inet1);
        InetAddress inet2=InetAddress.getByName("baidu.com");
        System.out.println(inet2);

        InetAddress inet3=InetAddress.getByName("127.0.0.1");
        System.out.println(inet3);

        //获取本地ip
        InetAddress inet4=InetAddress.getLocalHost();
        System.out.println(inet4);

        System.out.println(inet2.getHostName());
        System.out.println(inet2.getHostAddress());



    }
}

package java1;

import org.junit.Test;

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

/**
 *
 * 实现TCP的网络编程
 * 例子1:客户端发送信息给服务端,服务端将数据显示在控制台上。
 * @create 2022-04-13 22:52
 */
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,11111);
            //2.获取一个输出流,用于输出数据
            os = socket.getOutputStream();
            //3.写出数据
            os.write("你好,我是客户端GG".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            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(11111);
            //2.调用accept()接收来自于客户端的socket
            socket = ss.accept();
            //3.获取输入流
            is = socket.getInputStream();

            //4.读取输入流中的数据
            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());
            System.out.println("来源:"+socket.getInetAddress().getHostAddress());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            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;
import java.net.UnknownHostException;

/**
 *
 *
 * 客户端发送文件给服务端,服务端将文件保存在本地
 * @create 2022-04-14 10:37
 */
public class TCPTest2 {
    @Test
    public void client()   {
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 11111);
            os = socket.getOutputStream();

            fis = new FileInputStream(new File("4.png"));

            byte[] buffer=new byte[1024];
            int len;
            while((len=fis.read(buffer))!=-1){
                os.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            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;
        FileOutputStream fos = null;
        try {
            ss = new ServerSocket(11111);
            socket = ss.accept();
            is = socket.getInputStream();
            fos = new FileOutputStream(new File("5.png"));

            byte[] buffer=new byte[1024];
            int len;
            while((len=is.read(buffer))!=-1){
                fos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos!=null){
                try {
                    fos.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;

/**
 *
 * @create 2022-04-14 11:10
 */
public class TCPTest3 {

    @Test
    public void client()   {
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            socket = new Socket(InetAddress.getByName("127.0.0.1"), 11111);
            os = socket.getOutputStream();
            fis = new FileInputStream(new File("5.png"));

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

            socket.shutdownOutput();

            //接收来自服务器端的数据

            is = socket.getInputStream();
            baos = new ByteArrayOutputStream();

            byte[] buffer1=new byte[1024];
            int len1;
            while((len1=is.read(buffer1))!=-1){
                baos.write(buffer1,0,len1);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(baos!=null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            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;
        FileOutputStream fos = null;
        OutputStream os = null;
        try {
            ss = new ServerSocket(11111);
            socket = ss.accept();
            is = socket.getInputStream();
            fos = new FileOutputStream(new File("6.png"));

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

            System.out.println("图片传输完成");

            os = socket.getOutputStream();
            os.write("已收到!".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(os!=null){
                try {
                    os.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos!=null){
                try {
                    fos.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 com.sun.xml.internal.ws.api.message.Packet;
import org.junit.Test;

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

/**
 *
 *
 * @create 2022-04-14 12:21
 */
public class UDPTest {
    @Test
    public void sender() throws IOException {
        DatagramSocket socket = new DatagramSocket();

        String str="你好!!!";
        byte[] data=str.getBytes();

        InetAddress inet = InetAddress.getLocalHost();
        DatagramPacket packet = new DatagramPacket(data, 0, data.length,inet,11111);

        socket.send(packet);
        socket.close();

    }

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

        byte[] buffer=new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);

        socket.receive(packet);

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

        socket.close();


    }
}

package java1;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 *
 * @create 2022-04-14 12:59
 */
public class URLTest1 {

    public static void main(String[] args) throws IOException {
        URL url =new URL("http:/myweb/3.png");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.connect();

        InputStream is = urlConnection.getInputStream();
        FileOutputStream fos = new FileOutputStream("day10\\222.png");

        byte[] buffer=new byte[1024];
        int len;
        while((len=is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }
        System.out.println("下载完成");

        if(fos!=null){
            fos.close();
        }
        if(is!=null){
            is.close();
        }
        if(urlConnection!=null){
            urlConnection.disconnect();
        }
    }
}
posted @ 2022-04-14 15:20  ice--cream  阅读(25)  评论(0编辑  收藏  举报