网络编程
2021/8/12 12:06:28
本文主要是介绍网络编程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
IP
try { InetAddress ia = InetAddress.getByName("www.baidu.com"); InetAddress myHost = InetAddress.getLocalHost(); System.out.println(myHost); // 连带主机名与ip System.out.println("---------"); System.out.println(ia.getHostAddress()); // 获取ip地址 System.out.println(ia.getCanonicalHostName()); // 获取规范的名字 System.out.println(ia.getHostName()); } catch (UnknownHostException e) { e.printStackTrace(); }
output:
LAPTOP-K7UPH2AE/192.168.0.102 --------- 110.242.68.3 110.242.68.3 www.baidu.com
端口
public static void main(String[] args) { InetSocketAddress isa = new InetSocketAddress("www.baidu.com", 3306); InetAddress address = isa.getAddress(); System.out.println(address); System.out.println(isa.getHostName()); System.out.println(isa.getPort()); }
output:
www.baidu.com/110.242.68.3 www.baidu.com 3306
通信协议
TCP聊天
客户端:
public static void main(String[] args) throws Exception { InetAddress ip = InetAddress.getByName("localhost"); Socket socket = new Socket(ip, 9999); OutputStream os = socket.getOutputStream(); os.write("你好,我叫王一赫,我们可以交个朋友吗?".getBytes()); os.close(); socket.close(); }
服务端:
public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(9999); Socket accept = server.accept(); InputStream is = accept.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } System.out.println(baos); baos.close(); is.close(); accept.close(); server.close(); }
TCP文件上传
客户端
{ Socket socket = new Socket(InetAddress.getByName("localhost"), 9000); OutputStream os = socket.getOutputStream(); FileInputStream fis = new FileInputStream("2.jpg"); byte[] buffer = new byte[1024]; int len; while ((len = fis.read(buffer)) != -1) { os.write(buffer, 0, len); } socket.shutdownOutput(); InputStream is = socket.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } System.out.println(baos); baos.close(); is.close(); fis.close(); os.close(); socket.close(); }
服务端
{ ServerSocket server = new ServerSocket(9000); Socket accept = server.accept(); InputStream is = accept.getInputStream(); FileOutputStream fos = new FileOutputStream("out.jpg"); byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); } OutputStream os = accept.getOutputStream(); os.write("我接受到了".getBytes()); os.close(); fos.close(); is.close(); accept.close(); server.close(); }
UDP
UDP 消息发送
接收端
{ public static void main(String[] args) throws Exception { DatagramSocket socket = new DatagramSocket(9900); byte[] data = new byte[1024]; DatagramPacket packet = new DatagramPacket(data, 0, data.length); socket.receive(packet); System.out.println(packet.getAddress()); System.out.println(new String(packet.getData(), 0, packet.getLength())); socket.close(); } }
发送端
{ public static void main(String[] args) throws Exception { DatagramSocket socket = new DatagramSocket(); String data = "你好,接收端"; DatagramPacket packet = new DatagramPacket(data.getBytes(), 0, data.getBytes().length, InetAddress.getByName("localhost"), 9900); socket.send(packet); socket.close(); } }
这篇关于网络编程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-06小米11i印度快充版ROM合集:极致体验,超越期待
- 2024-10-06【ROM下载】小米11i 5G 印度版系统, 疾速跃迁,定义新速度
- 2024-10-06【ROM下载】小米 11 青春活力版,青春无极限,活力全开
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求