系统学习javaweb-05-网络编程
Write By CS逍遥剑仙
我的主页: csxiaoyao.com
GitHub: github.com/csxiaoyaojianxian
Email: sunjianfeng@csxiaoyao.com
QQ: 1724338257
1 InetAddress
InetAddress(IP类)
常用方法
----getLocalHost() 获取本机的IP地址对象
----getByName("IP或者主机名") 根据IP地址的字符串或主机名生成IP地址对象(用于获取别人的IP地址对象)
--------getHostAddress() 返回IP地址的字符串
--------getHostName() 返回计算机的主机名
public class test {
public static void main(String[] args) throws UnknownHostException {
//获取本机的IP地址对象
InetAddress address = InetAddress.getLocalHost();
System.out.println("IP地址:"+address.getHostAddress());
System.out.println("主机名:"+address.getHostName());
//根据IP地址的字符串或主机名生成IP地址对象
InetAddress address2 = InetAddress.getByName("SUNSHINE");
System.out.println("IP地址:"+address2.getHostAddress());
System.out.println("主机名:"+address2.getHostName());
InetAddress[] arr = InetAddress.getAllByName("www.baidu.com");//域名
System.out.println(Arrays.toString(arr));//[www.baidu.com/111.13.100.92, www.baidu.com/111.13.100.91]
}
}
2 Socket
2.1 UDP
发送端使用步骤:
- 建立UDP服务
- 准备数据,把数据封装到数据包中发送,发送端的数据包要带套接字
- 调用udp的服务,发送数据
- 关闭资源
public class test {
public static void main(String[] args) throws IOException {
//建立udp的服务
DatagramSocket datagramSocket = new DatagramSocket();
//准备数据,把数据封装到数据包中
String data = "一个udp的例子";
//创建了一个数据包
DatagramPacket packet = new DatagramPacket(data.getBytes(), data.getBytes().length,InetAddress.getLocalHost() , 9090);
//调用udp的服务发送数据包
datagramSocket.send(packet);
//关闭资源 - 释放占用的端口号
datagramSocket.close();
}
}
接收端的使用步骤
- 建立udp的服务
- 准备空的数据包接收数据
- 调用udp的服务接收数据
- 关闭资源
public class test {
public static void main(String[] args) throws IOException {
//建立udp的服务,并且要监听一个端口
DatagramSocket socket = new DatagramSocket(9090);
//准备空的数据包用于存放数据
byte[] buf = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length); // 1024
//调用udp的服务接收数据
socket.receive(datagramPacket); //receive是一个阻塞型的方法,没有接收到数据包之前会一直等待。 数据实际存储到byte数组中
System.out.println("接收端接收到的数据:"+ new String(buf,0,datagramPacket.getLength())); // getLength() 获取数据包存储了几个字节
//关闭资源
socket.close();
}
}
UDP群聊发送端
//群聊发送端
public class test extends Thread {
@Override
public void run() {
try {
//建立udp的服务
DatagramSocket socket = new DatagramSocket();
//准备数据,把数据封装到数据包中发送
BufferedReader keyReader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
DatagramPacket packet = null;
while((line = keyReader.readLine())!=null){
//把数据封装到数据包中,然后发送
packet = new DatagramPacket(line.getBytes(), line.getBytes().length, InetAddress.getByName("192.168.15.255"), 9090);
//把数据发送出去
socket.send(packet);
}
//关闭资源
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
UDP群聊接收端
//群聊接收端
public class test extends Thread {
@Override
public void run() {
try {
//建立udp的服务,要监听一个端口
DatagramSocket socket = new DatagramSocket(9090);
//准备空的数据包存储数据
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
boolean flag = true;
while(flag){
socket.receive(packet);
// packet.getAddress() 获取对方数据包的IP地址对象。
System.out.println(packet.getAddress().getHostAddress()+"说:"+new String(buf,0,packet.getLength()));
Thread.sleep(10);
}
//关闭资源
socket.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
群聊主类
public class control {
public static void main(String[] args) {
ChatReceive chatReceive = new ChatReceive();
chatReceive.start();
ChatSender chatSender = new ChatSender();
chatSender.start();
}
}
2.2 TCP
客户端
tcp客户端使用步骤:
- 建立tcp的客户端服务
- 获取到对应的流对象
- 写出或读取数据
- 关闭资源
//tcp客户端
public class test {
public static void main(String[] args) throws IOException{
//建立tcp的服务
Socket socket = new Socket(InetAddress.getLocalHost(),9090);
//获取到Socket的输出流对象
OutputStream outputStream = socket.getOutputStream();
//利用输出流对象把数据写出
outputStream.write("服务端你好".getBytes());
//获取到输入流对象,读取服务端回送的数据
InputStream inputStream = socket.getInputStream();
byte[] buf = new byte[1024];
int length = inputStream.read(buf);
System.out.println("客户端接收到的数据:"+ new String(buf,0,length));
//关闭资源
socket.close();
}
}
服务端
ServerSocket的使用步骤
- 建立tcp服务端服务
- 接受客户端的连接产生一个Socket
- 获取对应的流对象读取或者写出数据
- 关闭资源
//服务端
public class test {
public static void main(String[] args) throws Exception {
//建立Tcp的服务端,并且监听一个端口
ServerSocket serverSocket = new ServerSocket(9090);
//接受客户端的连接
Socket socket = serverSocket.accept(); //accept() 接受客户端的连接,该方法也是一个阻塞型的方法,没有客户端与其连接时,会一直等待下去
//获取输入流对象,读取客户端发送的内容
InputStream inputStream = socket.getInputStream();
byte[] buf = new byte[1024];
int length = 0;
length = inputStream.read(buf);
System.out.println("服务端接收:"+ new String(buf,0,length));
//获取socket输出流对象,想客户端发送数据
OutputStream outputStream = socket.getOutputStream();
outputStream.write("客户端你好".getBytes());
//关闭资源
serverSocket.close();
}
}
群聊
注意事项:
- 如果使用BuffrerdReader的readline方法一定要加上\r\n才把数据写出
- 使用字符流一定要调用flush方法数据才会写出
TCP群聊客户端
//群聊客户端
public class test {
public static void main(String[] args) throws IOException {
//建立tcp的客户端服务
Socket socket = new Socket(InetAddress.getLocalHost(),9090);
//获取socket的输出流对象。
OutputStreamWriter socketOut = new OutputStreamWriter(socket.getOutputStream());
//获取socket的输入流对象
BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//获取键盘的输入流对象,读取数据
BufferedReader keyReader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
//不断的读取键盘录入的数据,然后把数据写出
while((line = keyReader.readLine())!=null){
socketOut.write(line+"\r\n");
//刷新
socketOut.flush();
//读取服务端回送的数据
line = socketReader.readLine();
System.out.println("服务端回送的数据是:"+line);
}
//关闭资源
socket.close();
}
}
TCP群聊服务端
//群聊服务端
public class test {
public static void main(String[] args) throws IOException {
//建立tcp的服务端
ServerSocket serverSocket = new ServerScsxiaoyao.com ocket(9090);
//接受客户端的连接,产生一个SOcket
Socket socket = serverSocket.accept();
//获取到Socket的输入流对象
BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//获取到Socket输出流对象
OutputStreamWriter socketOut = new OutputStreamWriter(socket.getOutputStream());
//获取键盘的输入流对象
BufferedReader keyReader = new BufferedReader(new InputStreamReader(System.in));
//读取客户端的数据
String line = null;
while((line = socketReader.readLine())!=null){
System.out.println("服务端接收到的数据:"+ line);
System.out.println("请输入回送给客户端的数据:");
line = keyReader.readLine();
socketOut.write(line+"\r\n");
socketOut.flush();
}
//关闭资源
serverSocket.close();
}
}
3 模拟Tomcat服务器
//模拟Tomcat服务器
public class TomcatDemo extends Thread {
Socket socket;
public TomcatDemo(Socket socket){
this.socket = socket;
}
public void run() {
try {
//获取socket的输出流对象
OutputStream outputStream = socket.getOutputStream();
//把数据写到浏览器上
outputStream.write("<html><head><title>sunshine</title></head><body>浏览器你好</body></html>".getBytes());
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
//建立tcp的服务端
ServerSocket serverSocket = new ServerSocket(9090);
//不断的接受客户端的连接
while(true){
Socket socket = serverSocket.accept();
new TomcatDemo(socket).start();
}
}
}
4 文件上传下载
客户端下载
//下载图片的客户端
public class test {
public static void main(String[] args) throws Exception{
//建立tcp的服务
Socket socket = new Socket(InetAddress.getLocalHost(),9090);
//获取socket的输入流对象
InputStream inputStream = socket.getInputStream();
//获取文件的输出流对象
FileOutputStream fileOutputStream = new FileOutputStream("F:\\sunshine.jpg");
//边读边写
byte[] buf = new byte[1024];
int length = 0 ;
while((length = inputStream.read(buf))!=-1){
fileOutputStream.write(buf,0,length);
}
//关闭资源
fileOutputStream.close();
socket.close();
}
}
服务端给多个客户端发送
//服务端给多个客户端发送图片(多线程)
public class ImageServer extends Thread {
Socket socket ;
//使用该集合是用于存储ip地址的。
static HashSet<String> ips = new HashSet<String>();
public ImageServer(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
//获取socket输出流对象
OutputStream outputStream = socket.getOutputStream();
//获取图片的输入流对象
FileInputStream fileInputStream = new FileInputStream("F:\\sunshine.jpg");
//读取图片数据,把数据写出
byte[] buf = new byte[1024];
int length = 0 ;
while((length = fileInputStream.read(buf))!=-1){
outputStream.write(buf,0,length);
}
String ip = socket.getInetAddress().getHostAddress(); // socket.getInetAddress() 获取对方的IP地址
if(ips.add(ip)){
System.out.println( ip+" 成功下载,当前下载的人数是:"+ ips.size());
}
//关闭资源
fileInputStream.close();
socket.close();
}catch (IOException e) {
}
}
public static void main(String[] args) throws IOException {
//建立tcp的服务 ,并且要监听一个端口
ServerSocket serverSocket = new ServerSocket(9090);
while(true){
//接受用户的链接。
Socket socket = serverSocket.accept();
new ImageServer(socket).start();
csxiaoyao.com }
}
}