-
要想实现网络传输,需要考虑哪些问题
- 如何准确地定位到网络上的一台主机
- 如何才能进行可靠高效的传输
-
Java如何实现网络通信
- 使用IP地址定位主机,使用端口号定位主机上的应用 ---> InetAddress类
- InetAddress类对象使用
InetAddress inet = InetAddress.getByName(""127.0.0.1); inet.getLocalHost();//获取本机的InetAddress对象 inet.getHostName();//获取域名 inet.getHostAddress();//获取主机地址
- 如何实现通信 ---> TCP/UDP
public class TestUDP { @Test public void send(){ DatagramSocket ds = null; try { ds = new DatagramSocket(); byte[] b = "data to be send".getBytes(); //创建一个数据报,每个数据报不能大于64k,每个都记录着数据信息+发送端IP及端口号+接收端IP及端口号 DatagramPacket packet = new DatagramPacket(b,0, b.length, InetAddress.getByName("127.0.0.1"), 9090); ds.send(packet); } catch (IOException e) { e.printStackTrace(); }finally{ if (ds != null) ds.close(); } } @Test public void receive(){ DatagramSocket ds = null; try { ds = new DatagramSocket(9090); byte[] b = new byte[1024]; DatagramPacket packet = new DatagramPacket(b, 0, b.length); ds.receive(packet); String str = new String(packet.getData(), 0,packet.getLength() ); System.out.println(str); } catch (IOException e) { e.printStackTrace(); } finally { ds.close(); } } }
//TCP编程例一:客户端给服务端发送信息,服务端输出此信息到控制台上 public class TestTCP1 { //client @Test public void client() { Socket socket = null; OutputStream os = null; try { //1.创建一个Socket的对象,通过构造器指明服务端的IP地址+端口号 socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090); //2. getOutputStream(),发送数据,该方法返回一个OutputStream对象 os = socket.getOutputStream(); //3.具体的输出过程 os.write("I am client".getBytes()); } catch (IOException e) { e.printStackTrace(); }finally { //4.关闭相应的流 if(os != null) try { os.close(); } catch (IOException e) { e.printStackTrace(); } if(socket != null) try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } //server @Test public void server() { ServerSocket ss = null; InputStream is = null; Socket s = null; try { //1. 创建serversocket对象,通过构造器指明自身的端口号 ss = new ServerSocket(9090); //2.调用accept方法返回一个socket对象 s = ss.accept(); //3.调用socket对象的getInputStream方法获取同客户端发送过来的输入流 is = s.getInputStream(); //4 对获取的流进行的具体操作 byte[] b = new byte[20]; int len; while((len = is.read(b)) != -1){ String str = new String(b,0,len); System.out.print(str); } System.out.println("\n receive message from " + s.getInetAddress().getHostAddress()); } catch (IOException e) { e.printStackTrace(); }finally { //5.关闭相应的流、socket、serversocket的对象 if(is != null) try { is.close(); } catch (IOException e) { e.printStackTrace(); } if(s != null) try { s.close(); } catch (IOException e) { e.printStackTrace(); } if(ss != null) try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } } }
- URL编程
public class TestURL { public static void main(String[] args) { InputStream is = null; URL url = null; try { url = new URL("https://www.runoob.com/java/java-collections.html");//类似于File类的初始化 // url.getProtocol();//获取该URL的协议名 // url.getHost();//获取该URL的主机名 // url.getPort();//获取该URL的端口号 // url.getPath();//获取该URL的文件路径 // url.getFile();//获取该URL的文件名 // url.getRef();//获取该URL在文件中的相对位置 // url.getQuery();//获取该URL的查名 //如何将服务端数据读入:openStream() 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.println(str); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); } catch (IOException e) { e.printStackTrace(); } } //如果既有数据输入又有数据输出,考虑使用URLConnection InputStream is1 = null; FileOutputStream fos = null; try { URLConnection urlConnection = url.openConnection(); is1 = urlConnection.getInputStream(); fos = new FileOutputStream(new File("abc.txt")); byte[] b = new byte[20]; int len1; while((len1 = is1.read(b))!=-1){ fos.write(b,0,len1); } } catch (IOException e) { e.printStackTrace(); }finally { try { if(fos != null) fos.close(); if(is1 != null) is1.close(); } catch (IOException e) { e.printStackTrace(); } } } }
网友评论