Java网络编程之入门篇
目录
- 一、网络基础
- 二、网络协议
- URL类
一、网络基础
文章图片
文章图片
文章图片
二、网络协议
文章图片
文章图片
文章图片
实现TCP的网络编程 例子1:客户端发送信息给服务端,服务端将数据显示在控制台上public class TCPTest1 {//客户端@Testpublic void client() {Socket socket = null; OutputStream os = null; try {//1.创建Socket对象,指明服务器端的ip和端口号InetAddress inet = InetAddress.getByName("127.0.0.1"); socket = new Socket(inet, 8899); //2.获取一个输出流,用于输出数据os = socket.getOutputStream(); //3.写出数据的操作os.write("你好,我是客户端mm".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(); }}} }//服务端@Testpublic void server(){ServerSocket ss = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try {//1.创建服务器的ServerSocket,指明自己的端口号ss = new ServerSocket(8899); //2.调用accept()表示接收来自于客户端的socketsocket = ss.accept(); //3.获取输入流is = socket.getInputStream(); //不建议这样写,可能会有乱码//byte[] buffer = new byte[1024]; //int len; //while((len = is.read(buffer)) != -1){//String str = new String(buffer,0,len); //System.out.println(str); //}//4.读取输入流中的数据baos = new ByteArrayOutputStream(); byte[] buffer = new byte[5]; int len; while((len = is.read(buffer)) != -1){baos.write(buffer,0,len); }System.out.println(baos.toString()); } catch (IOException e) {e.printStackTrace(); } finally {//5.关闭资源if(baos != null){try {baos.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(ss != null){try {ss.close(); } catch (IOException e) {e.printStackTrace(); }}}}}
实现TCP的网络编程 例题2:客户端发送文件给服务端,服务端将文件保存在本地。public class TCPTest2 {//这里异常处理的方式应该使用try-catch-finally@Testpublic void client() throws IOException {Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090); OutputStream os = socket.getOutputStream(); FileInputStream fis = new FileInputStream(new File("beauty.jpg")); byte[] buffer = new byte[1024]; int len; while((len = fis.read(buffer)) != -1){os.write(buffer,0,len); }fis.close(); os.close(); socket.close(); }//这里异常处理的方式应该使用try-catch-finally@Testpublic void server() throws IOException {ServerSocket ss = new ServerSocket(9090); Socket socket = ss.accept(); InputStream is = socket.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("beauty1.jpg")); byte[] buffer = new byte[1024]; int len; while((len = is.read(buffer)) != -1){fos.write(buffer,0,len); }fos.close(); is.close(); socket.close(); ss.close(); }}
实现TCP的网络编程 例题3:从客户端发送文件给服务端,服务端保存到本地,并返回"发送成功"给客户端。并关闭相应的连接 public class TCPTest3 {@Testpublic void client() throws IOException {Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090); OutputStream os = socket.getOutputStream(); FileInputStream fis = new FileInputStream(new File("beauty.jpg")); byte[] buffer = new byte[1024]; int len; while((len = fis.read(buffer)) != -1){os.write(buffer,0,len); }//服务区端给予客户端反馈OutputStream os1 = socket.getOutputStream(); os.write("你好,美女,照片我以收到,非常漂亮!".getBytes()); fis.close(); os.close(); socket.close(); os1.close(); }//这里异常处理的方式应该使用try-catch-finally@Testpublic void server() throws IOException {ServerSocket ss = new ServerSocket(9090); Socket socket = ss.accept(); InputStream is = socket.getInputStream(); FileOutputStream fos = new FileOutputStream(new File("beauty2.jpg")); byte[] buffer = new byte[1024]; int len; while((len = is.read(buffer)) != -1){fos.write(buffer,0,len); }//接受来自于服务器端的数据,并显示到控制台上InputStream is1 = socket.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] bufferr = new byte[20]; int len1; while((len1 = is1.read(buffer)) != -1){baos.write(buffer,0,len1); }System.out.println(baos.toString()); fos.close(); is.close(); socket.close(); ss.close(); baos.close(); }}
文章图片
UDP协议的网络编程public class UDPTest {@Testpublic void sender() throws IOException {DatagramSocket socket = new DatagramSocket(); String str = "我是UDP方式发送的导弹"; byte[] data = https://www.it610.com/article/str.getBytes(); InetAddress inet = InetAddress.getLocalHost(); DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090); socket.send(packet); socket.close(); }@Testpublic void receiver() throws IOException {DatagramSocket socket = new DatagramSocket(9090); byte[] buffer = new byte[100]; DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); socket.receive(packet); System.out.println(new String(packet.getData(),0,packet.getLength())); }
文章图片
文章图片
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/examples/beauty.jpg?username=Tom"); //public String getProtocol()获取该URL的协议名System.out.println(url.getProtocol()); // http//public String getHost()获取该URL的主机名System.out.println(url.getHost()); //localhost//public String getPort()获取该URL的端口号System.out.println(url.getPort()); // 8080//public String getPath()获取该URL的文件路径System.out.println(url.getPath()); //examples/beauty.jpg//public String getFile()获取该URL的文件名System.out.println(url.getFile()); //examples/beauty.jpg?username=Tom//public String getQuery()获取该URL的查询名System.out.println(url.getQuery()); //username=Tom} catch (MalformedURLException e) {e.printStackTrace(); }}}
【Java网络编程之入门篇】以上就是Java网络编程之入门篇的详细内容,更多关于Java网络编程的资料请关注脚本之家其它相关文章!
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- PMSJ寻平面设计师之现代(Hyundai)
- 太平之莲
- 闲杂“细雨”
- 七年之痒之后
- 深入理解Go之generate
- 由浅入深理解AOP
- 期刊|期刊 | 国内核心期刊之(北大核心)
- 生活随笔|好天气下的意外之喜
- 感恩之旅第75天