网络编程:
IP地址:InetAddress
》唯一的标识
端口号:标识正在计算机运行的进程
0~65535其中0~1023被预先定义
端口号与IP地址的组合得出一个网络套接字。
/** * InetAddress:位于java.net包下 * 1.InetAddress用来代表IP地址,一个InetAddress的对象就代表着一个IP地址 * 2.如何创建InetAddress的对象,getByName(String host) * 3.getHostName():获取IP地址对应的域名 * 4.getHostAddress():获取IP地址 */ public class NetADDress { public static void main(String[] args) throws UnknownHostException { //创建一个InetAddress对象,getByName() InetAddress inetAddress = InetAddress.getByName("www.baidu.com");//域名或IP地址 System.out.println(inetAddress); //两个方法 System.out.println(inetAddress.getHostName()); System.out.println(inetAddress.getHostAddress()); //获得本机的IP:getLocalHost() InetAddress inetAddress1 = InetAddress.getLocalHost(); System.out.println(inetAddress1); System.out.println(inetAddress1.getHostName()); System.out.println(inetAddress1.getHostAddress()); } }
TCP和UDP
TCP协议:
》使用TCP协议前,须先建立TCP连接,形成传输数据通道
》传输前,采用“三次握手”方式,是可靠的
》TCP协议进行通信的两个应用进程:客户端、服务器
》传输完毕,须释放已建立的连接,效率低
UDP协议:
》将数据、源、目的封装成数据包,不需要建立连接
》每个数据包的大小限制在64K内
》因无需连接,故是不可靠的
》发送数据结束时无需释放资源,速度快
TCP编程一:客户端给服务器发送消息,服务端输出此信息到控制台上
客户端:
//客户端 @Test public void client(){ Socket socket = null; OutputStream os = null; try { //1.创建一个Socket的对象,通过构造器指明服务器的IP地址,以及其接受的端口号 socket = new Socket(InetAddress.getByName("127.0.0.1"),8888); //2.getOutputStream(),发送数据,方法返回OutputStream的对象 os = socket.getOutputStream(); //3.具体的输出过程 os.write("我是客户端!hello".getBytes()); } catch (IOException e) { e.printStackTrace(); }finally { //4.关闭流和Socket对象 if (os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
服务器:
//服务器 @Test public void server(){ ServerSocket serverSocket = null; Socket so = null; InputStream is = null; try { //1.创建一个ServerSocket的对象,通过构造器指明自身的端口号 serverSocket = new ServerSocket(8888); //2.调用其accept()方法,返回一个Socket的对象 so = serverSocket.accept(); //3.调用Socket对象的getInputStream()获取一个从客户端发送过来的输入流 is = so.getInputStream(); //4.对获取的输入流进行操作 int len; byte[] b = new byte[20]; while((len = is.read(b))!=-1){ String str = new String(b,0,len); System.out.println(str); } System.out.println("收到来自" + so.getInetAddress().getHostAddress() + "的信息"); } catch (IOException e) { e.printStackTrace(); }finally { if(so!=null){ try { so.close(); } catch (IOException e) { e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(serverSocket!=null){ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }
TCP编程二:客户端给服务器发送消息,服务端输出此信息到控制台上,服务器收到信息回复“我已经收到”
客户端:
//客户端 @Test public void client(){ Socket socket = null; OutputStream os = null; InputStream is = null; try { socket = new Socket(InetAddress.getByName("127.0.0.1"),9999); os = socket.getOutputStream(); os.write("我是客户端!".getBytes()); //shutdownOutput():执行此方法,显式的告诉服务器发送完毕 socket.shutdownOutput(); is = socket.getInputStream(); int len; byte[] b = new byte[20]; while((len = is.read(b))!=-1){ String str = new String(b,0,len); System.out.print(str); } } catch (IOException e) { e.printStackTrace(); }finally { if(socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if(os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } }
服务器:
//服务器 @Test public void server(){ ServerSocket serverSocket = null; Socket socket = null; InputStream is = null; OutputStream os = null; try { serverSocket = new ServerSocket(9999); socket = serverSocket.accept(); is = socket.getInputStream(); int len; byte[] b = new byte[20]; while((len = is.read(b))!=-1){ String str = new String(b,0,len); System.out.print(str); } System.out.println("服务器收到来自" + socket.getInetAddress().getHostAddress() + "的信息"); os = socket.getOutputStream(); os.write("服务器收到信息".getBytes()); socket.shutdownOutput(); } catch (IOException e) { e.printStackTrace(); }finally { if (socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if(os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(serverSocket!=null){ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
TCP编程三:从客户端发送文件给服务器,服务器保存到本地,并返回“发送成功”给客户端,并关闭相关的连接
//服务器 @Test public void server(){ ServerSocket serverSocket = null; Socket socket = null; InputStream is = null; FileOutputStream fos = null; OutputStream os = null; try { serverSocket = new ServerSocket(9999); socket = serverSocket.accept(); is = socket.getInputStream(); //创建照片 fos = new FileOutputStream(new File("4.jpg")); int len; byte[] b = new byte[2048]; while((len = is.read(b))!=-1) { fos.write(b,0,len); } //发送信息 os = socket.getOutputStream(); os.write("发送成功".getBytes()); socket.shutdownOutput(); } catch (IOException e) { e.printStackTrace(); }finally { if(fos!=null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } if (is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if (serverSocket!=null){ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
//客户端 @Test public void client(){ Socket socket = null; OutputStream os = null; InputStream is = null; FileInputStream fis = null; try { socket = new Socket(InetAddress.getByName("127.0.0.1"),9999); os = socket.getOutputStream(); is = socket.getInputStream(); //发送照片 fis = new FileInputStream(new File("1.jpg")); int len; byte[] b = new byte[2048]; while((len = fis.read(b)) != -1){ os.write(b,0,len); } socket.shutdownOutput(); //接受服务器的信息 while((len = is.read(b))!=-1){ String str = new String(b,0,len); System.out.println(str); } } catch (IOException e) { e.printStackTrace(); }finally { if(os!=null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(fis!=null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
UDP网络通信
1.DatagramSocket与DatagramPacket
2.建立发送端,接收端
3.建立数据包
4.调用Socket的发送、接受方法
5.关闭Socket
发送端和接收端是两个独立的运行程序。
//发送端 @Test public void send(){ DatagramSocket ds = null; try { ds = new DatagramSocket(); byte[] b = "我是发送端,hi好".getBytes(); DatagramPacket packet = new DatagramPacket(b,b.length,InetAddress.getByName("127.0.0.1"),9090); ds.send(packet); } catch (IOException e) { e.printStackTrace(); } finally { ds.close(); } } //接收端 @Test public void receive(){ DatagramSocket ds = null; try { ds = new DatagramSocket(9090); byte[] b =new byte[30]; DatagramPacket packet = new DatagramPacket(b,0,b.length); ds.receive(packet); String str = new String(b,0,b.length); System.out.println(str); } catch (IOException e) { e.printStackTrace(); } finally { ds.close(); } }
URL:统一资源定位符,一个URL的对象,对应着互联网上一个资源
可以通过URL的对象调用其相应的方法,将此资源读取("下载")
方法:
》public String getProtocol()获取该URL的协议名
》public String getHost()获取该URL的主机名
》public String getPort()获取该URL的端口号
》public String getPath()获取该URL的文件路径
》public String getFile()获取该URL的文件名
》public String getRef()获取该URL在文件中的相对位置
》public String getQuery()获取该URL的查询名
URL url = new URL("http://www.baidu.com") //如何将服务端的资源读取进来:openStream() InputStream is = url.openStream(); byte[] b = new byte[20]; int len; while((len=is,read(b))!-1){ String str = new String(b,0,len); System.out.print(str); } //如果既有数据的输入,又有数据的输出,则考虑使用URLConnection URLConnection urlConn = url.openConnection(); urlConn.getInputStream() urlConn.getOutputStream()