java网络编程笔记
程序员文章站
2022-06-16 18:00:48
网页左边,向下滑有目录索引,可以根据标题跳转到你想看的内容特殊说明,本节笔记,是我学习马士兵java课程记录,所用某些ppt图片也是马士兵教育的课件截图1、InetAddress对象,处理IP地址package com.company;import java.net.InetAddress;import java.net.UnknownHostException;public class Main{ public static void main(String......
网页左边,向下滑有目录索引,可以根据标题跳转到你想看的内容 |
---|
特殊说明,本节笔记,是我学习马士兵java课程记录,所用某些ppt图片也是马士兵教育的课件截图 |
1、InetAddress对象,处理IP地址
package com.company;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Main{
public static void main(String[] args) {
InetAddress localHost = null;
InetAddress byName = null;
try {
localHost = InetAddress.getLocalHost();//获取本机ip地址,结果为XXXXXXX-XXXXXXX/xxx.xxx.xx.xxx,其中前面一串是本机名称,后面是ip
System.out.println(localHost);
byName = InetAddress.getByName("XXXXXXX-XXXXXXX");//根据主机名获取ip地址,我这里用的XXX代表一切字符,毕竟我不能暴露我的主机名给你
System.out.println(byName+"--------"+byName.getHostAddress()+"---------------"+byName.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
2、传输协议和Socket套接字
1、基于TCP的Socket编程
1、基本客户端—服务器案例注意shutdownInput()这些方法
package com.company;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服务端
*/
public class ServerSocketTest {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket server = null;
InputStream inputStream = null;
DataInputStream dataInputStream = null;
OutputStream outputStream = null;
try {
//服务端需要用serverSocket开放本地端口,这样别人就可以通过ip地址和端口号找到你
serverSocket = new ServerSocket(10086);
//要想接受client传过来的数据,也需要Socket对象,不过此对象需要用serverSocket.accept()方法返回
server = serverSocket.accept();
//获取输入流
inputStream = server.getInputStream();
//包装输入流
dataInputStream = new DataInputStream(inputStream);
//读取对应的数据
String s = dataInputStream.readUTF();
System.out.println(s);
server.shutdownInput();//关闭流对象,如果不关,尽管程序执行结束,也依旧占用内存
//获取输出流,给客户端反馈信息
outputStream = server.getOutputStream();
outputStream.write("你好客户端,我收到你的请求了".getBytes());
server.shutdownOutput();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
dataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.company;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
* 客户端
*/
public class Main{
public static void main(String[] args) {
Socket client = null;
OutputStream outputStream = null;
DataOutputStream dataOutputStream = null;
InputStream inputStream = null;
try {
//创建Socket对象,实际上是开启实现io的虚拟接口,此接口不是java接口,而是网络接口,可以与网络通信
//需要指定接收方的ip地址和端口号,这里localhost是本机ip的意思,10086是一个端口号
client = new Socket("localhost", 10086);
//获取输出流,向服务端发送数据
outputStream = client.getOutputStream();
//包装输出流,简化代码
dataOutputStream = new DataOutputStream(outputStream);
//传输数据
dataOutputStream.writeUTF("服务端您好!!!我是客户端发来的请求");
client.shutdownOutput();//关闭流对象,如果不关,尽管程序执行结束,也依旧占用内存
//获取输入流对象,用来接收服务端反馈
inputStream = client.getInputStream();
byte[] buf = new byte[1024];
int readLength = inputStream.read(buf);
System.out.println(new String(buf,0,readLength));
client.shutdownInput();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭这些流
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2、基于UDP的Socket编程(应用场景不多,简单了解一下即可)
package com.company;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
/**
* 客户端
*/
public class Main{
public static void main(String[] args) throws Exception {
//创建UDP通信Socket,随便指定一个自己的端口10000
DatagramSocket datagramSocket = new DatagramSocket(10000);
//从控制台接收数据
System.out.println("请输入要给服务端发送的数据:");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
//构造一个发送指定长度的分组数据包,并指定服务端的端口号
DatagramPacket localhost = new DatagramPacket(str.getBytes(), str.getBytes().length, InetAddress.getByName("localhost"), 9857);
//将分组数据包发送出去
datagramSocket.send(localhost);
datagramSocket.close();
}
}
package com.company;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* 服务端
*/
public class ServerSocketTest {
public static void main(String[] args) throws Exception {
DatagramSocket datagramSocket = new DatagramSocket(9857);
byte[] buf = new byte[1024];
// 创建数据包对象
DatagramPacket datagramPacket = new DatagramPacket(buf,buf.length);
// 利用数据包对象接收数据
datagramSocket.receive(datagramPacket);
System.out.println("客户端发来数据:"+new String(datagramPacket.getData(),0,datagramPacket.getLength()));
datagramSocket.close();
}
}
本文地址:https://blog.csdn.net/grd_java/article/details/109862876
上一篇: JS实现炫酷轮播图
下一篇: C#事件处理的代理机制