1.达到网络编程的步骤
-
确定网络上的一台主机。端口,定位到这个计算机上的某个资源。
-
找到了这个主机,如何传输数据. Tcp/IP C/S
2.网络通信要素
-
如何准备定位网络上一台或者多台主机
-
找到主机后通信。
-
网络编程要素:IP和端口号。
-
网络通信写协议。udp dcp
-
万物皆对象。
3.IP
ip地址:InterAddress
-
唯一定位一台网络上计算机。
-
本机localhost:12.0.0.1。
-
IP地址的分类。(1.ip地址分类IPV4 / IPV6)IPV4:127.0.0.1,4个字节组成。0-255.42亿。IPV6: fe80::1a56:8bc3:54e2:28b1%10 128位,8个无符号整数!
-
公网(互联网)—私网(局域网)。
-
192.168.xx专门给死人使用的。
-
-
域名:
-
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestIntAddress {
public static void main(String[] args) {
InetAddress inetAddress1 = null;
try {
// 查询本机地址
inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress3 = InetAddress.getByName("localhost");
InetAddress inetAddress4= InetAddress.getLocalHost();
System.out.println(inetAddress3);
System.out.println(inetAddress4);
// 查询网络地址
InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2);
System.out.println(inetAddress2.getCanonicalHostName()); //规范的名字
System.out.println(inetAddress2.getHostAddress()); //ip地址
System.out.println(inetAddress2.getHostName()); // 域名或者自己的名字
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
}
4.端口
端口表示计算机一个程序的进程。
不同进程有不同端口号!用来区分软件。端口数:TCP/UDP : 65535*2
端口分类
-
公用端口:0---1023。http:80, https:443, ftp:21, telent:23
-
程序注册端口:1024--49151
-
Tocat默认端口:8080
-
MYSql:3306
-
Oracle:1521
-
动态、私有49152---65535
netstat -ano #查看电脑所有端口
netstat -ano findstr "5900" #查看指定端口
tasklist/findstr "8696" #查看指定端口进程InetSocketAddress socketAddress= new InetSocketAddress("127.0..0.1",8080);
InetSocketAddress socketAddress2= new InetSocketAddress("localhost",8080);
System.out.println(socketAddress);
System.out.println(socketAddress2);
System.out.println(socketAddress.getAddress());
System.out.println(socketAddress.getHostName());
System.out.println(socketAddress.getPort());
5.通信协议
协议:约定。
网络通信协议:速率,传输码率。代码接口,传输控制。。。分层。
TCP/IP协议簇:实际上是一组协议
TCP:用户传输协议
UDP:用户数据报协议
IP:网络互联协议
TCP和UDP对比
TCP:打电话(连接,稳定。三次握手,四次分手。客户端、服务端没有明确界限。传输完成,释放连接)
UDP:发短信(不连接,不稳定。客户端、服务端,没有明确界限。)
6.TCP
客户端
-
连接服务器Socket
-
发送消息
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
// 客户端
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
// 1.知道服务器地址
InetAddress serveIp = InetAddress.getByName("127.0.0.1");
int port = 9999;
// 2.创建一个socket连接
socket=new Socket(serveIp,port);
// 3.发送消息 IO级
os = socket.getOutputStream();
os.write("你好,欢迎学习".getBytes());
} catch (UnknownHostException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if(os!= null){
try {
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
服务器
-
建立服务的端口 ServerSocket
-
等待用户连接 accept
-
接受用户消息
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServeDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is =null;
ByteArrayOutputStream baos =null;
try {
// 1.需要有一个地址
serverSocket=new ServerSocket(9999);
// 2.等待客户端连接
socket = serverSocket.accept();
// 读取客户端消息
is = socket.getInputStream();
//管道流
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len= is.read(buffer)) != -1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
// 关闭资源
if(baos!=null){
try {
baos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
7.TCP文件上传
服务器端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServeDemo02 {
public static void main(String[] args) throws IOException {
// 1.创建服务
ServerSocket serverSocket = new ServerSocket(9000);
// 2.监听客户端连接
Socket socket = serverSocket.accept(); //阻断式监听,一直等待客户端连接
// 3.获取输入流
InputStream is = socket.getInputStream();
// 4.文件输出
FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!= -1){
fos.write(buffer,0,len);
}
// 通知客户端接收完毕了
OutputStream os = socket.getOutputStream();
os.write("我接受完毕了可以断开".getBytes());
//关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
客户端
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TestClientDemo02 {
public static void main(String[] args) {
try {
// 1.创建一个socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
// 2.创建一个输出流
OutputStream os = socket.getOutputStream();
// 3。文件流
FileInputStream fis= new FileInputStream(new File("312.jpg"));
// 4.写出文件
byte[] buffer = new byte[1024];
int len;
while ((len=fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
//通知服务器,我已经结束
socket.shutdownOutput();
// 确定服务器接受完毕,才能断开连接
InputStream inputStream = socket.getInputStream();
// String byte【】
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[2014];
int len2;
while ((len2=inputStream.read(buffer2))!= -1){
baos.write(buffer2,0,len2);
}
// 5.关闭查询
fis.close();
os.close();
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}