111111 /* InetAddress 此类表示Internet协议(IP)地址 public static InetAddress getByName(String host):确定主机名称的IP地址。主机名称可以是机器名称,也可以是IP地址 public String getHostName():获取此IP地址的主机名 public String getHostAddress():返回文本显示中的IP地址字符串 */
class InetAddressDemos {
public static void main(String[] args) throws UnknownHostException {
//public static InetAddress getByName(String host):确定主机名称的IP地址。主机名称可以是机器名称,也可以是IP地址
// InetAddress address = InetAddress.getByName("itheima");
InetAddress address = InetAddress.getByName("192.168.119.44");
//public String getHostName():获取此IP地址的主机名
String name = address.getHostName();
//public String getHostAddress():返回文本显示中的IP地址字符串
String ip = address.getHostAddress();
System.out.println("主机名:" + name);
System.out.println("IP地址:" + ip);
}
}
/* UDP发送数据的步骤 1:创建发送端的Socket对象(DatagramSocket) 2:创建数据,并把数据打包 3:调用DatagramSocket对象的方法发送数据 4:关闭发送端
public class SendDemo {
public static void main(String[] args) throws IOException {
//创建发送端的Socket对象(DatagramSocket)
// DatagramSocket() 构造数据报套接字并将其绑定到本地主机上的任何可用端口
DatagramSocket ds = new DatagramSocket();
//创建数据,并把数据打包
//DatagramPacket(byte[] buf, int length, InetAddress address, int port)
//构造一个数据包,发送长度为 length的数据包到指定主机上的指定端口号。
byte[] bys = "hello,udp,我来了".getBytes();
// int length = bys.length;
// InetAddress address = InetAddress.getByName("192.168.119.44");
// int port = 10086;
// DatagramPacket dp = new DatagramPacket(bys,length,address,port);
DatagramPacket dp = new DatagramPacket(bys,bys.length,InetAddress.getByName("192.168.119.44"),10086);
//调用DatagramSocket对象的方法发送数据
//void send(DatagramPacket p) 从此套接字发送数据报包
ds.send(dp);
//关闭发送端
//void close() 关闭此数据报套接字
ds.close();
}
}
/* UDP接收数据的步骤 1:创建接收端的Socket对象(DatagramSocket) 2:创建一个数据包,用于接收数据 3:调用DatagramSocket对象的方法接收数据 4:解析数据包,并把数据在控制台显示 5:关闭接收端 */
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
//创建接收端的Socket对象(DatagramSocket)
//DatagramSocket(int port) 构造数据报套接字并将其绑定到本地主机上的指定端口
DatagramSocket ds = new DatagramSocket(10086);
//创建一个数据包,用于接收数据
//DatagramPacket(byte[] buf, int length) 构造一个 DatagramPacket用于接收长度为 length数据包
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys,bys.length);
//调用DatagramSocket对象的方法接收数据
ds.receive(dp);
//解析数据包,并把数据在控制台显示
//byte[] getData() 返回数据缓冲区
// byte[] datas = dp.getData();
//int getLength() 返回要发送的数据的长度或接收到的数据的长度
// int len = dp.getLength();
// String dataString = new String(datas,0,len);
// System.out.println("数据是:" + dataString);
System.out.println("数据是:" + new String(dp.getData(),0,dp.getLength()));
//关闭接收端
ds.close();
}
}
/* UDP发送数据: 数据来自于键盘录入,直到输入的数据是886,发送数据结束 */
public class SendDemo {
public static void main(String[] args) throws IOException {
//创建发送端的Socket对象(DatagramSocket)
DatagramSocket ds = new DatagramSocket();
//自己封装键盘录入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = br.readLine()) != null) {
//输入的数据是886,发送数据结束
if ("886".equals(line)) {
break;
}
//创建数据,并把数据打包
byte[] bys = line.getBytes();
DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("192.168.119.44"), 12345);
//调用DatagramSocket对象的方法发送数据
ds.send(dp);
}
//关闭发送端
ds.close();
}
}
/* UDP接收数据: 因为接收端不知道发送端什么时候停止发送,故采用死循环接收 */
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
//创建接收端的Socket对象(DatagramSocket)
DatagramSocket ds = new DatagramSocket(12345);
while (true) {
//创建一个数据包,用于接收数据
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
//调用DatagramSocket对象的方法接收数据
ds.receive(dp);
//解析数据包,并把数据在控制台显示
System.out.println("数据是:" + new String(dp.getData(), 0, dp.getLength()));
}
//关闭接收端
// ds.close();
}
}
/* TCP发送数据的步骤 1:创建客户端的Socket对象(Socket) 2:获取输出流,写数据 3:释放资源 */
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket对象(Socket)
//Socket(InetAddress address, int port) 创建流套接字并将其连接到指定IP地址的指定端口号
// Socket s = new Socket(InetAddress.getByName("192.168.119.44"),10000);
//Socket(String host, int port) 创建流套接字并将其连接到指定主机上的指定端口号
Socket s = new Socket("192.168.119.44",10001);
//获取输出流,写数据
//OutputStream getOutputStream() 返回此套接字的输出流
OutputStream os = s.getOutputStream();
os.write("hello,tcp,我来了".getBytes());
//释放资源
s.close();
}
}
/* TCP接收数据的步骤 1:创建服务器端的Socket对象(ServerSocket) 2:获取输入流,读数据,并把数据显示在控制台 3:释放资源 */
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器端的Socket对象(ServerSocket)
//ServerSocket(int port) 创建绑定到指定端口的服务器套接字
ServerSocket ss = new ServerSocket(10001);
//Socket accept() 侦听要连接到此套接字并接受它
Socket s = ss.accept();
//获取输入流,读数据,并把数据显示在控制台
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);
//释放资源
s.close();
ss.close();
}
}
/* 客户端:发送数据,接收服务器反馈 */
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket对象(Socket)
Socket s = new Socket("192.168.119.44", 10002);
//获取输出流,写数据
OutputStream os = s.getOutputStream();
os.write("hello,tcp,我来了".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);
//释放资源
// is.close();
// os.close();
s.close();
}
}
/* 服务器:接收数据,给出反馈 */
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器端的Socket对象(ServerSocket)
ServerSocket ss = new ServerSocket(10002);
//监听客户端连接,返回一个Socket对象
Socket s = ss.accept();
//获取输入流,读数据,并把数据显示在控制台
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);
//给出反馈
OutputStream os = s.getOutputStream();
os.write("数据已经收到".getBytes());
//释放资源
// s.close();
ss.close();
}
}
/* 客户端:数据来自于键盘录入,直到输入的数据是886,发送数据结束 */
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("192.168.119.44",10003);
//数据来自于键盘录入,直到输入的数据是886,发送数据结束
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;
}
//获取输出流对象
// OutputStream os = s.getOutputStream();
// os.write(line.getBytes());
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
s.close();
}
}
/* 服务器:接收到的数据在控制台输出 */
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器Socket对象
ServerSocket ss = new ServerSocket(10003);
//监听客户端的连接,返回一个对应的Socket对象
Socket s = ss.accept();
//获取输入流
// 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);
}
//释放资源
ss.close();
}
}
/* 服务器:接收到的数据写入文本文件,给出反馈 */
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器Socket对象
ServerSocket ss = new ServerSocket(10000);
//监听客户端连接,返回一个对应的Socket对象
Socket s = ss.accept();
//接收数据
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//把数据写入文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("myNet\\Copy.java"));
String line;
while ((line=br.readLine())!=null) { //等待读取数据
// if("886".equals(line)) {
// break;
// }
bw.write(line);
bw.newLine();
bw.flush();
}
// System.out.println(11111);
//给出反馈
BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件上传成功");
bwServer.newLine();
bwServer.flush();
//释放资源
bw.close();
ss.close();
}
}
/* 客户端:数据来自于文本文件,接收服务器反馈 */
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("192.168.119.44",10000);
//封装文本文件的数据
BufferedReader br = new BufferedReader(new FileReader("myNet\\InetAddressDemo.java"));
//封装输出流写数据
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line=br.readLine())!=null) {
bw.write(line);
bw.newLine();
bw.flush();
}
// System.out.println(22222);
//自定义结束标记
// bw.write("886");
// bw.newLine();
// bw.flush();
//public void shutdownOutput()
s.shutdownOutput();
//接收反馈
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
String data = brClient.readLine(); //等待读取数据
System.out.println("服务器的反馈:" + data);
//释放资源
br.close();
s.close();
}
}
/* 客户端:数据来自于文本文件,接收服务器反馈 */
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("192.168.119.44",10000);
//封装文本文件的数据
BufferedReader br = new BufferedReader(new FileReader("myNet\\InetAddressDemo.java"));
//封装输出流写数据
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line;
while ((line=br.readLine())!=null) {
bw.write(line);
bw.newLine();
bw.flush();
}
s.shutdownOutput();
//接收反馈
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
String data = brClient.readLine(); //等待读取数据
System.out.println("服务器的反馈:" + data);
//释放资源
br.close();
s.close();
}
}
/* 服务器:接收到的数据写入文本文件,给出反馈,代码用线程进行封装,为每一个客户端开启一个线程 */
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器Socket对象
ServerSocket ss = new ServerSocket(10000);
while (true) {
//监听客户端连接,返回一个对应的Socket对象
Socket s = ss.accept();
//为每一个客户端开启一个线程
new Thread(new ServerThread(s)).start();
}
// ss.close();
}
}