package com.jwt.network;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddress_ {
public static void main(String[] args) throws UnknownHostException {
//获取本机InetAddress 对象
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
//根据指定主机名/域名获取对象
InetAddress host2 = InetAddress.getByName("jianjian");
System.out.println(host2);
InetAddress host3 = InetAddress.getByName("www.baidu.com");
System.out.println(host3);
//获取InetAddress对象的主机名
String host3Name = host3.getHostName();
System.out.println(host3Name);
//获取InetAddress对象的地址
String host3Address = host3.getHostAddress();
System.out.println(host3Address);
}
}
/**
jianjian/192.168.50.204
jianjian/192.168.50.204
www.baidu.com/14.215.177.39
www.baidu.com
14.215.177.39
**/
TCP,传输前先开服务端,accept,等客户端接入,然后获得客户端 socket 然后进行 IO 操作
Step 1
:创建ServerSocket对象,绑定监听的端口
Step 2
:调用accept()方法监听客户端的请求
step 3
:连接建立后,通过输入流读取客户端发送的请求信息
Step 4
:通过输出流向客户端发送响应信息
Step 5
:关闭相关资源
package com.jwt.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketTCP01Server {
public static void main(String[] args) throws IOException {
//1. 监听本机的9999 端口, 等待连接
// 细节: 要求在本机没有其它服务在监听9999
// 细节:这个ServerSocket 可以通过accept() 返回多个Socket[多个客户端连接服务器的并发]
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("我是服务端,正在监听9999端口,等待连接...");
//2. 当没有客户端连接9999 端口时,程序会阻塞, 等待连接
// 如果有客户端连接,则会返回Socket 对象,程序继续
Socket socket = serverSocket.accept();
System.out.println("服务端socket=" + socket.getClass());
//3. 通过输入流,读取客户端写入到数据通道的数据, 显示
InputStream inputStream = socket.getInputStream();
//4. IO 读取
byte[] buf = new byte[1024];
int readLen = 0;
while ((readLen=inputStream.read(buf))!= -1) {
System.out.println(new String(buf,0,readLen));
}
//5. 通过输出流,写入数据到数据通道
OutputStream outputStream = socket.getOutputStream();
outputStream.write("服务端说:你好,客户端!".getBytes());
// 设置结束标记
socket.shutdownOutput();
//6.关闭流和socket
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
System.out.println("服务端退出.....");
}
}
package com.jwt.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class SocketTCP01Client {
public static void main(String[] args) throws IOException {
//1. 连接服务端(ip , 端口)如果连接成功,返回Socket 对象
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端socket返回=" + socket.getClass());
//2. 连接上后,生成Socket, 通过socket.getOutputStream()
OutputStream outputStream = socket.getOutputStream();
//3. 通过输出流,写入数据到数据通道
outputStream.write("客户端说:你好,服务端!".getBytes());
// 设置结束标记
socket.shutdownOutput();
//4. 获取和socket 关联的输入流. 读取数据(字节),并显示
InputStream inputStream = socket.getInputStream();
byte[] buf = new byte[1024];
int readLen = 0;
while ((readLen=inputStream.read(buf))!= -1) {
System.out.println(new String(buf,0,readLen));
}
//5. 关闭流对象和socket, 必须关闭
inputStream.close();
outputStream.close();
socket.close();
System.out.println("客户端退出.....");
}
}
package com.jwt.socket;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketTCP02Server {
public static void main(String[] args) throws IOException {
//1. 监听本机的9999 端口, 等待连接
// 细节: 要求在本机没有其它服务在监听9999
// 细节:这个ServerSocket 可以通过accept() 返回多个Socket[多个客户端连接服务器的并发]
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("我是服务端,正在监听9999端口,等待连接...");
//2. 当没有客户端连接9999 端口时,程序会阻塞, 等待连接
// 如果有客户端连接,则会返回Socket 对象,程序继续
Socket socket = serverSocket.accept();
System.out.println("服务端socket=" + socket.getClass());
//3. 通过socket.getInputStream() 读取客户端写入到数据通道的数据, 显示
InputStream inputStream = socket.getInputStream();
//4. IO 读取, 使用字符流, 使用InputStreamReader 将inputStream 转成字符流
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
System.out.println(s);
//5. 通过输出流,写入数据到数据通道
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("服务端说:你好,客户端!");
// 设置结束标记
bufferedWriter.newLine();// 插入一个换行符,表示回复内容的结束
bufferedWriter.flush();//注意需要手动的flush
//6.关闭流和socket
bufferedWriter.close();
bufferedReader.close();
socket.close();
serverSocket.close();
System.out.println("服务端退出.....");
}
}
package com.jwt.socket;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketTCP02Server {
public static void main(String[] args) throws IOException {
//1. 监听本机的9999 端口, 等待连接
// 细节: 要求在本机没有其它服务在监听9999
// 细节:这个ServerSocket 可以通过accept() 返回多个Socket[多个客户端连接服务器的并发]
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("我是服务端,正在监听9999端口,等待连接...");
//2. 当没有客户端连接9999 端口时,程序会阻塞, 等待连接
// 如果有客户端连接,则会返回Socket 对象,程序继续
Socket socket = serverSocket.accept();
System.out.println("服务端socket=" + socket.getClass());
//3. 通过socket.getInputStream() 读取客户端写入到数据通道的数据, 显示
InputStream inputStream = socket.getInputStream();
//4. IO 读取, 使用字符流, 使用InputStreamReader 将inputStream 转成字符流
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
System.out.println(s);
//5. 通过输出流,写入数据到数据通道
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("服务端说:你好,客户端!");
// 设置结束标记
bufferedWriter.newLine();// 插入一个换行符,表示回复内容的结束
bufferedWriter.flush();//注意需要手动的flush
//6.关闭流和socket
bufferedWriter.close();
bufferedReader.close();
socket.close();
serverSocket.close();
System.out.println("服务端退出.....");
}
}
package com.jwt.socket;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPFileUploadServer {
public static void main(String[] args) throws Exception {
//1. 服务端在本机监听8888端口
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务端监听8888端口....");
//2. 等待连接
Socket socket = serverSocket.accept();
//3. 读取客户端发送的数据,通过Socket 得到输入流
System.out.println("服务端开始接收图片");
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] bytes = StreamUtils.streamToByteArray(bis);
//4. 将得到bytes 数组,写入到指定的路径,就得到一个文件了
String destFilePath = "src//a.jpeg";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFilePath));
bos.write(bytes);
bos.flush();
System.out.println("服务端接收图片成功");
//5.向客户端回复"收到图片"通过socket 获取到输出流(字符)
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write("服务端:收到图片");
bw.flush();//把内容刷新到数据通道
socket.shutdownOutput();//设置写入结束标记
//6.关闭其他资源
bw.close();
bos.close();
bis.close();
socket.close();
serverSocket.close();
System.out.println("服务端退出.....");
}
}
/**
服务端监听8888端口....
服务端开始接收图片
服务端接收图片成功
服务端退出.....
**/
package com.jwt.socket;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TCPFileUploadClient {
public static void main(String[] args) throws Exception {
//1.客户端连接服务端8888,得到Socket 对象
Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
//2.创建读取磁盘文件的输入流
System.out.println("客户端开始发送图片");
String filePath = "/Users/jianjian/Downloads/a.jpeg";
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
//bytes 就是filePath 对应的字节数组
byte[] bytes = StreamUtils.streamToByteArray(bis);
//3.通过socket 获取到输出流, 将bytes 数据发送给服务端
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write(bytes);
bos.flush();
socket.shutdownOutput();//设置写入数据的结束标记
System.out.println("客户端发送图片成功");
//4.接收从服务端回复的消息
InputStream inputStream = socket.getInputStream();
//使用StreamUtils 的方法,直接将inputStream 读取到的内容转成字符串
String s = StreamUtils.streamToString(inputStream);
System.out.println(s);
//5.关闭相关的流
inputStream.close();
bos.close();
bis.close();
socket.close();
System.out.println("客户端退出.....");
}
}
/**
客户端开始发送图片
客户端发送图片成功
服务端:收到图片
客户端退出.....
**/
UDP,以数据报作为数据的传输载体,在进行传输时首先要把传输的数据定义成数据报(Datagram),在数据报中指明数据要到达的Socket(主机地址和端口号),然后再将数据以数据报的形式发送出去,然后就没有然后了,服务端收不收到我就不知道了,除非服务端收到后又给我回一段确认的数据报。
package com.jwt.socket;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPReceiverA {
public static void main(String[] args) throws IOException {
//1. 创建一个DatagramSocket 对象,准备在9999 接收数据
DatagramSocket socket = new DatagramSocket(9999);
//2. 构建一个DatagramPacket 对象,准备接收数据
// 一个UDP数据包最大64k
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf,buf.length);
System.out.println("接收端A等待接收数据..");
//3. 调用接收方法, 将通过网络传输的DatagramPacket 对象,填充到packet 对象
//当有数据包发送到本机的9999 端口时,就会接收到数据
// 如果没有数据包发送到本机的9999 端口, 就会阻塞等待.
socket.receive(packet);
//4. 把packet进行拆包,取出数据,并显示
int length = packet.getLength();
byte[] data = packet.getData();
String s = new String(data, 0, length);
System.out.println(s);
//5. 回复信息给B端,将需要发送的数据,封装到DatagramPacket 对象
data = "接收端:好的, 明天见".getBytes();
//内容字节数组, 数组长度,主机(IP),端口
packet = new DatagramPacket(data, data.length, InetAddress.getByName("jianjian"), 9998);
socket.send(packet);
//6. 关闭资源
socket.close();
System.out.println("A 端退出...");
}
}
/**
接收端A等待接收数据..
发送端:hello 明天吃火锅~
A 端退出...
**/
package com.jwt.socket;
import java.io.IOException;
import java.net.*;
public class UDPSenderB {
public static void main(String[] args) throws IOException {
//1.创建DatagramSocket 对象,准备在9998 端口接收数据
DatagramSocket socket = new DatagramSocket(9998);
//2. 将需要发送的数据,封装到DatagramPacket 对象
byte[] data = "发送端:hello 明天吃火锅~".getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName("jianjian"), 9999);
socket.send(packet);
//3. 接收从A 端回复的信息
//(1).构建一个DatagramPacket 对象,准备接收数据
byte[] buf = new byte[1024];
packet = new DatagramPacket(buf, buf.length);
//(2) 调用接收方法, 将通过网络传输的DatagramPacket 对象,填充到packet 对象
socket.receive(packet);
//(3) 可以把packet 进行拆包,取出数据,并显示.
int length = packet.getLength();//实际接收到的数据字节长度
data = packet.getData();//接收到数据
String s = new String(data, 0, length);
System.out.println(s);
//4. 关闭资源
socket.close();
System.out.println("B 端退出");
}
}
/**
接收端:好的, 明天见
B 端退出
**/
package com.jwt.socket.homework;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Homework01Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("我是服务端,正在监听9999端口,等待连接...");
Socket socket = serverSocket.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String s = br.readLine();
System.out.println(s);
String answer = "";
if ("name".equals(s)) {
answer = "我是nova";
} else if("hobby".equals(s)) {
answer = "编写java程序";
} else {
answer = "你说啥呢";
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(answer);
bw.flush();
socket.shutdownOutput();
bw.close();
br.close();
socket.close();
serverSocket.close();
System.out.println("服务端退出.....");
}
}
package com.jwt.socket.homework;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class Homework01Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getByName("jianjian"),9999);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//从键盘读取用户的问题
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的问题:");
String question = scanner.next();
bw.write(question);
bw.flush();
socket.shutdownOutput();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String s = br.readLine();
System.out.println(s);
br.close();
bw.close();
socket.close();
System.out.println("客户端退出.....");
}
}
package com.jwt.socket.homework;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class Homework02ReceiverA {
//- (1)编写一个接收端A和一个发送端B,使用UDP协议完成
//- (2)接收端在8888端口等待接收数据
//- (3)发送端向接收端发送数据"四大名著是哪些"
//- (4)接收端接收到发送端发送的问题后,返回"四大名著是<<红楼梦>> ..”,否则返回what?
//- (5)接收端和发送端程序退出
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(8888);
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf,0,buf.length);
System.out.println("接收端A等待接收数据..");
socket.receive(packet);
int length = packet.getLength();
byte[] data = packet.getData();
String s = new String(data,0,length);
System.out.println(s);
String anser = "";
if("四大名著是哪些".equals(s)){
anser = "四大名著是<<红楼梦>>...";
} else {
anser = "What?";
}
buf = anser.getBytes();
packet = new DatagramPacket(buf, buf.length, InetAddress.getByName("jianjian"), 9999);
socket.send(packet);
socket.close();
System.out.println("A 端退出");
}
}
package com.jwt.socket.homework;
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
public class Homework02SenderB {
//- (1)编写一个接收端A和一个发送端B,使用UDP协议完成
//- (2)接收端在8888端口等待接收数据
//- (3)发送端向接收端发送数据"四大名著是哪些"
//- (4)接收端接收到发送端发送的问题后,返回"四大名著是<<红楼梦>> ..”,否则返回what?
//- (5)接收端和发送端程序退出
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(9999);
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的问题:");
String question = scanner.next();
byte[] buf = question.getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getByName("jianjian"), 8888);
socket.send(packet);
buf = new byte[1024];
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
int length = packet.getLength();
byte[] data = packet.getData();
String s = new String(data, 0, length);
System.out.println(s);
socket.close();
System.out.println("B 端退出");
}
}
package com.jwt.socket.homework;
import com.jwt.socket.StreamUtils;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Homework03Server {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端监听9999端口....");
Socket socket = serverSocket.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String s = br.readLine();
System.out.println(s);
//发送音乐
System.out.println("服务端开始发送音乐");
String music = "";
if ("高山流水".equals(s)){
music = "高山流水.mp3";
} else {
music = "无名.mp3";
}
String filePath = "src//" + music;
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
byte[] bytes = StreamUtils.streamToByteArray(bis);
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write(bytes);
bos.flush();
socket.shutdownOutput();//设置写入数据的结束标记
System.out.println("客户端发送音乐成功");
bos.close();
bis.close();
socket.close();
serverSocket.close();
System.out.println("服务端退出...");
}
}
package com.jwt.socket.homework;
import com.jwt.socket.StreamUtils;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class Homework03Client {
public static void main(String[] args) throws Exception {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
Scanner scanner = new Scanner(System.in);
System.out.println("输入音乐名:");
String downMusic = scanner.next();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(downMusic);
bw.flush();
socket.shutdownOutput();
//读取客户端发送的数据,通过Socket 得到输入流
System.out.println("客户端开始接收音乐");
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] bytes = StreamUtils.streamToByteArray(bis);
// 将得到bytes 数组,写入到指定的路径,就得到一个文件了
String destFilePath = "/Users/jianjian/Downloads/" + downMusic + ".mp3";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFilePath));
bos.write(bytes);
bos.flush();
System.out.println("客户端接收音乐成功");
bos.close();
bis.close();
bw.close();
socket.close();
System.out.println("客户端退出...");
}
}