import org.junit.Test;
import java.net.InetAddress;
import java.net.UnknownHostException;
/*
一:网络编程中有两个主要的问题
1.如何准确的定位网络上一台或多台主机,定位主机上特定的应用
2.找到主机后如何可靠的高效的进行数据传输
二。网络编程中的两个要素
1.对应问题一:ip和端口号
2.对应问题二:提供网络通信协议:TCP/IP参考模型(应用层,传输层,网络层,物理+数据链路层)
三。通信要素一:IP和端口号
1.IP:唯一标识Internet上的计算机(通信实体)
2.在Java中使用InetAddress类代表IP
3.IP分类:IPV4/IPV6,万维网和局域网
4.域名:www.baidu.com
5.本地回路地址:127.0.0.1对应着localhost
6.如何实例化InetAddress:俩个方法:getbyname(String host),getLocalhost()
两个常用方法:getHostname()/getHostAddress()
7.端口号:正在计算机上运行的进程
要求:不同的进程有不同的端口号
范围:被规定为一个16位的整数0~65535
8.端口号与IP地址的组合得出一个网络套接字:Socket
*/
public class InetAddressTest {
@Test
public void test1() throws UnknownHostException {
InetAddress byName = InetAddress.getByName("127.0.0.1");
System.out.println(byName);
//获取本机地址
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
InetAddress byName1 = InetAddress.getByName("www.baidu.com");
System.out.println(byName1);
//
System.out.println(localHost.getHostName());
//
System.out.println(localHost.getAddress());
}
}
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/*
实现TCP的网络编程
例一:客户端发送信息给服务端,服务端将信息显示在控制台上
*/
public class TcpTest1 {
@Test
//客户端
public void client() throws IOException {
//1.创建Socket对象,来指明ip地址和端口号
InetAddress inet = InetAddress.getByName("127.0.0.1");
Socket socket=new Socket(inet,8899);
//2.获取一个输出流,用于输出数据
OutputStream outputStream = socket.getOutputStream();
//3.写出数据的操作
outputStream.write("你好我是客户端的mm".getBytes());
//4.资源的关闭
outputStream.close();
socket.close();
}
@Test
//服务端
public void server() throws IOException {
//1.创建服务器端的Serversocket,指明自己的端口号
ServerSocket ss=new ServerSocket(8899);
//2.调用accept()表示接收来自于客户端的socket
Socket socket = ss.accept();
//3.获取输入流
InputStream is = socket.getInputStream();
//4.读取输入流中的数据
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len;
byte[] bytes=new byte[5];
while ((len=is.read(bytes))!=-1){
baos.write(bytes,0,len);
}
System.out.println(baos.toString());
//5.关闭资源
baos.close();
is.close();
socket.close();
ss.close();
}
}
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/*
实现TCP的网络编程
例题2:客户端发送文件给服务端,服务端将文件保存在本地
*/
public class TCPTest2 {
/*
这里设计的异常应该使用try-catch-finally处理
*/
@Test
public void client() throws IOException {
//1.创建socket对象,来指明ip地址和端口号
Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),9090);
//2.获取一个输出流用来输出数据
OutputStream os = socket.getOutputStream();
//3.把文件读进来
FileInputStream fis=new FileInputStream(new File("xueshengzheng.jpg"));
//4.读写的操作
int len;
byte[] bytes=new byte[1024];
while ((len=fis.read(bytes))!=-1){
os.write(bytes,0,len);
}
//5.资源关闭
fis.close();
os.close();
socket.close();
}
@Test
public void server() throws IOException {
//1.创建服务端serverSocket,指明自己的端口号
ServerSocket serverSocket = new ServerSocket(9090);
//2.调用accept(),表示接收客户端的socket。
Socket socket = serverSocket.accept();
//3.获取输入流
InputStream inputStream = socket.getInputStream();
//4.保存文件地址为xueshengzheng1.jpg
FileOutputStream fileOutputStream = new FileOutputStream(new File("xueshengzheng1.jpg"));
//5.读写的操作
int len ;
byte[] bytes=new byte[1024];
while ((len=inputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
System.out.println("保存成功");
//6.资源的关闭
fileOutputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/*
实现TCP的网络编程
例题3:从客户端发送文件给服务端,服务端保存本地。并返回”发送成功“给客户端。并关闭相应的连接
*/
public class TCPTest3 {
@Test
public void client() throws IOException {
//1.创建socket对象,指明ip和端口号
Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),9090);
//2.获取一个输出流来输出数据
OutputStream os = socket.getOutputStream();
//3.把文件读进来
FileInputStream file=new FileInputStream(new File("xueshengzheng.jpg"));
//4.读写的操作
int len;
byte[] bytes=new byte[1024];
while ((len=file.read(bytes))!=-1){
os.write(bytes,0,len);
}
socket.shutdownOutput();//关闭数据的输出
//5.接收来自于服务器端的数据,并显示在控制台上
InputStream is = socket.getInputStream();
//读取输入流中的数据
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len1;
byte[] bytes1=new byte[1024];
while ((len=is.read(bytes))!=-1){
baos.write(bytes,0,len);
}
System.out.println(baos.toString());
//6..资源关闭
file.close();
os.close();
socket.close();
}
@Test
public void server() throws IOException {
//1.创建服务端serversocket,指明自己的端口号
ServerSocket ss=new ServerSocket(9090);
//2.调用accept(),表示接收客户端的socket。
Socket socket = ss.accept();
//3.获取输入流
InputStream is = socket.getInputStream();
//4.保存文件地址为xueshengzheng2.jpg
FileOutputStream fos=new FileOutputStream(new File("xueshengzheng2.jpg"));
//5.读写操作
int len;
byte[] bytes=new byte[1024];
while ((len=is.read(bytes))!=-1){
fos.write(bytes,0,len);
}
System.out.println("图片传输完成");
//6.服务器端给客户端反馈
OutputStream os = socket.getOutputStream();
os.write("你好,照片已经收到,谢谢".getBytes());
//7.资源关闭
fos.close();
is.close();
socket.close();
ss.close();
}
}
import java.net.MalformedURLException;
import java.net.URL;
/*
URL网络编程
1.URL:统一资源定位符,对应着互联网的某一资源地址
2.格式:
http://localhost:8080/xueshengzheng.jpg?username=Tom
协议 主机名 端口号 资源地址 参数列表
*/
public class URLTest {
public static void main(String[] args) throws MalformedURLException {
URL url=new URL("http://localhost:8080/xueshengzheng.jpg");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.connect();
InputStream is = urlConnection.getInputStream();
FileOutputStream file=new FileOutputStream("xueshengzheng.jpg");
byte[] bytes=new byte[1024];
int len;
while ((len=is.read(bytes))!=-1){
file.write(bytes,0,len);
}
file.close();
is.close();
urlConnection.disconnect();
//public String getProtocol()获取该URL的协议名
//public String getHost()获取该URL的主机名
//public String getPort()获取该URL的端口号
//public String getPath()获取该URL的路径
//public String getFile()获取该URL的文件名
//public String getQuery()获取该URL的查询名
}
}