ip地址:inetAddress
唯一定位一台网络上的计算机
127.0.0.1:本机 localhost
ip地址的分类
ipv4 四个字节组成 0-255
ipv6 128位 。八个无符号整数!
2001:1ab2:cccc:0000:0015:1e2d:1314:22bb
公网(互联网) 私网(局域网)
public class Ip {
public static void main(String[] args) {
try {
//查询本机地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress3 = InetAddress.getByName("localhost");
System.out.println(inetAddress3);
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
//百度ip地址
InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
端口表示计算机上的一个程序的进程
不同的进程又不同的端口号!用来区分软件
被规定0-65535
TCP / UDP :65535 * 2 TCP:80 UDP:80 单个协议下端口号不能冲突,不同协议端口号可以相同
端口号分类
公有端口0-1023
程序注册端口:1024-49151 分配和用户和程序
动态端口、私有端口:49152-65535
netstat -ano #查看所有的端口
netstat -ano|findstr "" #查看指定的端口
tasklist|findstr "" #查看指定端口的进程
协议:约定,就好比我现在说的普通话才能交流
网络通信协议:速率、传输码率、代码结构、传输控制
问题:非常的复杂?
大事化小:分层!
TCP/IP协议簇:实际上时一组协议
TCP UDP对比
TCP:打电话
UDP:发短信
客户端
//客户端
public class TcpClientDemo {
public static void main(String[] args) {
Socket socket = null;
OutputStream outputStream = null;
try {
//1.知道服务器的地址
InetAddress serverIp = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2.创建一个socket连接
socket = new Socket(serverIp,port);
//3.发送消息 IO流
outputStream = socket.getOutputStream();
outputStream.write("nihao".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务器
//服务端
public class TcpServerDemo {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
//1.有一个地址
serverSocket = new ServerSocket(9999);
//2.等待客户端连接进来
accept = serverSocket.accept();
//3.读取客户端的消息
inputStream = accept.getInputStream();
//管道流
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len=inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer,0,len);
}
System.out.println(byteArrayOutputStream.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (byteArrayOutputStream != null)
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(accept != null)
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (serverSocket != null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务器端
public class TCPServer{
public static void main(String[] args){
ServerSocket serverSocket = null;
Socket accept = null;//阻塞式监听,会一直等待客户端连接
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
//创建服务
serverSocket = new ServerSocket(9090);
//监听客户端的连接
accept = serverSocket.accept();
//获取输入流
inputStream = accept.getInputStream();
//文件输出
fileOutputStream = new FileOutputStream(new File("基础语法\\receive.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1){
fileOutputStream.write(buffer,0,len);
}
//通知客户端我接受完毕了
OutputStream outputStream = accept.getOutputStream();
outputStream.write("我接受完毕了,可以断开了".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
try {
if (fileOutputStream != null)
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (accept != null)
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (serverSocket != null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端
public class TcpClient{
public static void main(String[] args) {
Socket socket = null;
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
//建立Socket连接
socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
//创建一个输出流
outputStream = socket.getOutputStream();
//文件流
//读取文件
fileInputStream = new FileInputStream(new File("基础语法\\沙滩.jpg"));
//写出文件
byte[] buffer = new byte[1024];
int len;
while ((len = fileInputStream.read(buffer)) != -1){
outputStream.write(buffer,0,len);
}
//通知服务器,我已经结束了
socket.shutdownOutput();//我已经传输完了!
//确定服务器接受完毕,才能断开连接
inputStream = socket.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer2= new byte[1024];
int len2;
while ((len2 = inputStream.read(buffer2)) != -1){
byteArrayOutputStream.write(buffer2,0,len2);
}
System.out.println(byteArrayOutputStream.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
try {
if (byteArrayOutputStream!=null)
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (inputStream!=null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileInputStream!=null)
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (outputStream!=null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务端
客户端
发短信:不用链接 需要知道对方的地址
发送消息
发送端
//不需要连接服务器
public class Demo {
public static void main(String[] args) throws Exception {
//建立一个Socket
DatagramSocket datagramSocket = new DatagramSocket();
//建个包
String msg = "nihao";
//发送给谁
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9999;
//数据 数据的长度起始 发送给谁
DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
//发送包
datagramSocket.send(datagramPacket);
}
}
接收端
等待客户端的连接
public class Demo01 {
public static void main(String[] args) throws Exception {
//开放端口
DatagramSocket datagramSocket = new DatagramSocket(9999);
//接收数据 接受包
byte[] buffer = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(buffer, 0, buffer.length);
//阻塞接收
datagramSocket.receive(datagramPacket);
System.out.println(datagramPacket.getAddress().getHostAddress());
System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));
//关闭连接
datagramSocket.close();
}
循环发送消息
public class Sender01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
//准备数据 控制台读取 System.in
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true){
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));
socket.send(packet);
if (data.equals("bye")){
break;
}
}
socket.close();
}
}
循环接收消息
public class Receicer01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while (true){
//准备接受包裹
byte[] contain= new byte[1024];
DatagramPacket packet = new DatagramPacket(contain,0,contain.length);
socket.receive(packet);//阻塞式接受包裹
//断开连接 bye
byte[] data = packet.getData();
String datas = new String(data,0,data.length);
System.out.println(datas);
if (datas.equals("bye")){
break;
}
}
socket.close();
}
}
发送线程
public class TalkSend implements Runnable {
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIp;
private int toPort;
public TalkSend(int fromPort, String toIp, int toPort) {
this.fromPort = fromPort;
this.toIp = toIp;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIp, this.toPort));
socket.send(packet);
if (data.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
接收线程
public class TalkReceive implements Runnable {
DatagramSocket socket = null;
private int port;
private String msgFrom;
public TalkReceive(int port,String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
//准备接受包裹
byte[] contain= new byte[1024];
DatagramPacket packet = new DatagramPacket(contain,0,contain.length);
socket.receive(packet);//阻塞式接受包裹
//断开连接 bye
byte[] data = packet.getData();
String datas = new String(data,0,data.length);
System.out.println(msgFrom+":"+datas);
if (datas.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
学生端
public class TalkStudent {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
老师端
public class TalkTeacher {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
统一资源定位符:定位互联网上的某一个资源
协议://ip地址:端口/项目名/资源
public class Url {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=qiu&password=123");
System.out.println(url.getProtocol()); //协议
System.out.println(url.getHost()); //主机 ip
System.out.println(url.getPort()); //端口
System.out.println(url.getPath()); //文件
System.out.println(url.getFile()); //全路径
System.out.println(url.getQuery()); //参数
}
}
下载网络资源
public class Url {
public static void main(String[] args) throws Exception {
//下载地址
URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=qiu&password=123");
//连接到这个资源 HTTP
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("基础语法\\abc.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1){
fileOutputStream.write(buffer,0,len);
}
fileOutputStream.close();
inputStream.close();
urlConnection.disconnect();
}
}
原文:https://www.cnblogs.com/qiudajiang/p/13376882.html