InetAddress类
其对象用来表示一个ip地址
package java2;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressTest {
public static void main(String[] args) {
InetAddress inet1 = null;
try {
inet1 = InetAddress.getByName("192.168.10.14");
System.out.println(inet1);// /192.168.10.14
InetAddress inet2 = InetAddress.getByName("www.baidu.com");
System.out.println(inet2);// www.baidu.com/36.152.44.95
InetAddress inet3 = InetAddress.getByName("127.0.0.1");
System.out.println(inet3);// /127.0.0.1
//获取本地ip
InetAddress inet4 = InetAddress.getLocalHost();
System.out.println(inet4);// DESKTOP-5RQTA0T/169.254.25.151
//
System.out.println(inet2.getHostName());//www.baidu.com
System.out.println(inet2.getHostAddress());//36.152.44.96
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
三个例题
- 例子1:客户端发送信息给服务端,服务端将数据显示在控制台上
package java2;
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;
//例题1:客户端发送信息给服务端,服务端将数据显示在控制台上
public class TCPTest1 {
//客户端
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
try {
//1、创建Socket对象,指明服务器端的ip和端口号
InetAddress inet = InetAddress.getByName("192.168.1.110");//服务器地址:此处为本机ip地址
socket = new Socket(inet,8888);
//2、获取一个输出流,用于输出数据
os = socket.getOutputStream();
//3、写出数据的操作
os.write("你好,我是客户端".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//4、资源的关闭
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.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(8888);
//2、调用accept()方法,接收来自客户端的Socket
socket = ss.accept();
//3、获取一个输入流
is = socket.getInputStream();
//不建议这样写:会出现乱码问题
// byte[] buffer = new byte[20];
// int len;
// while ((len = is.read(buffer)) != -1){
// String str = new String(buffer,0,len);
// System.out.println(str);
//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());//显示客户端ip地址:192.168.1.110
} 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();
}
}
}
}
- 例题2:客户端发送文件给服务器,服务端将文件保存在本地。
package java2;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
//例题2:客户端发送文件给服务器,服务端将文件保存在本地。
public class TCPTest2 {
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
try {
//1、创建Socket对象,指明服务器端的ip和端口号
InetAddress inet1 = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet1,8888);
//2、获取一个输出流,用于输出数据
os = socket.getOutputStream();
//3、写出数据的操作
byte[] buffer = new byte[20];
int len;
fis = new FileInputStream(new File("学生证.jpg"));
while ((len = fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4、资源的关闭
if(fis != null)
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
if (os != null)
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
if (socket != null)
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void server(){
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
FileOutputStream fos = null;
try {
//1、创建服务器端的ServerSocket,指明自己的端口号
ss = new ServerSocket(8888);
//2、调用accept()方法,接收来自客户端的Socket
socket = ss.accept();
//3、获取一个输入流
is = socket.getInputStream();
//4、读取输入流中的数据
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[20];
int len;
while ((len = is.read(buffer)) != -1){
baos.write(buffer,0,len);
}
byte[] buffer1 = baos.toByteArray();
fos = new FileOutputStream(new File("学生证_服务器.jpg"));
fos.write(buffer1);
} catch (IOException e) {
e.printStackTrace();
} finally {
//5、资源的关闭
if (fos != null)
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
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();
}
}
}
}
- 例题3:客户端发送文件给服务器,服务端将文件保存在本地,并返回“发送成功”给客户端
package java2;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
//例题3:客户端发送文件给服务器,服务端将文件保存在本地,并返回“发送成功”给客户端
public class TCPTest3 {
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
ByteArrayOutputStream baos = null;
InputStream is = null;
try {
//1、创建Socket对象,指明服务器端的ip和端口号
InetAddress inet1 = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet1,8888);
//2、获取一个输出流,用于输出数据
os = socket.getOutputStream();
//3、写出数据的操作
byte[] buffer = new byte[20];
int len;
fis = new FileInputStream(new File("学生证.jpg"));
while ((len = fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
//表示图片已经传完,关闭数据的输出,用于终止服务端的while循环
socket.shutdownOutput();
//4、接收服务端的回复
baos = new ByteArrayOutputStream();
byte[] buffer1 = new byte[20];
int len1;
is = socket.getInputStream();
while ((len1 = is.read(buffer1)) != -1){
baos.write(buffer1,0,len1);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
//5、资源的关闭
if(fis != null)
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
if (os != null)
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
if (socket != null)
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
if (baos != null)
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
if (is != null)
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void server(){
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
FileOutputStream fos = null;
OutputStream os = null;
try {
//1、创建服务器端的ServerSocket,指明自己的端口号
ss = new ServerSocket(8888);
//2、调用accept()方法,接收来自客户端的Socket
socket = ss.accept();
//3、获取一个输入流
is = socket.getInputStream();
//4、读取输入流中的数据
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[20];
int len;
while ((len = is.read(buffer)) != -1){
baos.write(buffer,0,len);
}
byte[] buffer1 = baos.toByteArray();
fos = new FileOutputStream(new File("学生证_服务器1.jpg"));
fos.write(buffer1);
//5、服务端收到文件后,给予客户端反馈
os = socket.getOutputStream();
os.write("文件接收成功".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//6、资源的关闭
if (fos != null)
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
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();
}
if (os != null)
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}