本文共--字 阅读约--分钟 | 浏览: -- Last Updated: 2022-06-25
IP:唯一的表示Internet上的计算机(通信实体),在Java中使用InetAddress类代表IP。
IP分类:IPv4和IPv6;万维网和局域网;
端口号标识正在计算机上运行的进程,不同的进程有不同的端口号,被规定为一个16位二进制的整数,0~65535,分为:
端口和IP地址组合得出了一个网络套接字。
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressTest {
public static void main(String[] args) {
try {
InetAddress inet1 = InetAddress.getByName("192.169.10.11");
System.out.println(inet1); // /192.169.10.11
InetAddress inet2 = InetAddress.getByName("www.baidu.com"); // www.baidu.com/14.215.177.39
System.out.println(inet2);
// InetAddress inet3 = InetAddress.getByName("www.baidu111111111111.com");
// Error www.baidu111111111111.com: nodename nor servname provided, or not known
InetAddress inet4 = InetAddress.getByName("127.0.0.1");
System.out.println(inet4); // /127.0.0.1
InetAddress inet5 = InetAddress.getLocalHost();
System.out.println(inet5); // upcccz.local/127.0.0.1
System.out.println(inet2.getHostName()); // www.baidu.com
System.out.println(inet2.getHostAddress()); // 14.215.177.39
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPTest {
// 客户端
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
try {
// 1、创建Socket对象,指明服务器端的ip和端口号
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 8899);
// 2、获取一个输出流,用于输出数据
os = socket.getOutputStream();
// 3、写出数据的操作
os.write("你好,我是客户端".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4、资源的关闭
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 服务端
@Test
public void server() {
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
// 1、创建服务器端的ServerSocket,指明自己的端口号
ss = new ServerSocket(8899);
// 2、调用accept()表示接收来自客户端的socket
socket = ss.accept();
// 3、获取输入流
is = socket.getInputStream();
// 4、读取输入流中的数据
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[5];
int len;
while((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
System.out.println("收到了来自:" + socket.getInetAddress().getHostAddress() + "的数据");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 5、资源关闭
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPTest2 {
@Test
public void client() throws IOException {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
// 通过fis读取本地的1.jpg 通过socket的os发送给服务端
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("1.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
// 关闭数据的输出,表示已经输出完成
socket.shutdownOutput();
// 通过is接收来自服务端的数据,通过baos输出并显示在控制台上
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while ((len2 = is.read(buffer2)) != -1) {
baos.write(buffer2, 0, len2);
}
System.out.println("服务端发来的消息" + baos.toString());
baos.close();
is.close();
fis.close();
os.close();
socket.close();
}
@Test
public void server() throws IOException {
ServerSocket ss = new ServerSocket(9090);
Socket socket = ss.accept();
// 通过is接收客户端传入的数据
InputStream is = socket.getInputStream();
// 通过fos将接收到的数据写到本地为upload.jpg
FileOutputStream fos = new FileOutputStream(new File("upload.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
// 通过os输出流 将服务端给予客户端反馈
OutputStream os = socket.getOutputStream();
os.write("已收到数据".getBytes());
os.close();
fos.close();
is.close();
socket.close();
ss.close();
}
}
import org.junit.Test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPTest {
//发送端
@Test
public void sender() throws IOException {
DatagramSocket socket = new DatagramSocket();
String str = "我是UDP方式发送的信息";
byte[] data = str.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
socket.send(packet);
socket.close();
}
//接收端
@Test
public void receiver() throws IOException {
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
public class URLTest {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/examples/beauty.jpg?username=Tom");
// public String getProtocol() 获取该URL的协议名
System.out.println(url.getProtocol()); // http
// public String getHost() 获取该URL的主机名
System.out.println(url.getHost()); // localhost
// public String getPort() 获取该URL的端口号
System.out.println(url.getPort()); // 8080
// public String getPath() 获取该URL的文件路径
System.out.println(url.getPath()); // /examples/beauty.jpg
// public String getFile() 获取该URL的文件名
System.out.println(url.getFile()); // /examples/beauty.jpg?username=Tom
// public String getQuery() 获取该URL的查询名
System.out.println(url.getQuery());// username=Tom
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
通过URL获取服务器上的图片并写入本地
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLTest2 {
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("http://www.upcccz.cn/blog/static/hugo-learn-theme/images/weixin-sou2.png");
// 建立连接 正式去访问这个url
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
// 将接收到的数据写到本地
is = urlConnection.getInputStream();
fos = new FileOutputStream("day01/1.jpg");
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
System.out.println("下载完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(urlConnection != null){
urlConnection.disconnect();
}
}
}
}