1.TCP(客户端与服务端)
//TCP编程例三:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。 //如下的程序,处理异常时,要使用try-catch-finally!!本例仅为了书写方便~ public class TestTCP3 { @Test public void client()throws Exception{ //1.创建Socket的对象 Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9898);//假设服务端本机 //2.从本地获取一个文件发送给服务端 OutputStream os = socket.getOutputStream(); FileInputStream fis = new FileInputStream(new File("1.jpg")); byte[] b = new byte[1024]; int len; while((len = fis.read(b)) != -1){ os.write(b,0,len); } //shutdownOutput():执行此方法,显式的告诉服务端发送完毕! socket.shutdownOutput(); //3.接收来自于服务端的信息 InputStream is = socket.getInputStream(); byte[] b1 = new byte[1024]; int len1; while((len1 = is.read(b1)) != -1){ String str = new String(b1,0,len1); System.out.print(str); } //4.关闭相应的流和Socket对象 从下面往上面关 is.close(); os.close(); fis.close(); socket.close(); } @Test public void server() throws Exception{ //1.创建一个ServerSocket的对象 ServerSocket ss = new ServerSocket(9898); //2.调用其accept()方法,返回一个Socket的对象 Socket s = ss.accept(); //3.将从客户端发送来的信息保存到本地 InputStream is = s.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("3.jpg")); byte[] b = new byte[1024]; int len; while((len = is.read(b)) != -1){ fos.write(b, 0, len); } System.out.println("收到来自于" + s.getInetAddress().getHostAddress() + "的文件"); //4.发送"接收成功"的信息反馈给客户端 OutputStream os = s.getOutputStream(); os.write("你发送的图片我已接收成功!".getBytes()); //5.关闭相应的流和Socket及ServerSocket的对象 os.close(); fos.close(); is.close(); s.close(); ss.close(); } }2.UDP
//UDP编程的实现 public class TestUDP { // 发送端 @Test public void send() { DatagramSocket ds = null; try { ds = new DatagramSocket(); byte[] b = "你好,我是要发送的数据".getBytes(); //创建一个数据报:每一个数据报不能大于64k,都记录着数据信息,发送端的IP、端口号,以及要发送到 //的接收端的IP、端口号。 DatagramPacket pack = new DatagramPacket(b, 0, b.length, InetAddress.getByName("127.0.0.1"), 9090); ds.send(pack); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(ds != null){ ds.close(); } } } // 接收端 @Test public void rceive() { DatagramSocket ds = null; try { ds = new DatagramSocket(9090); byte[] b = new byte[1024]; DatagramPacket pack = new DatagramPacket(b, 0, b.length); ds.receive(pack); String str = new String(pack.getData(), 0, pack.getLength()); System.out.println(str); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(ds != null){ ds.close(); } } } }3.URL
/URL:统一资源定位符,一个URL的对象,对应着互联网上一个资源。 //我们可以通过URL的对象调用其相应的方法,将此资源读取(“下载”) public class TestURL { public static void main(String[] args) throws Exception { //1.创建一个URL的对象 URL url = new URL("http://127.0.0.1:8080/examples/HelloWorld.txt?a=b");//File file = new File("文件的路径"); /* * 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的查询名 */ // System.out.println(url.getProtocol()); // System.out.println(url.getHost()); // System.out.println(url.getPort()); // System.out.println(url.getFile()); // System.out.println(url.getRef()); // System.out.println(url.getQuery()); //如何将服务端的资源读取进来: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); } is.close(); //如果既有数据的输入,又有数据的输出,则考虑使用URLConnection URLConnection urlConn = url.openConnection(); InputStream is1 = urlConn.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("abc.txt")); byte[] b1 = new byte[20]; int len1; while((len1 = is1.read(b1)) != -1){ fos.write(b1, 0, len1); } fos.close(); is1.close(); } }