网络编程
目录
一、网络编程概述
二、网络通信要素概述
三、通信要素1:ip和端口号
四、通信要素2:网络协议
五、TCP网络编程
六、UDP网络编程
七、URL编程
一、网络编程概述
网络编程中有两个主要问题
- 1、如何准确地定位网络上一台或多台主机,定位主机上的特定的应用
- 2、找到主机后如何可靠高效地进行数据传输
二、网络通信要素概述
三、通信要素1:ip和端口号
四、通信要素2:网络协议
五、TCP网络编程
练习
练习1:客户端发送内容给服务端,服务端将内容打印到控制台上。
public class TCPTest {
//实现TCP网络编程
//例子1:客户端发送信息给服务端, 服务端将数据显示在控制台上
//客户端
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
try {
//1、创建InetAddress类的对象,获取IP地址
InetAddress inet = InetAddress.getByName("127.0.0.1");
//2、实例化Scoket类的对象——>指明IP地址和端口,形成套接字
socket = new Socket(inet,8899);
//3、实例化OutputStream接口,获取一个输出流,用于输出数据
os = socket.getOutputStream();
//4、在流中写入数据
os.write("你好,我是客户端".getBytes());//注意要转换为字节
} catch (IOException e) {
e.printStackTrace();
} finally {
//5、注意:关闭流资源和 释放Socket类的线路(断开与客户端的连接)
try {
if(os != null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//服务端
@Test
public void server(){
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1、实例化ServerSocket类的对象,指明自己的端口号
//不用指明自己的IP(自己的IP自己知道)
serverSocket = new ServerSocket(8899);
//2、调用accept()方法,监听连接请求,如果客户端请求连接,则接受连接,返回通信套接字
socket = serverSocket.accept();
//3、获取输入流
is = socket.getInputStream();
//不建议这么写,可能出现乱码
// byte[] byteBuffer = new byte[20];
// int len;
// while((len = is.read(byteBuffer)) != -1){
// String str = new String(byteBuffer,0,len);
// System.out.print(str);
// }
//4、读取输入的数据
baos = new ByteArrayOutputStream();
byte[] byteBuffer = new byte[5];
int len;
while((len = is.read(byteBuffer)) != -1){
baos.write(byteBuffer,0,len);
}
System.out.println(baos.toString());
System.out.println("收到了来自于:"+socket.getInetAddress().getHostAddress()+"的信息");
} catch (IOException e) {
e.printStackTrace();
} finally {
//5、关闭资源
try {
if(baos != null)
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(serverSocket != null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
练习2:客户端发送文件给服务端, 服务端将文件保存在本地。
public class TCPExer1 {
//客户端
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
try {
//1、获取文件
File file = new File("idea.jpg");
fis = new FileInputStream(file);
//2、获取IP地址
InetAddress inet = InetAddress.getByName("127.0.0.1");
//3、指明IP地址和端口
socket = new Socket(inet,9090);
//4、获取输出流
os = socket.getOutputStream();
byte[] byteBuffer = new byte[1024];
int len;
//5、把文件的字节码写入byteBUffer数组中
while((len = fis.read(byteBuffer)) != -1){
//6、把数组中的字节码写入输出流中
os.write(byteBuffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os != null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//服务端
@Test
public void server(){
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
FileOutputStream fos= null;
try {
serverSocket = new ServerSocket(9090);
socket = serverSocket.accept();
is = socket.getInputStream();
fos = new FileOutputStream(new File("idea5.jpg"));
byte[] byteBuffer = new byte[20];
int len;
while ((len = is.read(byteBuffer)) != -1) {
fos.write(byteBuffer,0,len);
}
}catch (IOException e) {
e.printStackTrace();
} finally {
//5、关闭资源
try {
if(is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(serverSocket != null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
练习3:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。
public class TCPExer1 {
//客户端
@Test
public void client() throws IOException {
//3、指明IP地址和端口
Socket socket = new Socket("127.0.0.1",9090);
//4、获取输出流
OutputStream os = socket.getOutputStream();
//1、获取文件
FileInputStream fis = new FileInputStream(new File("idea.jpg"));
byte[] byteBuffer = new byte[1024];
int len;
//5、把文件的字节码写入byteBUffer数组中
while((len = fis.read(byteBuffer)) != -1){
//6、把数组中的字节码写入输出流中
os.write(byteBuffer,0,len);
}
//7、shutdownInput();显式地告诉read(),数据已经收到
socket.shutdownOutput();
//7、接收服务端的反馈,并显示到控制台上
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] byteBuffer2 = new byte[10];
int len2;
while((len2 = is.read(byteBuffer2)) != -1){
baos.write(byteBuffer2,0,len2);
}
System.out.println(baos.toString());
os.close();
socket.close();
fis.close();
baos.close();
}
//服务端
@Test
public void server() throws IOException {
ServerSocket serverSocket = new ServerSocket(9090);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("idea6.jpg"));
byte[] byteBuffer = new byte[1024];
int len;
while ((len = is.read(byteBuffer)) != -1) {
fos.write(byteBuffer,0,len);
}
//5、给予客户端反馈
OutputStream os = socket.getOutputStream();
os.write("照片已收到".getBytes());
//6、关闭资源
is.close();
socket.close();
serverSocket.close();
fos.close();
os.close();
}
}
练习4:客户端给服务端发送文本, 服务端会将文本转成大写在返回给客户端。
六、UDP网络编程
发送端
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
byte[] by = "hello,atguigu.com".getBytes();
DatagramPacket dp = new DatagramPacket(by, 0, by.length,
InetAddress.getByName("127.0.0.1"), 10000);
ds.send(dp);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ds != null)
ds.close();
}
接收端
DatagramSocket ds = null;
try {
ds = new DatagramSocket(10000);
byte[] by = new byte[1024];
DatagramPacket dp = new DatagramPacket(by, by.length);
ds.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println(str + "--" + dp.getAddress());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ds != null)
ds.close();
}
七、URL编程
1、说明:统一资源定位符,对应着互联网的某一资源地址
2、格式
http://localhost:9090/examples/idea.jpg?username=sakk&ffjjf=123
协议 主机名 端口号 资源地址 参数列表
参数列表格式:参数1名=参数1值&参数2名=参数2值