博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java网络编程一
阅读量:6345 次
发布时间:2019-06-22

本文共 9477 字,大约阅读时间需要 31 分钟。

 

1.InetAddress的应用

import java.util.List;import java.math.BigDecimal;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.Map;import java.util.LinkedList;import java.util.Scanner;import java.util.Set;import java.util.Stack;import javax.naming.spi.DirStateFactory.Result;import org.omg.PortableInterceptor.DISCARDING;import java.util.Map;import java.util.Queue;/** * InetAddress类 * @author 16114 * */public class Main{    public static void main(String[] args) {        try {            InetAddress address = InetAddress.getLocalHost();            System.out.println("计算机名:" + address.getHostName());            System.out.println("IP地址:" + address.getHostAddress());            byte[] bytes = address.getAddress();            System.out.println("字节数组形式的IP:" + Arrays.toString(bytes));            System.out.println(address);        } catch (UnknownHostException e) {            e.printStackTrace();        }        try {            InetAddress address2 = InetAddress.getByName("Roni");            System.out.println(address2.getHostName());            System.out.println(address2.getHostAddress());        } catch (UnknownHostException e) {            e.printStackTrace();        }                    }}

 2.URL

import java.util.List;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.math.BigDecimal;import java.net.InetAddress;import java.net.MalformedURLException;import java.net.URL;import java.net.UnknownHostException;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.Map;import java.util.LinkedList;import java.util.Scanner;import java.util.Set;import java.util.Stack;import javax.naming.spi.DirStateFactory.Result;import org.omg.PortableInterceptor.DISCARDING;import java.util.Map;import java.util.Queue;/** * URL类 * @author 16114 * */public class Main{    public static void main(String[] args) {                try {            URL url = new URL("http://www.baidu.com");            try {                InputStream is = url.openStream();                InputStreamReader isr = new InputStreamReader(is,"utf-8");                BufferedReader br = new BufferedReader(isr);                String data = br.readLine();                while(data!=null) {                    System.out.println(data);                    data=br.readLine();                }                br.close();                isr.close();                is.close();            } catch (IOException e) {                e.printStackTrace();                            }                    } catch (MalformedURLException e) {            e.printStackTrace();        }            }}

 3.客户端向服务器端的通信

Server

package com.zrl.thread;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.ServerSocket;import java.net.Socket;import java.util.Scanner;/** * 基于TCP协议的Socket协议 * 服务端 * @author 16114 * */public class Server {    public static void main(String[] args) {        //1.创建一个服务器端Socket,即ServerSocket,指定绑定的端口        try {            ServerSocket serverSocket = new  ServerSocket(8888);            //2.调用accept()方法开始监听,等待客户端的连接            System.out.println("***服务器即将启动,等待客户端的链接***");            Socket socket = serverSocket.accept();            //3.获取输入流,并读取客户端信息            InputStream is = socket.getInputStream();            InputStreamReader isr = new InputStreamReader(is);            BufferedReader br = new BufferedReader(isr);            String info = null;            while((info=br.readLine())!=null) {                System.out.println("我是服务器,客户端说:" + info);            }            socket.shutdownInput();            //4.关闭相关资源            br.close();            isr.close();            is.close();            socket.close();            serverSocket.close();        } catch (IOException e) {            e.printStackTrace();        }    }}

Client

package com.zrl.thread;import java.io.IOException;import java.io.OutputStream;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;public class Client {    public static void main(String[] args) {        //1.创建客户端Socket,指定服务器端地址和端口        Socket socket;        try {            socket = new Socket("localhost", 8888);            //2.获取输出流,向服务器发送消息            OutputStream os = socket.getOutputStream();            PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流            pw.write("用户名:admin;密码:123");            pw.flush();            socket.shutdownInput();            //3.关闭资源            pw.close();            os.close();            socket.close();        } catch (UnknownHostException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }            }}

 一个服务端,多个客户端

Server.java

package com.zrl.thread;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.util.Scanner;/** * 基于TCP协议的Socket协议 * 服务端 * @author 16114 * */public class Server {    public static void main(String[] args) {        //1.创建一个服务器端Socket,即ServerSocket,指定绑定的端口        try {            ServerSocket serverSocket = new  ServerSocket(8888);            //2.调用accept()方法开始监听,等待客户端的连接            System.out.println("***服务器即将启动,等待客户端的链接***");            Socket socket = null;            //记录客户端的数量            int cnt = 0;            //循环监听等待客户端的连接            while(true) {                //调用accept()方法开始监听,等待客户端的连接                socket = serverSocket.accept();                //创建一个新线程                ServerThread serverThread = new ServerThread(socket);                //启动线程                serverThread.start();                cnt++;                System.out.println("客户端的数量:" + cnt);                InetAddress address = socket.getInetAddress();                System.out.println("当前客户端的IP:" + address.getHostAddress());            }        } catch (IOException e) {            e.printStackTrace();        }    }}

Client.java

package com.zrl.thread;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;public class Client {    public static void main(String[] args) {        //1.创建客户端Socket,指定服务器端地址和端口        Socket socket;        try {            socket = new Socket("localhost", 8888);            //2.获取输出流,向服务器发送消息            OutputStream os = socket.getOutputStream();            PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流            pw.write("用户名:admin; 密码:456");            pw.flush();            socket.shutdownOutput();            //3.获取输入流并且读取服务器端的响应信息            InputStream is = socket.getInputStream();            InputStreamReader isr = new InputStreamReader(is);            BufferedReader br = new BufferedReader(isr);            String info = null;            while((info=br.readLine())!=null) {                System.out.println("我是客户端,服务端说:" + info);            }            //4.关闭资源            br.close();            is.close();            pw.close();            os.close();            socket.close();        } catch (UnknownHostException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }            }}

ServerThread.java

package com.zrl.thread;/** * 服务器端线程处理类 * @author 16114 * */import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.Socket;public class ServerThread extends Thread {    //和本线程相关的Socket    Socket socket = null;    public ServerThread(Socket socket) {        this.socket = socket;    }    //线程执行的操作,响应客户端的请求    @Override    public void run() {        //获取输入流,并读取客户端信息        InputStream is=null;        InputStreamReader isr=null;        BufferedReader br=null;        OutputStream os =null;        PrintWriter pw = null;        try {            is = socket.getInputStream();            isr = new InputStreamReader(is);            br = new BufferedReader(isr);            String info = null;            while((info=br.readLine())!=null) {                System.out.println("我是服务器,客户端说:" + info);            }            socket.shutdownInput();            //获取输出流,响应客户端的请求            os = socket.getOutputStream();            pw = new PrintWriter(os); //包装为打印流            pw.write("欢迎您!");            pw.flush(); //调用flush将缓冲输出                    } catch (IOException e) {            e.printStackTrace();        }finally {
//关闭相关资源 try { if(pw!=null) pw.close(); if(os!=null) os.close(); if(br!=null) br.close(); if(isr!=null) isr.close(); if(is!=null) is.close(); if(socket!=null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } }}

 

转载于:https://www.cnblogs.com/Roni-i/p/9788983.html

你可能感兴趣的文章
c# 免费版pdf转word尝试
查看>>
iOS学习笔记12-UISearchBar
查看>>
用代码实现以下程序:篮子中有10个玩具,每60秒取出3个,同时每40秒向篮子中放入1个,不断重复上述动作,当篮子中剩余玩具不足3个是,程序结束...
查看>>
使用JDBC-ODBC读取Excel文件
查看>>
(一)在HTML页面中实现一个简单的Tab
查看>>
jinian
查看>>
SQL Serever学习5——数据库配置
查看>>
codevs——3344 迷宫
查看>>
Google Map API使用详解(五)——Google Map基本常识(下)
查看>>
实验1-8 输出倒三角图案
查看>>
ios 检查内存泄露
查看>>
ios retain copy assign相关
查看>>
Tcp/ip简介
查看>>
第九章 类
查看>>
50 years, 50 colors
查看>>
数据结构与算法----->数据结构----->树------->二叉树的遍历
查看>>
 空格用法
查看>>
Linux命令
查看>>
ubuntu16.04下配置JDK 1.8+安装Java EE,并实现最大子数组算法
查看>>
1015. 德才论 (25)
查看>>