package 网络编程入门;
/*
为了方便我们对IP地址的获取和操作,Java提供了一个类InetAddress供我们使用
InetAddress:此类表示Internet协议(IP)地址
方法名 说明
static InetAddress getByName(String host)确定主机名称的IP地址。主机名称可以是机器名称,也可以是IP地址
String getHostName() 获取此IP地址的主机名
String getHostAddress() 返回文本显示中的IP地址字符串
*/
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressDemo {
public static void main(String[] args) throws UnknownHostException {
InetAddress ar = InetAddress.getByName("192.168.1.66");
String name = ar.getHostName();
String IP = ar.getHostAddress();
System.out.println(name + " " + IP);
}
}
UDP通信程序
UDP发送数据
package 网络编程入门.UDP通信程序;
/*
步骤:
1,创建发送端Socket对象(DatagramSocket)
DatagramSocket()
2,创建数据,并打包
DatagramPacket(byte[] buf, int length,InetAddress address,int port)
3,调用DatagramSocket对象的方法发送数据
void send(DatagramPacket p)
4,关闭发送端
void close()
*/
import java.io.IOException;
import java.net.*;
public class UDP发送数据 {
public static void main(String[] args) throws IOException {
//创建发送端Socket对象(DatagramSocket)
DatagramSocket ds = new DatagramSocket();
//创建数据,并打包
byte[] bytes ="hello,udp,我来了".getBytes();
int len = bytes.length;
InetAddress address = InetAddress.getByName("192.168.9.107");
int port = 10000;
DatagramPacket dp=new DatagramPacket(bytes,len,address,10000);
//调用DatagramSocket对象的方法发送数据
ds.send(dp);
//关闭发送端
ds.close();
}
}
UDP接收数据
package 网络编程入门.UDP通信程序;
/*
接收数据的步骤
1.创建接收端的Socket对象(DatagramSocket)
DatagramSocket(int port)
2.创建一个数据包,用于接收数据
DatagramPacket(byte[]buf,int length)
3.调用DatagramSocket对象的方法接收数据
void receive(DatagramPacket p)
4 解析数据包,并把数据在控制台显示
byte[] getData()
int getLength()
5.关闭接收端
void close()
*/
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UDP接收数据 {
public static void main(String[] args) throws IOException {
//创建接收端的Socket对象(DatagramSocket)
DatagramSocket ds = new DatagramSocket(10000);
//创建一个数据包,用于接收数据
byte[] bytes = new byte[1024];
int len = bytes.length;
DatagramPacket dp = new DatagramPacket(bytes, len);
//调用DatagramSocket对象的方法接收数据
ds.receive(dp);
//解析数据包,并把数据在控制台显示
System.out.println(new String(dp.getData(),0,dp.getLength()));
//关闭接收端
ds.close();
}
}
练习
package 网络编程入门.UDP通信程序.练习;
/*
UDP发送:
数据来自于键盘录入,直到输入的数据使886,发送数据结束
UDP接收:
因为接收端不知道发送端什么时候停止发送,故采用死循环接收
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class 发送 {
public static void main(String[] args) throws IOException {
DatagramSocket ds=new DatagramSocket();
//键盘录入
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = br.readLine()) != null) {
if("886".equals(line)) {
break;
}
}
byte[] bys=line.getBytes();
DatagramPacket dp=new DatagramPacket(bys,bys.length, InetAddress.getByName("192.168.9.107"),12345);
ds.send(dp);
ds.close();
}
}
package 网络编程入门.UDP通信程序.练习;
import 集合进阶.Collection单列.Set.TreeSet集合概述和特点;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class 接受 {
public static void main(String[] args) throws IOException {
DatagramSocket ds=new DatagramSocket(12345);
while(true) {
byte[] buf=new byte[1024];
int len=buf.length;
DatagramPacket dp=new DatagramPacket(buf, len);
ds.receive(dp);
System.out.println(new String(dp.getData(),0,dp.getLength()));
}
// ds.close();//死循环,不关闭了
}
}
TCP通信程序
TCP发送数据
package 网络编程入门.TCP通信程序;
/*
步骤:
1,创建客户端的Socket对象(Socket)
Socket(String host,int port)
2,获取输出流,写数据
OutputStream getOutputStream()
3,释放资源
void close()
*/
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class TCP发送数据 {
public static void main(String[] args) throws IOException {
//创建客户端的Socket对象(Socket)
Socket s=new Socket("192.168.9.107",10000);
//获取输出流,写数据
OutputStream os=s.getOutputStream();
os.write("hello,tcp,我来了".getBytes());
s.close();
}
}
TCP接收数据
package 网络编程入门.TCP通信程序;
/*
步骤:
1,创建服务器端Socket对象(Server Socket)
ServerSocket(int port)
2,监听客户端连接,返回一个Socke对象
Socket accept()
3,获取输入流,读数据,并显示在控制台
InputStream getInputStream()
4,释放资源
void close()
*/
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCP接收数据 {
public static void main(String[] args) throws IOException {
//创建服务器端Socket对象(Server Socket)
ServerSocket ss = new ServerSocket(10000);
//监听客户端连接,返回一个Socke对象
Socket s = ss.accept();
//获取输入流,读数据,并显示在控制台
InputStream is = s.getInputStream();
byte[] bytes = new byte[1024];
int len = is.read(bytes);
String data = new String(bytes, 0, len);
System.out.println(data);
//释放资源
s.close();
ss.close();
}
}
练习1
package 网络编程入门.TCP通信程序.练习1;
/*
客户端发送数据,接收服务器反馈
服务器接收数据,给出反馈
*/
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s = new Socket("192.168.9.107", 10000);
//获取输出流,写数据
OutputStream os = s.getOutputStream();
os.write("hello,tcp,我来了".getBytes());
//接收服务器反馈
InputStream is = s.getInputStream();
byte[] bytes = new byte[1024];
int len = is.read(bytes);
String data = new String(bytes, 0, len);
System.out.println("客户端:" + data);
//释放资源
s.close();
}
}
package 网络编程入门.TCP通信程序.练习1;
/*
客户端发送数据,接收服务器反馈
服务器接收数据,给出反馈
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器端的Socket对象
ServerSocket ss=new ServerSocket(10000);
//监听客户端连接,返回一个Socket对象
Socket s = ss.accept();
//获取输入流,读数据,并把数据显示在控制台
InputStream is = s.getInputStream();
byte[] bytes = new byte[1024];
int len = is.read(bytes);
String data = new String(bytes, 0, len);
System.out.println("服务器:"+data);
//给出反馈
OutputStream os = s.getOutputStream();
os.write("数据已经收到".getBytes());
//释放资源
ss.close();
}
}
练习2
package 网络编程入门.TCP通信程序.练习2;
/*
客户端:数据来源于键盘输入,直到输入的数据使886,发送数据结束
服务器:接收到的数据在控制台输出
*/
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s=new Socket("192.168.9.107",10000);
//数据来源于键盘输入,直到输入的数据使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;
}
}
bw.write(line);
bw.newLine();
bw.flush();
//释放资源
s.close();
}
}
package 网络编程入门.TCP通信程序.练习2;
/*
客户端:数据来源于键盘输入,直到输入的数据使886,发送数据结束
服务器:接收到的数据在控制台输出
*/
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
//创建服务器端ServerSocket对象
ServerSocket ss=new ServerSocket(10000);
//监听
Socket s = ss.accept();
//获取输入流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
//释放资源
ss.close();
}
}
练习3
package 网络编程入门.TCP通信程序.练习3;
/*
客户端:数据来自于键盘录入,直到输入数据为886,发送数据结束
服务器:接收到的数据写入文本文件
*/
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端Socket对象
Socket s=new Socket("192.168.9.107",10000);
//数据来源于键盘输入,直到输入的数据使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;
}
}
bw.write(line);
bw.newLine();
bw.flush();
//释放资源
s.close();
}
}
package 网络编程入门.TCP通信程序.练习3;
/*
客户端:数据来自于键盘录入,直到输入数据为886,发送数据结束
服务器:接收到的数据写入文本文件
*/
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(10000);
Socket s = ss.accept();
//接收数据
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//把数据写入文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("idea_text\\.s.txt"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
ss.close();
}
}
练习4
package 网络编程入门.TCP通信程序.练习4;
/*
客户端数据来自于文本文件
客户端接收到的数据写入文本文件
*/
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.9.107",10000);
//封装文本数据
BufferedReader br=new BufferedReader(new FileReader("idea_text\\s.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 网络编程入门.TCP通信程序.练习4;
/*
客户端数据来自于文本文件
客户端接收到的数据写入文本文件
*/
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10000);
Socket s = ss.accept();
//接收数据
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//把数据写入文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("idea_text\\s.txt"));
String line;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
ss.close();
}
}
练习5
package 网络编程入门.TCP通信程序.练习5;
/*
客户端:数据来自于文本文件,接收服务器反馈
服务器:接收到的数据写入文本文件,给出反馈
出现问题:程序一直等待
原因:读数据的方法使阻塞式的
解决办法:1自定义结束标记
2.使用 shutdownOutput()方法 (推荐)
*/
import java.io.*;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Socket s = new Socket("192.168.9.107", 10000);
//封装文本数据
BufferedReader br = new BufferedReader(new FileReader("idea_text\\s.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();
*/
//shutdownOutput()方法
s.shutdownOutput();
//接受反馈
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
String data = brClient.readLine();
System.out.println("服务器反馈:" + data);
br.close();
s.close();
}
}
package 网络编程入门.TCP通信程序.练习5;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10000);
Socket s = ss.accept();
//接收数据
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//把数据写入文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("idea_text\\s.txt"));
String line;
while ((line = br.readLine()) != null) {//等待接收数据
bw.write(line);
bw.newLine();
bw.flush();
}
//给出反馈
BufferedWriter bwServer=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件上传成功");
bw.newLine();
bw.flush();
bw.close();
ss.close();
}
}