AI智能
改变未来

网络编程中TCP/UDP编程的实例


剑指Offer 16

操作给定的二叉树,将其变换为源二叉树的镜像。

public class Offer18 {public void solution(TreeNode root){if (root==null){return;}TreeNode temp = root.left;root.left = root.right;root.right = temp;solution(root.left);solution(root.right);}}

socket编程示例

TCP(传输控制协议)概述

  • TCP面向连接的协议
  • TCP连接有两端(点对点的通信)
  • TCP提供可靠的传输服务
  • 传输完毕,徐世芳已建立的连接,效率比UDP低
    1.利用tcpt进行字符传递,并显示在控制台中
    客户端
import java.io.IOException;import java.io.OutputStream;import java.net.InetAddress;import java.net.Socket;//客户端public class ClientTest {public static void main(String[] args) {Socket socket = null;OutputStream outputStream = null;try {//建立tcp连接InetAddress inet = InetAddress.getByName(\"192.168.246.1\");socket = new Socket(inet, 8989);//socket输出流outputStream = socket.getOutputStream();outputStream.write(\"我最帅,我最帅\".getBytes());//关闭资源} catch (Exception e) {e.printStackTrace();} finally {if (socket != null) {try {socket.close();} catch (IOException ex) {ex.printStackTrace();}}if (outputStream != null) {try {outputStream.close();} catch (IOException ex) {ex.printStackTrace();}}}}}

服务端

import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;//服务端public class ServerTest {public static void main(String[] args) {ServerSocket ss = null;Socket socket =null;ByteArrayOutputStream baos=null;try{//serversocket获取链接ss = new ServerSocket(8989);socket = ss.accept();//scoket输入流InputStream is = socket.getInputStream();baos = new ByteArrayOutputStream();//写入byte[] buffer = new byte[1024*1024];int len;while ((len = is.read(buffer))!=-1){baos.write(buffer,0,len);}System.out.println(socket.getInetAddress().getHostAddress());//获取客户端的ip地址System.out.println(baos.toString());//释放资源}catch (Exception e){e.printStackTrace();}finally {if (baos != null) {try {baos.close();} catch (IOException ex) {ex.printStackTrace();}}if (socket != null) {try {socket.close();} catch (IOException ex) {ex.printStackTrace();}}if (ss != null) {try {ss.close();} catch (IOException ex) {ex.printStackTrace();}}}}}

2、在tcp编程中传递文件

客户端

public class ClientTest {public static void main(String[] args) throws IOException {Socket socket = new Socket(InetAddress.getByName(\"127.0.0.1\"),1111);OutputStream os = socket.getOutputStream();FileInputStream fis = new FileInputStream(new File(\"C:\\\\Users\\\\Administrator\\\\IdeaProjects\\\\springboot\\\\27-java-review\\\\1.png\"));byte[] bytes = new byte[1024];int len;while ((len=fis.read(bytes))!=-1){os.write(bytes,0,len);}socket.shutdownOutput();InputStream is = socket.getInputStream();ByteArrayOutputStream baos = new ByteArrayOutputStream();byte[] bytes1 = new byte[20];int len1;while ((len1=is.read(bytes1))!=-1){baos.write(bytes1,0,len1);}System.out.println(baos.toString());fis.close();os.close();socket.close();}}

服务端

public class ServerTest {public static void main(String[] args) throws IOException {ServerSocket ss = new ServerSocket(1111);Socket socket = ss.accept();InputStream is = socket.getInputStream();FileOutputStream fos = new FileOutputStream(new File(\"4.png\"));byte[] bytes = new byte[1024];int len;while ((len=is.read(bytes))!=-1){fos.write(bytes,0,len);}System.out.println(\"接受完成\");OutputStream os = socket.getOutputStream();os.write(\"我已收到文件\".getBytes());fos.close();is.close();socket.close();ss.close();}}

UDP编程示例

UDP(用户数据报协议)概述

  • UDP是无连接的协议
  • UDP不能保证可靠的交付数据
  • UDP是面向报文传输的
  • 传输完毕,无需释放资源,开销小,相对于TCP效率快
    发送端
public class Send {public static void main(String[] args) throws IOException {DatagramSocket socket = new DatagramSocket();String str = \"以UDP的方式发送数据\";byte[] data = str.getBytes();DatagramPacket packet = new DatagramPacket(data,0,data.length,InetAddress.getLocalHost(),1111);socket.send(packet);socket.close();}}

接收端

public class Receive {public static void main(String[] args) throws IOException {DatagramSocket socket = new DatagramSocket(1111);byte[] bytes = new byte[100];DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);socket.receive(packet);System.out.println(new String(packet.getData(),0,packet.getLength()));socket.close();}}
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » 网络编程中TCP/UDP编程的实例