代码链接:https://download.csdn.net/download/qq_52354698/86405518?spm=1001.2014.3001.5501
局域网:覆盖范围最小,仅仅覆盖一个教室或一个机房
城域网:覆盖范围较大们可以覆盖一个城市
广域网:覆盖范围最大,可以覆盖全国,甚至全球,万维网是广域网的代表
127.0.0.1 表示本机地址
协议
TCP/IP:传输控制协议、因特网互联协议
是Internet最基本的协议、Internet国际互联网的基础
TCP 协议:传输控制协议
UDP 协议:用户数据协议
package com.qdu.api;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author dell
* @version 1.0
*/
public class API_ {
public static void main(String[] args) throws UnknownHostException {
//获取本机的InetAddress对象
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);//DESKTOP-3I3I0JA/192.168.1.5 前半部分是计算机名,后半部分是IP地址
//根据指定主机名,获取InetAddress对象
InetAddress host1 = InetAddress.getByName("DESKTOP-3I3I0JA");
System.out.println(host1);
//根据域名返回InetAddress对象
InetAddress host2 = InetAddress.getByName("www.baidu.com");
System.out.println(host2);
//通过InetAddress对象获取对应的地址
String hostAddress = host2.getHostAddress();
System.out.println(hostAddress);
//通过InetAddress获取对应的主机名或域名
String hostName = host2.getHostName();
System.out.println(hostName);
}
}
1.编写一个服务器端,和一个客户端
2.服务器端在9999端口监听
3.客户端连接到服务器端,发送"hello, server",然后退出
4.服务器端接收到客户端发送的信息,输出,并退出
客户端
package com.qdu.socket;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author dell
* @version 1.0
*/
public class SocketTCP01Client {
public static void main(String[] args) throws IOException {
//连接服务端(ip,端口)
//连接本机的9999端口,如果连接成返回socket对象
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端socket返回 = " + socket.getClass());
//连接上后,生成socket,通过socket.getOutputStream()
//得到和socket对象关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello,socket".getBytes());
//关闭流对象和socket,必须关闭
outputStream.close();
socket.close();
System.out.println("客户端退出...");
}
}
服务端
package com.qdu.socket;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author dell
* @version 1.0
*/
public class SocketTCP01Server {
public static void main(String[] args) throws IOException {
//在本机的9999端口监听,等待连接
//要求本机没有其他服务在监听9999端口
//ServerSocket可以通过accept()返回多个Socket[多个客户端连接服务器的并发]
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端,在9999端口监听,等待连接...");
//当没有客户端连接9999端口,程序会堵塞,等待连接
//如果有客户端连接,则会返回socket对象,程序继续
Socket socket = serverSocket.accept();
System.out.println("服务端socket = " + socket.getClass());
//通过socket.getInputStream()读取客户端写入到数据通道的数据
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int readLen = 0;
while ((readLen = inputStream.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, readLen));
}
inputStream.close();
socket.close();
serverSocket.close();
System.out.println("服务端关闭...");
}
}
客户端和服务端都要有一个socket
1.编写个服务端,和一个客户端
2.服务器端在9999端口监听
3.客户端连接到服务端,发送"hello, server"并接收服务器端回发的"hello,client",再退出
4.服务器端接收到客户端发送的信息,输出,并发送"hello, client", 再退出
服务端
package com.qdu.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author dell
* @version 1.0
*/
public class SocketTCP01Server {
public static void main(String[] args) throws IOException {
//在本机的9999端口监听,等待连接
//要求本机没有其他服务在监听9999端口
//ServerSocket可以通过accept()返回多个Socket[多个客户端连接服务器的并发]
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端,在9999端口监听,等待连接...");
//当没有客户端连接9999端口,程序会堵塞,等待连接
//如果有客户端连接,则会返回socket对象,程序继续
Socket socket = serverSocket.accept();
System.out.println("服务端socket = " + socket.getClass());
//通过socket.getInputStream()读取客户端写入到数据通道的数据
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int readLen = 0;
while ((readLen = inputStream.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, readLen));
}
//获取socket相关联的输出流
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello, client".getBytes());
//设置结束标记
socket.shutdownOutput();
inputStream.close();
outputStream.close();
socket.close();
serverSocket.close();
System.out.println("服务端关闭...");
}
}
客户端
package com.qdu.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author dell
* @version 1.0
*/
public class SocketTCP01Client {
public static void main(String[] args) throws IOException {
//连接服务端(ip,端口)
//连接本机的9999端口,如果连接成返回socket对象
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端socket返回 = " + socket.getClass());
//连接上后,生成socket,通过socket.getOutputStream()
//得到和socket对象关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello,server".getBytes());
socket.shutdownOutput();
//获取和socket相关联的输入流,读取数据并显示
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int readLen = 0;
while ((readLen = inputStream.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, readLen));
}
//结束标记
socket.shutdownInput();
//关闭流对象和socket,必须关闭
outputStream.close();
inputStream.close();
socket.close();
System.out.println("客户端退出...");
}
}
当连个socket服务端和客户端互相发送多条信息时,socket并不能直接确定上条消息何时发送结束,因此在上条消息发送结束后,一定要添加结束标记(socket.shutdownInput();),来告诉socket我将要发送第二条消息了
1.编写一个服务端,和一个客户端
2.服务端在9999端口监听
3.客户端连接到服务端,发送"hello, server",并接收服务端回发的"hello,client",再退出
4.服务端接收到客户端发送的信息,输出,并发送"hello, client", 再退出
服务端
package com.qdu.socket;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author dell
* @version 1.0
*/
public class SocketTCP01Server {
public static void main(String[] args) throws IOException {
//在本机的9999端口监听,等待连接
//要求本机没有其他服务在监听9999端口
//ServerSocket可以通过accept()返回多个Socket[多个客户端连接服务器的并发]
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务端,在9999端口监听,等待连接...");
//当没有客户端连接9999端口,程序会堵塞,等待连接
//如果有客户端连接,则会返回socket对象,程序继续
Socket socket = serverSocket.accept();
System.out.println("服务端socket = " + socket.getClass());
//通过socket.getInputStream()读取客户端写入到数据通道的数据
InputStream inputStream = socket.getInputStream();
//IO读取,使用字符流
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
System.out.println(s);
//获取socket相关联的输出流
OutputStream outputStream = socket.getOutputStream();
//使用字节输出流的方式回复信息
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello,client 字符流");
bufferedWriter.newLine();//插入一个换行符,表示回复的内容结束
bufferedWriter.flush();//手动刷新
bufferedReader.close();
bufferedWriter.close();
socket.close();
serverSocket.close();
System.out.println("服务端关闭...");
}
}
客户端
package com.qdu.socket;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author dell
* @version 1.0
*/
public class SocketTCP01Client {
public static void main(String[] args) throws IOException {
//连接服务端(ip,端口)
//连接本机的9999端口,如果连接成返回socket对象
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端socket返回 = " + socket.getClass());
//连接上后,生成socket,通过socket.getOutputStream()
//得到和socket对象关联的输出流对象
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello,server 字符流");
bufferedWriter.newLine();//插入一个换行符,表示写入内容结束,要求对方一定要使用readLine()!!!
//如果使用字符流,一定要手动刷新,否则数据不会写入数据通道
bufferedWriter.flush();
//获取和socket相关联的输入流,读取数据并显示
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
System.out.println(s);
//关闭流对象和socket,必须关闭
bufferedWriter.close();
bufferedReader.close();
socket.close();
System.out.println("客户端退出...");
}
}
使用字节流,就不需要使用socket的结束标记了,写入一个换行符即可
1.编写一个服务端,和一个客户端
2.服务器端在8888端口监听
3.客户端连接到服务端,发送一张图片e:lqie.png
4.服务器端接收到客户端发送的图片,保存到src下,发送"收到图片”再退出
5.客户端接收到服务端发送的"收到图片",再退出
6.该程序要求使用StreamUtils.java,我们直接使用
客户端
package com.qdu.upload;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @author dell
* @version 1.0
*/
public class TCPFileUploadClient {
public static void main(String[] args) throws Exception {
//1。客户端连接服务端
Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
//2.创建读取磁盘文件的输入流
String filePath = "e:\\1.jpg";
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath));
//3.bytes就是filePath对应的字节数组
byte[] bytes = StreamUtils.streamToByteArray(bufferedInputStream);
//4.通过socket获取输出流将bytes发送给服务端
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
bufferedOutputStream.write(bytes);//将文件对应的字节数组内容写数据通道
bufferedInputStream.close();
socket.shutdownOutput();//写入数据的结束标记
//5.关闭相关的流
bufferedOutputStream.close();
socket.close();
}
}
服务端
package com.qdu.upload;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author dell
* @version 1.0
*/
public class TCPFileUploadServer {
public static void main(String[] args) throws Exception {
//1.服务端在本机监听8888端口
ServerSocket serverSocket = new ServerSocket(8888);
//2.等待连接
Socket socket = serverSocket.accept();
//3.读取客户端发送的数据
//通过socket得到输入流
BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream());
byte[] bytes = StreamUtils.streamToByteArray(bufferedInputStream);
//4.将得到的bytes数组,写入到指定的路径,就得到一个文件了
String destPath = "src\\2.jpg";
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destPath));
bufferedOutputStream.close();
//5.关闭其他资源
bufferedInputStream.close();
socket.close();
serverSocket.close();
}
}
netstat -an
可以查看当前主机网络情况,包括端口监听情况和网络连接情况netstat -an | more
可以分页显示Listening:表示某个端口在监听
如果有一个外部程序(客户端)连接到该端口,就会显示一条连接信息
可以输入Ctrl + c 退出命令