IP地址
端口
协议
IP地址:是网络中设备的唯一标识
IP地址分为两大类
IPv4:是给每个连接在网络上的主机分配一个32bit地址。 按照TCP/IP规定,IP地址用二进制来表示, 每个P地址长32bit,也就是4个字节。例如一个采用I进制形式的IP地址是11000000 101010000000001 01000010" ,这么长的地址,处理起来也太费劲了。为了方便使用,IP地址经常被写成+进制的形式,中间使用符号"分隔不同的字节。于是,. 上面的IP地址可以表示为"192168. 1.66" . IP地址的这种表示法叫做"盼+进制表示法”, 这显然比1和0容易记忆得多
IPv6:由于互联网的蓬勃发展, IP地址的需求量愈来愈大,但是网络地址资源有限,使得IP的分配越发紧张。为了扩大地址空间,通过IPv6重新定义地址空间,采用128位地址长度,每16个字节-组,分成8组十六进制数,这样就解决了网络地址资源数量不够的问题
常用命令:
特殊IP地址127.0.0.1:是回送地址,可以代表本机地址,一般用来测试使用
为了方便我们对IP地址的获取和操作, Java提供了一个类InetAddress供我们使用
InetAddress:此类表示Internet协议(IP) 地址
导包:import java.net.InetAddress
方法名 | 说明 |
---|---|
static InetAddress getByName(String host) | 确定主机名称的IP地址。 主机名称可以是机器名称,也可以是IP地址 |
String getHostName() | 获取此IP地址的主机名 |
String getHostAddress() | 返回文本显示中的IP地址字符串 |
package test;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Demo {
public static void main(String[] args) throws UnknownHostException {
//1,static InetAddress getByName(String host) 确定主机名称的IP地址。 主机名称可以是机器名称,也可以是IP地址
// InetAddress address = InetAddress.getByName("YM");
InetAddress address = InetAddress.getByName("192.168.128.1"); //推荐使用
System.out.println(address); //YM/192.168.128.1
//2,String getHostName() 获取此IP地址的主机名
String hostName = address.getHostName();
System.out.println(hostName); //YM
//3,String getHostAddress() 返回文本显示中的IP地址字符串
String hostAddress = address.getHostAddress();
System.out.println(hostAddress); //192.168.128.1
}
}
UDP协议
TCP协议
传输控制协议 (Transmission Control Protocol)
TCP协议是面向连接的通信协议,即传输数据之前,在发送端和接收端建立逻辑连接,然后再传输数据,它提供了两台计算机之间可靠无差错的数据传输。在TCP连接中必须要明确客户端与服务器端,由客户端向服务端发出连接请求,每次连接的创建都需要经过“三次握手"
三次握手:TCP协议中,在发送数据的准备阶段,客户端与服务器之间的三次交互,以保证连接的可靠
完成三次握手,连接建立后,客户端和服务器就可以开始进行数据传输了。由于这种面向连接的特性,TCP协议可以保证传输数据的安全,所以应用十分广泛。例如上传文件、下载文件、 浏览网页等
UDP协议是一种不可靠的网络协议,它在通信的两端各建立一个Socket对象, 但是这两个Socket只是发送,接收数据的对象因此对于基于UDP协议的通信双方而言,没有所谓的客户端和服务器的概念
Java提供了DatagramSocket类作为基于UDP协议的Socket
DatagramSocket()
DatogramPacket (byte[] buf, int Length, InetAddress address, int port)
void send(DatagramPacket p)
void close()
package test;
import java.io.IOException;
import java.net.*;
public class Demo {
public static void main(String[] args) throws IOException {
//1、创建发送端的Socket对象(DatagramSocket)
DatagramSocket ds = new DatagramSocket();
//2、创建数据, 并把数据打包
//DatogramPacket (byte[] buf, int Length, InetAddress address, int port)
// 构造一个数据包,发送长度为length的数据 包到指定主机上的指定端口号。
byte[] bys = "hello,java".getBytes();
DatagramPacket dp = new DatagramPacket(bys,bys.length, InetAddress.getByName("127.0.0.1"),10086);
//3、调用DatagramSocket对象的方法发送数据
//void send(DatagramPacket p)
ds.send(dp);
//4、关闭发送端
ds.close();
}
}
DatagramSocket(int port)
DatagramPacket(byte[] buf, int length)
void receive(DatagramPacketp)
byte[] getData()
、int getLength()
void close()
package test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
//1、创建接收端的Socket对象(DatagramSocket):DatagramSocket(int port)
DatagramSocket ds = new DatagramSocket(10086);
//2、创建一个数据包, 用于接收数据
//DatagramPacket(byte[] buf, int length):构造一个DatagramPacket用来接收长度为length的数据包
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
//3、调用DatagramSocket对象的方法接收数据:void receive(DatagramPacketp)
ds.receive(dp);
//4、解析数据包, 并把数据在控制台显示:
// byte[] getData():返回数据缓冲区 int getLength()
byte[] datas = dp.getData();
//int getlength():返回要发送的数据长度或接收的数据长度
int len = dp.getlength();
String dataString = new String(datas,0,len);
System.out.println("数据是:"+dataString);
//5、关闭接收端:void close()
ds.close();
}
}
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class SendDemo {
public static void main(String[] args) throws IOException {
//1、创建发送端的Socket对象(DatagramSocket):DatagramSocket()
DatagramSocket ds = new DatagramSocket();
//键盘录入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = br.readLine()) != null) {
if ("886".equals(line)) {
break;
} else {
//创建数据并把数据打包
byte[] bys = line.getBytes();
DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("127.0.0.1"), 1001);
//3、调用DatagramSocket对象的方法发送数据:void send(DatagramPacket p)
ds.send(dp);
}
}
ds.close();
}
}
package test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
//创建接收端的Socket对象(DatagramSocket):DatagramSocket(int port)
DatagramSocket ds = new DatagramSocket(1001);
while(true) {
//创建一个数据包接收数据
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
//调用DatagramSocket对象的方法接收数据:void receive(DatagramPacketp)
ds.receive(dp);
//解析数据包, 并把数据在控制台显示:byte[] getData()、int getLength()
System.out.println("数据是:" + new String(dp.getData(), 0, dp.getLength()));
}
//关闭
// ds.close();
}
}
Socket(String host, int port)
OutputStream对象的getOutputStream()方法
void close()
package test;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//1、创建客户端的Socket对象(Socket):Socket(String host, int port)
Socket s = new Socket("127.0.0.1",1000);
//2、获取输出流,写数据:OutputStream对象的getOutputStream()方法
OutputStream os = s.getOutputStream();
os.write("hello,java".getBytes());
//3、释放资源:void close()
s.close();
}
}
接收数据的步骤
ServerSocket(int port)
Socket accept()
InputStream对象的getInputStream()方法
void close()
范例
package test;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SeverDemo {
public static void main(String[] args) throws IOException {
//1、创建服务 器端的Socket对象(ServerSocket):ServerSocket(int port)
ServerSocket ss = new ServerSocket(1000);
//2、监听客户端连接, 返回个Socket对象:Socket accept()
Socket s = ss.accept();
//3、获取输入流, 读数据,并把数据显示在控制台:InputStream对象的getInputStream()方法
InputStream is = s.getInputStream();
byte[] bys = new byte[1014];
int len = is.read(bys);
String data = new String(bys,0,len);
System.out.println(data);
//4、释放资源:void close()
s.close();
ss.close();
}
}
package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//1、创建客户端的Socket对象(Socket):Socket(String host, int port)
Socket s = new Socket("127.0.0.1",1000);
//2、获取输出流,写数据:OutputStream对象的getOutputStream()方法
OutputStream os = s.getOutputStream();
os.write("hello,java".getBytes());
//接收服务器反馈
InputStream is = s.getInputStream();
byte[] bys = new byte[1024];
int len = is.read(bys);
String data = new String(bys,0,len);
System.out.println("客户端:"+data);
//3、释放资源:void close()
s.close();
//客户端:数据已经收到
}
}
package test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SeverDemo {
public static void main(String[] args) throws IOException {
//1、创建服务 器端的Socket对象(ServerSocket):ServerSocket(int port)
ServerSocket ss = new ServerSocket(1000);
//2、监听客户端连接, 返回个Socket对象:Socket accept()
Socket s = ss.accept();
//3、获取输入流, 读数据,并把数据显示在控制台:InputStream对象的getInputStream()方法
InputStream is = s.getInputStream();
byte[] bys = new byte[1014];
int len = is.read(bys);
String data = new String(bys,0,len);
System.out.println("服务器:"+data);
//给出反馈
OutputStream os = s.getOutputStream();
os.write("数据已经收到".getBytes());
//4、释放资源:void close()
s.close();
ss.close();
//服务器:hello,java
}
}
package test;
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket对象(Socket):Socket(String host, int port)
Socket s = new Socket("127.0.0.1",1000);
//数据来源于键盘录入,直到886停止
System.out.println("请输入数据");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//封装输出流对象
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while((line= br.readLine())!=null) {
if("886".equals(line)) {
break;
}
//获取输出流对象
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
s.close();
// hello
// 886
}
}
package test;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class SeverDemo {
public static void main(String[] args) throws IOException {
//1、创建服务 器端的Socket对象(ServerSocket):ServerSocket(int port)
ServerSocket ss = new ServerSocket(1000);
//2、监听客户端连接, 返回个Socket对象:Socket accept()
Socket s = ss.accept();
//3、获取输入流, 读数据,并把数据显示在控制台:InputStream对象的getInputStream()方法
// InputStream is = s.getInputStream();
// InputStreamReader isr = new InputStreamReader(is);
// BufferedReader br = new BufferedReader(isr);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
while((line=br.readLine())!=null) {
System.out.println(line);
}
//4、释放资源:void close()
s.close();
//hello
}
}
package test;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class SeverDemo {
public static void main(String[] args) throws IOException {
//1、创建服务 器端的Socket对象(ServerSocket):ServerSocket(int port)
ServerSocket ss = new ServerSocket(1000);
//2、监听客户端连接, 返回个Socket对象:Socket accept()
Socket s = ss.accept();
//3、获取输入流, 读数据,并把数据显示在控制台:InputStream对象的getInputStream()方法
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//把数据写入文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\test\\java.txt"));
String line;
while((line=br.readLine())!=null) {
bw.write(line);
bw.newLine();
bw.flush();
}
//4、释放资源:void close()
bw.close();
ss.close();
//文件中数据:hello
}
}
package test;
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket对象(Socket):Socket(String host, int port)
Socket s = new Socket("127.0.0.1",1000);
//封装文本文件数据
BufferedReader br =new BufferedReader(new FileReader("E:\\test\\fos.txt"));
//封装输出流对象
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while((line= br.readLine())!=null) {
//获取输出流对象
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
br.close();
s.close();
}
}
package test;
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket对象(Socket):Socket(String host, int port)
Socket s = new Socket("127.0.0.1",1000);
//封装文本文件数据
BufferedReader br =new BufferedReader(new FileReader("E:\\test\\fos.txt"));
//封装输出流对象
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while((line= br.readLine())!=null) {
//获取输出流对象
bw.write(line);
bw.newLine();
bw.flush();
}
// //自定义结束标记
// bw.write("886");
// bw.newLine();
// bw.flush();
s.shutdownOutput();//Socket类中方法:表示输出结束
//接收反馈
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
String data = brClient.readLine(); //等待读取数据
System.out.println("服务器的反馈:"+data); //服务器的反馈:文件上传成功
//释放资源
br.close();
s.close();
}
}
package test;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class SeverDemo {
public static void main(String[] args) throws IOException {
//1、创建服务 器端的Socket对象(ServerSocket):ServerSocket(int port)
ServerSocket ss = new ServerSocket(1000);
//2、监听客户端连接, 返回个Socket对象:Socket accept()
Socket s = ss.accept();
//3、获取输入流, 读数据,并把数据显示在控制台:InputStream对象的getInputStream()方法
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//把数据写入文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\test\\java.txt"));
String line;
while((line=br.readLine())!=null) { //等待读取数据
// if("886".equals(line)) {
// break;
// }
bw.write(line);
bw.newLine();
bw.flush();
}
//给出反馈
BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件上传成功");
bwServer.newLine();
bwServer.flush();
//4、释放资源:void close()
bw.close();
ss.close();
}
}
package test;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class SeverDemo {
public static void main(String[] args) throws IOException {
//创建服务 器端的Socket对象(ServerSocket):ServerSocket(int port)
ServerSocket ss = new ServerSocket(1000);
while(true) {
//监听客户端连接, 返回个Socket对象:Socket accept()
Socket s = ss.accept();
//为每一个客户端开启一个线程
new Thread(new ServerThread(s)).start();
}
}
}
package test;
import java.io.*;
import java.net.Socket;
public class ServerThread implements Runnable {
private Socket s;
public ServerThread(Socket s) {
this.s = s;
}
@Override
public void run() {
try {
//接收数据写入文本文件
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
// BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\test\\java.txt"));
//解决名称冲突问题,上传到不同文件中
int count = 0;
File file = new File("E:\\test\\["+count+"]java.txt");
while(file.exists()) {
count++;
file = new File("E:\\test\\["+count+"]java.txt");
}
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
String line;
while ((line=br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
//给出反馈
BufferedWriter bwSever =new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwSever.write("文件上传成功");
bwSever.newLine();
bwSever.flush();
//释放资源
s.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}