网络编程
1.网络编程中有两个主要的问题
1.如何准确地定位网络上一台或多台主机;定位主机上的特定的应用
2.找到主机后如何可靠高效地进行数据传输
2.网络通信要素概述
要素一: IP 和端口号
/*
IP:
1.IP:唯一的标识Internet 上的计算机(通信实体)
2.在Java中使用InetAddress类代表IP
3.IP分类:IPv4和IPv6 ;万维网和局域网
4.域名:www.baidu.com www . mi.com
5.本地回路地址: 127.0.0.1 对应着: Localhost
6.如何实例化InetAddress:
两个方法: getByName(String host) 、 getLocalHost()
两个常用方法: getHostName() / getHostAddress()
7.端口号:正在计算机上运行的进程。要求:不同的进程有不同的端口号
范围:被规定为一个16位的整数0~65535。
8.端口号与IP地址的组合得出一个网络套接字:Socket
*/
//如何实例化InetAddress:
public class OneTest {
public static void main(String[] args) {
try {
InetAddress name = InetAddress.getByName ("192.168.0.1");
System.out.println (name);
InetAddress name1 = InetAddress.getByName ("www.baidu.com");
System.out.println (name1);
//获取本机的
InetAddress localHost = InetAddress.getLocalHost ();
System.out.println (localHost);
System.out.println ("********************");
//两个方法:
String hostName = name1.getHostName ();
System.out.println (hostName);
String hostAddress = name1.getHostAddress ();
System.out.println (hostAddress);
} catch (UnknownHostException e) {
e.printStackTrace ();
}
}
}
要素二: 网络协议
三次握手举例:
1.我是张三
2.我知道你是张三,我是李四
3.我知道你知道我是张三,你是李四,我是张三
TCP四次挥手
3. TCP网络编程
①:客户端发送信息给服务端,服务端将数据显示控制台上
//例题一:客户端发送信息给服务端,服务端将数据显示控制台上
//客户端
@Test
public void test() {
Socket socket = null;
OutputStream ops = null;
try {
//1.建立Socket对象,指明服务器端的ip和端口号
InetAddress ia= InetAddress.getByName ("127.0.0.1");
socket = new Socket (ia,8899);
//2.获取一个输出流,用于输出数据
ops = socket.getOutputStream ();
//3.写出数据的操作
ops.write ("你好,我是霸王花".getBytes ());
} catch (IOException e) {
e.printStackTrace ();
} finally {
//4.资源的关闭
if(socket!=null){
try {
socket.close ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
}
}if(ops!=null){
try {
ops.close ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
}
}
}
}
//服务端
@Test
public void test1() {
Socket socket = null;
InputStream ips = null;
ByteArrayOutputStream baos= null;
ServerSocket serverSocket=null;
try {
//1.创造服务器端的ServerSocket,指明自己的端口号
serverSocket = new ServerSocket (8899);
//2.调用accept()表示接收来自客户端的servlet
socket = serverSocket.accept ();
//3.获取输入流
ips = socket.getInputStream ();
//4.读取输入流的数据
baos = new ByteArrayOutputStream ();
byte[] buffer=new byte[5];
int len;
while ((len=ips.read (buffer))!=-1){
baos.write (buffer,0,len);
}
System.out.print (baos.toString ());
} catch (IOException e) {
e.printStackTrace ();
} finally {
//5.资源关闭
if(ips!=null){
try {
ips.close ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
}
}if (baos!=null){
try {
baos.close ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
}
}if(socket!=null){
try {
socket.close ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
}
}if(serverSocket!=null){
try {
serverSocket.close ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
}
}
}
}
②:客户端发送文件给服务端,服务端将文件保存在本地。
//例题2:客户端发送文件给服务端,服务端将文件保存在本地。
//客户端
@Test
public void test() throws IOException {
InetAddress ia= InetAddress.getByName ("127.0.0.1");
Socket socket = new Socket (ia, 9090);
OutputStream ops = socket.getOutputStream ();
FileInputStream fis = new FileInputStream ("MVC+Dao.png");
byte[] buffer=new byte[5];
int len;
while ((len=fis.read (buffer))!=-1){
ops.write (buffer,0,len);
}
fis.close ();
ops.close ();
socket.close ();
}
//服务端
@Test
public void test2() throws IOException {
ServerSocket serverSocket = new ServerSocket (9090);
Socket socket = serverSocket.accept ();
InputStream is = socket.getInputStream ();
FileOutputStream fos = new FileOutputStream ("MVC+Dao999.png");
byte[] buffer=new byte[5];
int len;
while ((len=is.read (buffer))!=-1){
fos.write (buffer,0,len);
}
fos.close ();
socket.close ();
is.close ();
serverSocket.close ();
}
③:在2的基础上,服务器给予客户端反馈并在客户端进行输出
//客户端
@Test
public void test() throws IOException {
InetAddress ia= InetAddress.getByName ("127.0.0.1");
Socket socket = new Socket (ia, 9090);
OutputStream ops = socket.getOutputStream ();
FileInputStream fis = new FileInputStream ("MVC+Dao.png");
byte[] buffer=new byte[5];
int len;
while ((len=fis.read (buffer))!=-1){
ops.write (buffer,0,len);
}
//read()方法会阻塞,所以这里要添加一个通知已经传完数据的方法
socket.shutdownOutput ();
//接受来自服务端的信号
ByteArrayOutputStream baos2=new ByteArrayOutputStream ();
InputStream is = socket.getInputStream ();
byte[] buffer2=new byte[5];
int len2;
while ((len2=is.read (buffer2))!=-1){
baos2.write (buffer2,0,len2);
}
System.out.println (baos2.toString ());
baos2.close ();
is.close ();
fis.close ();
ops.close ();
socket.close ();
}
//服务端
@Test
public void test2() throws IOException {
ServerSocket serverSocket = new ServerSocket (9090);
Socket socket = serverSocket.accept ();
InputStream is = socket.getInputStream ();
FileOutputStream fos = new FileOutputStream ("MVC+Dao222.png");
byte[] buffer=new byte[5];
int len;
while ((len=is.read (buffer))!=-1){
fos.write (buffer,0,len);
}
System.out.println ("图片传输完成。");
//反馈给客户端的话
OutputStream os = socket.getOutputStream ();
os.write ("照片已经收到了,谢谢".getBytes ());
os.close ();
fos.close ();
socket.close ();
is.close ();
serverSocket.close ();
}
4. UDP 网络编程
举例:
public class UDPTest {
//客户端
@Test
public void test1() throws IOException {
DatagramSocket socket=new DatagramSocket ();
String str="你好,我是UDP的测试";
byte[] bytes = str.getBytes ();
InetAddress ia=InetAddress.getByName ("127.0.0.1");
DatagramPacket packet=new DatagramPacket (date,0,date.length,ia,9090);
socket.send (packet);
socket.close ();
}
//服务端
@Test
public void test2() throws IOException {
DatagramSocket socket=new DatagramSocket (9090);
byte[] buffer=new byte[500];
DatagramPacket packet=new DatagramPacket (buffer,0,buffer.length);
socket.receive (packet);
System.out.println (new String (packet.getData (),0,packet.getLength ()));
socket.close ();
}
}
5. URL编程
概述:
/*
URL网络编程
1.URL:统一资源定位符,对应着互联网的某一资源地址
2.格式:
http://Localhost:8080/examples/beauty.jpg?username=Tom
协议 主机名 端口号 资源地址 参数列表
*/
例一:
public class URLTest {
public static void main(String[] args) {
try {
URL url=new URL ("http://localhost:8080/login.jsp");
System.out.println (url.getProtocol ());//协议名
System.out.println (url.getHost ());//主机号
System.out.println (url.getPort ());//端口号
System.out.println (url.getFile ());//文件
System.out.println (url.getPath ());//路径
System.out.println (url.getQuery ());//查询名
} catch (MalformedURLException e) {
e.printStackTrace ();
}
}
}
例二:下载网络资源
public class URLTest {
public static void main(String[] args) throws IOException {
URL url = new URL ("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1524047774,2439907370&fm=26&gp=0.jpg");
//获取服务器连接
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection ();
urlConnection.connect ();
InputStream is = urlConnection.getInputStream ();
FileOutputStream fos = new FileOutputStream ("picture.png");
BufferedOutputStream bos = new BufferedOutputStream (fos);
byte[] buffer = new byte[10];
int len;
while ((len = is.read (buffer)) != -1) {
bos.write (buffer, 0, len);
}
//关闭资源
bos.close ();
is.close ();
urlConnection.disconnect ();
}
}
fos = new FileOutputStream (“picture.png”);
BufferedOutputStream bos = new BufferedOutputStream (fos);
byte[] buffer = new byte[10];
int len;
while ((len = is.read (buffer)) != -1) {
bos.write (buffer, 0, len);
}
//关闭资源
bos.close ();
is.close ();
urlConnection.disconnect ();
}
}