• java基础——网络编程基础TCP,URL


    TCP部分

    new socket对象

    package ip;
    /*
    1.通过IP地址和域名new InetAddress对象
    2.本地回路地址:127.0.0.1 对应localhost,表示本地主机
    3.IP地址和端口号接在一起组成一个socket,类似于网络节点
    @author zsben
    @create 2020-01-07 15:34
    */
    
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    public class Iptest {
    
        public static void main(String[] args) {
            try {
                //File file = new File("hello.txt");File对应文件,Inet对应网络地址
                InetAddress inetAddress = InetAddress.getByName("192.168.10.14");
                System.out.println(inetAddress);
    
                InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");
                System.out.println(inetAddress1);
    
                InetAddress inetAddress2 = InetAddress.getByName("127.0.0.1");
                System.out.println(inetAddress2);
    
                InetAddress inetAddress3 = InetAddress.getLocalHost();
                System.out.println(inetAddress3);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
    
    }

    客户端  发消息给  服务器

    package tcp;
    /*
    实现TCP的网络编程
    例子1:客户端发送信息给服务端,服务端将数据显示到控制台上
    @author zsben
    @create 2020-01-07 16:45
    */
    
    import org.junit.Test;
    
    import java.io.*;
    import java.net.InetAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class TcpTest1 {
    
        //客户端发送数据
        @Test
        public void client()    {
    
            Socket socket = null;
            OutputStream os = null;
    
    
            try {
                //创建socket对象,指明服务器端的ip 和端口号
                InetAddress inet = InetAddress.getByName("172.23.104.31");
                socket = new Socket(inet,10000);
    
                //得到输出流并写数据,进行发送
                os = socket.getOutputStream();
                os.write("你好,我是client".getBytes());
    
            }
            catch (IOException e) {
                System.out.println(e.getMessage());;
            }//资源关闭
            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 {
                ss = new ServerSocket(10000);//服务器端口
    
                socket = ss.accept();//调用accept接受来自客户端的socket
    
                is = socket.getInputStream();//从客户端socket得到输入流
    
                //从输入流里接受字节
                //防止字符的连续字节表示被截断出现乱码,必须保存一个完整的连续串
                baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[5];
                int len;
                while((len = is.read(buffer)) !=-1 )
                    baos.write(buffer,0,len);//数据写到了baos,baos可以自动扩充容量
    
                System.out.println(baos.toString());
    
                //获取客户端的ip地址
                System.out.println("收到了来自"+socket.getInetAddress().getHostAddress()+"的数据");
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                if(ss!=null){
                    try {
                        ss.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(socket!=null){
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(is!=null){
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(baos!=null){
                    try {
                        baos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    
        public static void main(String[] args) {
    
        }
    }

    客户端发文件给服务器

    package tcp;
    /*
    客户端发送文件给服务器,服务器将文件保存在本地:
    知识点:文件传输
    @author zsben
    @create 2020-01-07 22:43
    */
    
    import org.junit.Test;
    
    import java.io.*;
    import java.net.InetAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class TcpTest2 {
        @Test
        public void client(){
            Socket socket = null;
            OutputStream os = null;
            FileInputStream fis = null;
            try {
                //1.获取socket
                InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
                socket = new Socket(inetAddress, 12000);
                //2.获取socket的输出流
                os = socket.getOutputStream();
                //os.write("hello,i am client".getBytes());
                //3.获取文件的输入流
                fis = new FileInputStream("zsbenn.jpg");
                //4.fis->os
                byte[] buffer = new byte[1020];
                int len;
                while((len = fis.read(buffer))!=-1){
                    os.write(buffer,0,len);
                }
    
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            finally {
                if(os!=null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(socket!=null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(fis!=null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void server()  {
            ServerSocket ss = null;
            Socket socket = null;
            InputStream is = null;
            FileOutputStream fos = null;
            try {
                //1.获取服务器socket
                ss = new ServerSocket(12000);
                //获取客户端socket
                socket = ss.accept();
                //获取客户端socket的输入流
                is = socket.getInputStream();
    
                //服务器端创建一个输出流,接受客户端发送的文件
                fos = new FileOutputStream("zsbennn.jpg");
    
                //读取客户端的文件
                byte[] buffer = new byte[1004];
                int len;
                while((len=is.read(buffer)) != -1)
                    fos.write(buffer,0,len);
    
            } catch (IOException e) {
                System.out.println(e.getMessage());;
            }
            finally {
                if(socket!=null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(ss!=null) {
                    try {
                        ss.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();
                    }
                }
            }
        }
    
    }

    客户端发送终止信号给服务器

    package tcp;
    /*
    客户端发送文件给服务器,服务器将文件保存在本地,再给客户端发送一个接受成功
    知识点:输出终止信号使用
    @author zsben
    @create 2020-01-07 22:43
    */
    
    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(){
            Socket socket = null;
            OutputStream os = null;
            FileInputStream fis = null;
            try {
                //1.获取socket
                InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
                socket = new Socket(inetAddress, 12000);
                //2.获取socket的输出流
                os = socket.getOutputStream();
                //os.write("hello,i am client".getBytes());
                //3.获取文件的输入流
                fis = new FileInputStream("zsbenn.jpg");
                //4.fis->os
                byte[] buffer = new byte[1020];
                int len;
                while((len = fis.read(buffer))!=-1){
                    os.write(buffer,0,len);
                }
    
                //关闭数据的输出,强制终止输出
                socket.shutdownOutput();
    
                //5.接受来自服务器的数据,显示到控制台上
                InputStream is = socket.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                while((len = is.read(buffer))!=-1) {
                    baos.write(buffer,0,len);
                }
                System.out.println(baos.toString());
    
                baos.close();
                is.close();
    
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            finally {
                if(os!=null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(socket!=null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(fis!=null){
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void server()  {
            ServerSocket ss = null;
            Socket socket = null;
            InputStream is = null;
            FileOutputStream fos = null;
            try {
                //1.获取服务器socket
                ss = new ServerSocket(12000);
                //2.获取客户端socket
                socket = ss.accept();
                //3.获取客户端socket的输入流
                is = socket.getInputStream();
    
                //4.服务器端创建一个输出流,接受客户端发送的文件
                fos = new FileOutputStream("zsbennn.jpg");
    
                //5.读取客户端的文件
                byte[] buffer = new byte[1004];
                int len;
                //服务器得等到客户端强制终止输出,才算输出完成
                while((len=is.read(buffer)) != -1)
                    fos.write(buffer,0,len);
    
                System.out.println("图片传输完成");
    
                //6.服务器给客户端反馈
                OutputStream os = socket.getOutputStream();
                os.write("数据接收成功".getBytes());
    
                os.close();
    
            } catch (IOException e) {
                System.out.println(e.getMessage());;
            }
            finally {
                if(socket!=null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(ss!=null) {
                    try {
                        ss.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();
                    }
                }
            }
        }
    
    }

    URL部分

    package url;
    /*
    URL:统一资源定位符
    1.URL:同一资源定位符,对应着互联网的某一资源地址
    2.格式:
        http://localhost:8080/examples/beauty.jpg?username=Tom
        协议   主机名     端口号  资源地址              参数列表
    @author zsben
    @create 2020-01-08 22:30
    */
    
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class URLTest1 {
    
        public static void main(String[] args) {
            try {
                URL url = new URL("http://localhost:8080/examples/beauty.jpg?username=Tom");
                System.out.println("get协议名:"+url.getProtocol());
                System.out.println("get主机名:"+url.getHost());
                System.out.println("get端口号:"+url.getPort());
                System.out.println("get路径:"+url.getPath());
                System.out.println("get文件名:"+url.getFile());
                System.out.println("getQuery:"+url.getQuery());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
    
    
        }
    }
    package url;
    /*
    
    
    @author zsben
    @create 2020-01-08 22:46
    */
    
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class URLTest2 {
    
        public static void main(String[] args) {
    
            try {
                URL url = new URL("http://dmimg.5054399.com/allimg/pkm/pk/22.jpg");
                //打开一个关于Http服务器的连接
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                //获取连接
                urlConnection.connect();
                //从链接里获取输入流
                InputStream is = urlConnection.getInputStream();
                FileOutputStream fos = new FileOutputStream("image.jpg");
    
                byte[] buffer = new byte[1020];
                int len;
                while((len = is.read(buffer))!=-1){
                    fos.write(buffer,0,len);
                }
    
                System.out.println("下载完成");
    
                //关闭资源
                is.close();
                fos.close();
                urlConnection.disconnect();//断开连接
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    K3Wise K3List.OCX引入dotnet问题处理方法
    k3wise dotnet开发老单据时序簿工具条添加按钮
    c#调用dotnet写的com组件碰到注册失败的错误的补救方法
    bat注册dotnet com
    dotnet动态加载以及卸载dll的代码
    c#获取com对象的progid
    jpg和png的区别
    Fragment的FragmentTransaction 的commit()和commitAllowingStateLoss()以及commitNow()和commitNowAllowingStateLoss()
    Java泛型类型擦除与运行时类型获取
    rxjava介绍
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12169313.html
Copyright © 2020-2023  润新知