网络编程
1: 测试IP
1 package Net; 2 3 import java.net.InetAddress; 4 import java.net.UnknownHostException; 5 6 /** 7 * author liulei 8 * data 5.26 9 * since 1.8 10 * version 1.0 11 * Description 测试IP 12 */ 13 public class Test1 { 14 public static void main(String[] args) throws UnknownHostException { 15 //查询本机地址 16 InetAddress inetAddress = InetAddress.getByName("127.0.0.1"); 17 System.out.println(inetAddress);///127.0.0.1 18 InetAddress localhost = InetAddress.getByName("localhost"); 19 System.out.println(localhost);//localhost/127.0.0.1 20 InetAddress localHost1 = InetAddress.getLocalHost(); 21 System.out.println(localHost1);//DESKTOP-PAIHBK9/169.254.192.87 22 //查询网络ip地址 23 InetAddress byName = InetAddress.getByName("www.baidu.com"); 24 System.out.println(byName);//域名+ip 25 //常用方法 26 System.out.println(byName.getHostName());//域名 27 System.out.println(byName.getHostAddress());//ip 28 System.out.println(byName.getCanonicalHostName());//规范的名字 29 System.out.println(byName.getAddress());//[B@7f31245a 30 System.out.println(byName.getClass());//inetadress对象 31 } 32 }
2:测试端口
1 package Net; 2 3 import java.net.InetSocketAddress; 4 5 /** 6 * author liulei 7 * data 5.26 8 * since 1.8 9 * version 1.0 10 * Description 测试端口 11 */ 12 public class Test2 { 13 public static void main(String[] args) { 14 InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1",8080); 15 InetSocketAddress s = new InetSocketAddress("localhost",8080); 16 System.out.println(inetSocketAddress); 17 System.out.println(s); 18 System.out.println(inetSocketAddress.getAddress()); 19 System.out.println(inetSocketAddress.getHostName());//ip 20 System.out.println(inetSocketAddress.getPort());//端口 21 22 23 } 24 }
3:TCP
使用TCP进行发送消息
1 package Net; 2 3 import java.io.IOException; 4 import java.io.OutputStream; 5 import java.net.InetAddress; 6 import java.net.InetSocketAddress; 7 import java.net.Socket; 8 9 /** 10 * author liulei 11 * data 5.26 12 * since 1.8 13 * version 1.0 14 * Description 客户端 15 */ 16 public class Test31 { 17 public static void main(String[] args) throws IOException { 18 //1要知道服务器端的端口号和ip 19 InetAddress serverIp = InetAddress.getByName("127.0.0.1"); 20 int port = 9999; 21 //2创建一个socket连接 22 Socket socket = new Socket(serverIp,port); 23 OutputStream outputStream = socket.getOutputStream(); 24 //发送消息IO流 25 outputStream.write("你好,欢迎学习狂神说java".getBytes()); 26 outputStream.close(); 27 socket.close(); 28 29 } 30 }
1 package Net; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.net.ServerSocket; 7 import java.net.Socket; 8 9 /** 10 * author liulei 11 * data 5.26 12 * since 1.8 13 * version 1.0 14 * Description 服务端 15 */ 16 public class Test32 { 17 public static void main(String[] args) throws IOException { 18 ServerSocket serverSocket = null; 19 Socket socket = null; 20 InputStream inputStream = null; 21 ByteArrayOutputStream baos = null; 22 23 try { 24 //先有一个地址 25 serverSocket = new ServerSocket(9999); 26 while (true){ 27 //等待客户端连过来 28 socket = serverSocket.accept(); 29 //读取客户端的信息 30 inputStream = socket.getInputStream(); 31 byte[] buffer = new byte[1024]; 32 baos = new ByteArrayOutputStream();//管道流方法 33 int len; 34 while((len = inputStream.read(buffer))!=-1){ 35 //这种方法可能因为乱码出现错误 36 //String str=new String(buffer,0,len); 37 //System.out.println(str); 38 baos.write(buffer,0,len);//使用管道流方法接受信息 39 40 } 41 System.out.println(baos.toString()); 42 } 43 }catch (Exception e){ 44 e.printStackTrace(); 45 } 46 finally { 47 if(baos != null){ 48 baos.close(); 49 } 50 if(inputStream != null){ 51 inputStream.close(); 52 } 53 if(socket != null){ 54 socket.close(); 55 } 56 if(serverSocket != null){ 57 serverSocket.close(); 58 } 59 } 60 } 61 }
使用TCP进行文件的发送
1 package Net; 2 3 import java.io.*; 4 import java.net.InetAddress; 5 import java.net.Socket; 6 7 /** 8 * author liulei 9 * data 10 * since 1.8 11 * version 1.0 12 * Description TCp客户端进行发送图片 13 */ 14 public class Test41 { 15 public static void main(String[] args) throws Exception{ 16 //创建一个socket连接 17 Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9999); 18 //创建一个输出流 19 OutputStream os = socket.getOutputStream(); 20 //读取文件 21 FileInputStream fis = new FileInputStream(new File("a.png")); 22 byte[] buffer = new byte[1024]; 23 int len; 24 while ((len = fis.read(buffer))!=-1){ 25 os.write(buffer,0,len); 26 } 27 socket.shutdownOutput();//通知服务器我已经发送完毕,不需要继续监听堵塞了 28 //确定服务器接受完毕才可以断开连接 29 InputStream stream = socket.getInputStream(); 30 byte[] buffer1 = new byte[1024]; 31 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 32 int len1; 33 while ((len1 = stream.read(buffer1))!=-1){ 34 baos.write(buffer1,0,len1); 35 } 36 System.out.println(baos.toString()); 37 //关闭资源 38 baos.close(); 39 fis.close(); 40 os.close(); 41 socket.close(); 42 } 43 }
1 package Net; 2 3 import java.io.*; 4 import java.net.ServerSocket; 5 import java.net.Socket; 6 7 /** 8 * author liulei 9 * data 10 * since 1.8 11 * version 1.0 12 * Description tcp文件上传服务端 13 */ 14 public class Test42 { 15 public static void main(String[] args) throws IOException { 16 //先指明自己的端口号 17 ServerSocket serverSocket = new ServerSocket(9999); 18 Socket accept = serverSocket.accept();//获取一个socket对象 19 InputStream inputStream = accept.getInputStream();//输入流对象 20 FileOutputStream fos = new FileOutputStream(new File("b.jpg"));//文件输出流,把文件输出到本地 21 byte[] buffer = new byte[1024]; 22 int len; 23 while ((len = inputStream.read(buffer))!=-1){ 24 fos.write(buffer,0,len); 25 } 26 //通知客户端接受完毕 27 OutputStream os = accept.getOutputStream(); 28 os.write("我读到了你的信息,你可以断开了".getBytes()); 29 //关闭资源 30 os.close(); 31 fos.close(); 32 inputStream.close(); 33 serverSocket.close(); 34 } 35 }
4:UDP
1 package Net; 2 3 import java.io.IOException; 4 import java.net.*; 5 6 /** 7 * author liulei 8 * data 5.27 9 * since 1.8 10 * version 1.0 11 * Description udp客户端(简单udp通信的例子) 12 */ 13 public class Test51 {//不需要连接服务器 14 public static void main(String[] args) throws IOException { 15 //建立一个socket 16 DatagramSocket socket = new DatagramSocket(8888); 17 //建个包 18 String msg = "111你好啊11111"; 19 InetAddress localhost = InetAddress.getByName("localhost"); 20 int port = 9090; 21 //数据,数据的长度,数据发给谁 22 DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.length(),localhost,port); 23 //发送包, 24 socket.send(packet); 25 //关闭流 26 socket.close(); 27 } 28 }
1 package Net; 2 3 4 5 import java.net.DatagramPacket; 6 import java.net.DatagramSocket; 7 8 /** 9 * author liulei 10 * data 5.27 11 * since 1.8 12 * version 1.0 13 * Description udp服务器端(简单udp通信的例子) 14 */ 15 public class Test52 { 16 public static void main(String[] args) throws Exception{ 17 //开放端口 18 DatagramSocket socket = new DatagramSocket(9090); 19 //接受数据包 20 byte[] buffer = new byte[1024]; 21 DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length); 22 socket.receive(packet);//堵塞接受 23 System.out.println(packet.getAddress().getHostAddress()); 24 System.out.println(new String(packet.getData(),0,packet.getLength())); 25 socket.close();//关闭连接 26 } 27 }
5: 使用UDP进行单向通信
1 package Net; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 import java.net.DatagramPacket; 6 import java.net.DatagramSocket; 7 import java.net.InetSocketAddress; 8 import java.net.SocketException; 9 10 /** 11 * author liulei 12 * data 5.27 13 * since 1.8 14 * version 1.0 15 * Description 基于udp的单方向聊天 16 */ 17 public class Test61 { 18 public static void main(String[] args) throws Exception { 19 DatagramSocket socket = new DatagramSocket(8888); 20 while (true){ 21 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 22 String data = br.readLine(); 23 byte[] datas = data.getBytes(); 24 DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("127.0.0.1",6666)); 25 socket.send(packet); 26 if(data.equals("bye")){ 27 break; 28 } 29 } 30 socket.close(); 31 } 32 }
1 package Net; 2 3 4 5 import java.io.IOException; 6 import java.net.DatagramPacket; 7 import java.net.DatagramSocket; 8 import java.net.SocketException; 9 10 /** 11 * author liulei 12 * data 5.27 13 * since 1.8 14 * version 1.0 15 * Description 基于udp的单方向聊天 16 */ 17 public class Test62 { 18 public static void main(String[] args) throws IOException { 19 DatagramSocket socket = new DatagramSocket(6666); 20 while (true){ 21 byte[] data = new byte[1024]; 22 DatagramPacket packet = new DatagramPacket(data,0,data.length); 23 socket.receive(packet); 24 System.out.println(new String(packet.getData(),0,data.length)); 25 String stringreceive = new String(packet.getData(),0,data.length); 26 if(stringreceive.equals("bye")){ 27 break; 28 } 29 } 30 socket.close(); 31 32 } 33 }
6: 使用UDP进行双向通信
1 package Net; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedReader; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 import java.net.DatagramPacket; 8 import java.net.DatagramSocket; 9 import java.net.InetSocketAddress; 10 import java.net.SocketException; 11 12 /** 13 * author liulei 14 * data 5.27 15 * since 1.8 16 * version 1.0 17 * Description 发送端 18 */ 19 public class Test71 implements Runnable{ 20 public static void main(String[] args) { 21 22 } 23 DatagramSocket socket = null; 24 BufferedReader br = null; 25 DatagramPacket packet = null; 26 private String toIp; 27 private int toPort; 28 private int fromPort; 29 30 public Test71(int fromPort, int toPort, String toIp) throws Exception { 31 this.toIp = toIp; 32 this.toPort = toPort; 33 this.fromPort = fromPort; 34 socket = new DatagramSocket(fromPort); 35 br = new BufferedReader(new InputStreamReader(System.in)); 36 } 37 38 @Override 39 public void run() { 40 while (true){ 41 String data = null; 42 try { 43 data = br.readLine(); 44 45 byte[] datas = data.getBytes(); 46 packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(toIp,toPort)); 47 socket.send(packet); 48 if(data.equals("bye")){ 49 break; 50 } 51 } catch (IOException e) { 52 e.printStackTrace(); 53 } 54 55 } 56 try { 57 br.close(); 58 socket.close(); 59 } catch (IOException e) { 60 e.printStackTrace(); 61 } 62 } 63 }
1 package Net; 2 3 import java.io.IOException; 4 import java.net.DatagramPacket; 5 import java.net.DatagramSocket; 6 import java.net.SocketException; 7 8 /** 9 * author liulei 10 * data 11 * since 1.8 12 * version 1.0 13 * Description 接收端 14 */ 15 public class Test72 implements Runnable{ 16 DatagramSocket socket = null; 17 private int port; 18 private String msgfrom; 19 20 public Test72(int port, String msgfrom) throws SocketException { 21 this.port = port; 22 this.msgfrom = msgfrom; 23 socket = new DatagramSocket(port); 24 } 25 26 @Override 27 public void run() { 28 29 try { 30 31 while (true){ 32 byte[] data = new byte[1024]; 33 DatagramPacket packet = new DatagramPacket(data,0,data.length); 34 socket.receive(packet); 35 String stringreceive = new String(packet.getData(),0,data.length); 36 System.out.println(msgfrom + ":"+ stringreceive); 37 38 if(stringreceive.equals("bye")){ 39 System.out.println(msgfrom + "断开连接"); 40 break; 41 } 42 43 } 44 socket.close(); 45 } catch (IOException e) { 46 e.printStackTrace(); 47 } 48 49 50 } 51 }
1 package Net; 2 3 /** 4 * author liulei 5 * data 5.27 6 * since 1.8 7 * version 1.0 8 * Description 用户一 9 */ 10 public class Test73 { 11 public static void main(String[] args) throws Exception { 12 new Thread(new Test71(7777,9999,"localhost")).start(); 13 new Thread(new Test72(8888,"用户二")).start(); 14 } 15 }
1 package Net; 2 3 /** 4 * author liulei 5 * data 5.27 6 * since 1.8 7 * version 1.0 8 * Description 用户二 9 */ 10 public class Test74{ 11 public static void main(String[] args) throws Exception { 12 new Thread(new Test71(5555,8888,"localhost")).start(); 13 new Thread(new Test72(9999,"用户一")).start(); 14 } 15 }
7:域名解析
1 package Net; 2 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.net.HttpURLConnection; 7 import java.net.MalformedURLException; 8 import java.net.URL; 9 10 /** 11 * author liulei 12 * data 13 * since 1.8 14 * version 1.0 15 * Description 16 */ 17 public class Test8 { 18 public static void main(String[] args) throws IOException { 19 URL url = new URL("https://www.cnblogs.com/henuliulei/"); 20 System.out.println(url.getPort());//端口 21 System.out.println(url.getHost());//ip 22 System.out.println(url.getProtocol());//协议 23 System.out.println(url.getPath()); 24 System.out.println(url.getFile()); 25 System.out.println("--------------分割线-------"); 26 URL url1 = new URL("https://files.cnblogs.com/files/henuliulei/Java基础阶段笔记.zip"); 27 //连接这个资源 28 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 29 InputStream inputStream = urlConnection.getInputStream(); 30 FileOutputStream fileOutputStream = new FileOutputStream("a.doc"); 31 byte[] buffer = new byte[32768]; 32 int len; 33 while ((len= inputStream.read(buffer))!=-1){ 34 fileOutputStream.write(buffer,0,len);//写出数据 35 } 36 fileOutputStream.close(); 37 inputStream.close(); 38 urlConnection.disconnect(); 39 } 40 }
作者:你的雷哥
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。