Java 网络编程

Java 网络编程

一、IP地址

1. 什么是IP地址

2. IP地址的组成

IP地址= 网络地址 + 主机地址
网络地址:标识计算机或网络设备所在的网段
主机地址:标识特定主机或网络设备

3. IP地址的配置和检测

1)查看本机的IP地址:ipconfig。

2)测试网络是否通畅:ping 目标IP地址。

二、Socket

1. 简介

Socket的底层机制复杂,Java平台提供了一些简单的API,可以更简单有效的使用Socket开发而无需了解底层机制。

2. 发送数据

public class Client {
    public static void main(String[] args) {
        // 创建Socket对象

        try(// 通信链路的客户端点创建成功
            Socket socket = new Socket("localhost", 8088);
            // 获取输出流将登录信息发送给服务器端
            OutputStream os = socket.getOutputStream();
            // 获取输入流接收服务器端发送的响应信息
            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is))) {

            String str = "用户名:tom,密码:123456";
            byte[] bytes = str.getBytes();
            // 输出信息
            os.write(bytes);
            System.out.println("向服务器发送请求成功!");
            // 关闭输出流
            socket.shutdownOutput();

            // 获取输入流接收服务器端发送的响应信息
            String str2;
            while ((str2 = br.readLine()) != null) {
                System.out.println("这里是客户端,接收到的服务器发送来的响应信息为:");
                System.out.println(str2);
            }

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

    }
}

3. 接收数据

public class Server {
    public static void main(String[] args) {
        try(// 创建ServerSocket
            ServerSocket ss = new ServerSocket(8088);
            // 侦听客户端的请求
            Socket soc = ss.accept();
            // 获取输入流接收客户端的请求信息
            InputStream is = soc.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            // 获取输出流向客户端发送响应信息
            OutputStream os = soc.getOutputStream()) {

            String str;
            while ((str = br.readLine()) != null) {
                System.out.println("这里是服务器端,接收到的客户端信息为:");
                System.out.println(str);
            }

            // 获取输出流向客户端发送响应信息
            Thread.sleep(2000);
            String str2 = "用户名和密码正确,登录成功";
            byte[] bytes = str2.getBytes();
            os.write(bytes);

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

三、InetAddress、DatagramPacket、DatagramSocket

1. 移动端(发送端)

public class Send {
	/*
	 * 示例06:升级示例05,发送方发送咨询问题,接收方回应咨询。
	 * 
	 * 发送方实现步骤如下: 
	 * (1)获取本地主机的InetAddress对象。 
	 * (2)创建DatagramPacket对象,封装要发送的信息。
	 * (3)利用DatagramSocket对象将DatagramPacket对象数据发送出去。
	 */

	public static void main(String[] args) {
		DatagramSocket ds = null;
		InetAddress ia = null;
		String mess = "你好,我想咨询一个问题。";
		System.out.println("我说:" + mess);
		try {
			// 获取本地主机地址
			ia = InetAddress.getByName("localhost");
			// 创建DatagramPacket对象,封装数据
			DatagramPacket dp = new DatagramPacket(mess.getBytes(),
					mess.getBytes().length, ia, 8800);
			// 创建DatagramSocket对象,向服务器发送数据
			ds = new DatagramSocket();
			ds.send(dp);

			byte[] buf = new byte[1024];
			DatagramPacket dpre = new DatagramPacket(buf, buf.length);
			ds.receive(dpre);
			// 显示接收到的信息
			String reply = new String(dpre.getData(), 0, dpre.getLength());
			System.out.println(dpre.getAddress().getHostAddress() + "说:"
					+ reply);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			ds.close();
		}

	}
}

2. 服务器(接收端)

public class Receive {

	public static void main(String[] args) {
		/*
		 * 示例06:升级示例05,发送方发送咨询问题,接收方回应咨询。
		 * 
		 * 接收方实现步骤如下: 
		 * (1)创建DatagramPacket对象,准备接收封装的数据。
		 * (2)创建DatagramSocket对象,接收数据保存于DatagramPacket对象中。
		 * (3)利用DatagramPacket对象处理数据。
		 */

		DatagramSocket ds = null;
		DatagramPacket dp = null;
		DatagramPacket dpto = null;
		// 创建DatagramPacket对象,用来准备接收数据
		byte[] buf = new byte[1024];
		dp = new DatagramPacket(buf, 1024);
		try {
			// 创建DatagramSocket对象,接收数据
			ds = new DatagramSocket(8800);
			ds.receive(dp);
			// 显示接收到的信息
			String mess = new String(dp.getData(), 0, dp.getLength());
			System.out.println(dp.getAddress().getHostAddress() + "说:" + mess);

			String reply = "你好,我在,请咨询!";
			// 显示与本地对话框
			System.out.println("我  说:" + reply);
			// 创建DatagramPacket对象,封装数据
			SocketAddress sa = dp.getSocketAddress();
			dpto = new DatagramPacket(reply.getBytes(),
					reply.getBytes().length, sa);
			ds.send(dpto);
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			ds.close();
		}

	}
}

s().length, sa);
ds.send(dpto);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
ds.close();
}

}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Glensea

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

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

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

打赏作者

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

抵扣说明:

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

余额充值