IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> Java 网络编程 -> 正文阅读

[网络协议]Java 网络编程

Java 网络编程

基于TCP协议的Socket网络通信,用来实现双向安全连接网络通信。

Socket通信模型

进行网络通信时,Socket需要借助数据流来完成数据的传递工作

TCP

public class IntAddress {
    //一个工具类,ip地址,静态方法
    public static void main(String[] args) {
        try {
            InetAddress localHost = InetAddress.getLocalHost();//本地服务器
            //主机名+ip地址
            System.out.println(localHost);

            InetAddress byName = InetAddress.getByName("www.baidu.com");
            System.out.println(byName);//www.baidu.com/39.156.66.18
             System.out.println(byName.getHostAddress());//39.156.66.18
            System.out.println(byName.getHostName());//www.baidu.com

            InetAddress byName1 = InetAddress.getByName("14.215.177.38");
            System.out.println(byName1);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
public class Server {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(8000);
            //获取客户端的信息
            Socket accept = serverSocket.accept();
            InputStream inputStream = accept.getInputStream();
            //转换为字符串
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            while ((line = bufferedReader.readLine()) != null)
                System.out.println(line);
            serverSocket.close();
            accept.close();
            inputStream.close();
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}




public class Client {
    public static void main(String[] args) {
        //创建Socket
        try {
            Socket socket = new Socket("localhost",8000);
            //获取输出流
            OutputStream outputStream = socket.getOutputStream();
            //客户端把数据写出去
            String info="你好吗?";
            outputStream.write(info.getBytes());
            socket.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public class User implements Serializable {
    private String userName;
    private String password;

    public User() {
    }

    public User(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}


public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(9000);
            Socket accept = serverSocket.accept();
            InputStream inputStream = accept.getInputStream();
            //
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            User user=(User)objectInputStream.readObject();
            System.out.println("客户端发来的信息是:"+user);

            //回复客户端,从Socket中获取输出流
            String line="登录成功";
            OutputStream outputStream = accept.getOutputStream();
            outputStream.write(line.getBytes());

            serverSocket.close();
            accept.close();
            inputStream.close();
            objectInputStream.close();
            outputStream.close();

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



public class Client {
    public static void main(String[] args) {

        try {
            Socket socket = new Socket("127.0.0.1",9000);
            OutputStream outputStream = socket.getOutputStream();
            //序列化    对象需要序列化
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            //把用户写进去
            objectOutputStream.writeObject(new User("小明","123456"));

            //拿服务器端返回来的数据
            InputStream inputStream = socket.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line=null;
            while ((line=bufferedReader.readLine())!=null)
                System.out.println("服务端发来的信息:"+line);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public class User implements Serializable {
    private String userName;
    private String password;

    public User() {
    }

    public User(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}


public class Client1 {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost",11000);
            OutputStream outputStream = socket.getOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(new User("小明","123456"));

            //等待服务器传过来的消息
            InputStream inputStream = socket.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String info=null;
            while ((info=bufferedReader.readLine())!=null)
                System.out.println("服务器发来的消息:"+info);

            socket.close();
            outputStream.close();
            objectOutputStream.close();
            inputStream.close();
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



public class Client2 {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost",11000);
            OutputStream outputStream = socket.getOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(new User("小李","111111"));

            //等待服务器传过来的消息
            InputStream inputStream = socket.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String info=null;
            while ((info=bufferedReader.readLine())!=null)
                System.out.println("服务器发来的消息:"+info);


            socket.close();
            outputStream.close();
            objectOutputStream.close();
            inputStream.close();
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}






public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(11000);
            while(true)
            {
                Socket accept = serverSocket.accept();
                //创建一个线程 一个客户就是一个线程
                ThreadDemo threadDemo = new ThreadDemo(accept);
                //启动线程
                threadDemo.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}







public class ThreadDemo extends Thread{
    Socket accpect=null;
    public ThreadDemo(Socket accpect){
        this.accpect=accpect;
    }
    @Override
    public void run() {
        try {
            InputStream inputStream = accpect.getInputStream();
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            User user=(User)objectInputStream.readObject();
            System.out.println("客户端发来的消息:"+user);

            //
            String line="欢迎你,"+user.getUserName()+",登录成功";
            OutputStream outputStream = accpect.getOutputStream();
            outputStream.write(line.getBytes());

            accpect.close();
            objectInputStream.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

UDP

public class UDPClient {
    public static void main(String[] args) throws Exception {
        //1.创建一个数据包,把数据装到这个包裹中  host port
        //byte[] buf, int length, InetAddress address, int port
        //获取InetAddress对象
        String line="你好";
        InetAddress localHost = InetAddress.getLocalHost();
        DatagramPacket datagramPacket = new DatagramPacket(line.getBytes(),line.getBytes().length,localHost,9000);

        //2.发送包裹
        DatagramSocket socket = new DatagramSocket();
        socket.send(datagramPacket);

        //3.接收包裹
        byte[] bytes = new byte[1024];
        DatagramPacket datagramPacket1 = new DatagramPacket(bytes,bytes.length);
        socket.receive(datagramPacket1);
        //4.处理包裹
        System.out.println(new String(bytes));
        //5.关流
        socket.close();
    }
}
public class UDPServer {
    public static void main(String[] args) throws Exception {
        //1.接收包裹
        DatagramSocket socket = new DatagramSocket(9000);
        //2.拿取数据    byte[] buf, int length
        byte[] bytes = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length);
        socket.receive(datagramPacket);
        //3.展示数据
        System.out.println(new String(bytes));
        //4.给客户端返回数据
        String info = "收到";
        //5.发送包裹    byte[] buf, int length, SocketAddress address
        DatagramPacket datagramPacket1 = new DatagramPacket(info.getBytes(),info.getBytes().length,datagramPacket.getSocketAddress());
        socket.send(datagramPacket1);
        //6.关流
        socket.close();
    }
}
  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-07-11 16:55:33  更:2021-07-11 16:55:39 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/25 17:46:36-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码