UDP:
/** * UDP传输 * * 1.发送Send * 创建DatagramSocket, 随机端口号 * 创建DatagramPacket, 指定数据, 长度, 地址, 端口 * 使用DatagramSocket发送DatagramPacket * 关闭DatagramSocket * @throws Exception * */ /** * 2.接收Receive * 创建DatagramSocket, 指定端口号 * 创建DatagramPacket, 指定数组, 长度 * 使用DatagramSocket接收DatagramPacket * 关闭DatagramSocket * 从DatagramPacket中获取数据 * @throws Exception */
package cn.jkc.net; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Scanner; public class Main03 { /** * UDP传输优化 send * @param args * @throws Exception */ @SuppressWarnings("resource") public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); DatagramSocket socket = new DatagramSocket(); while(true) { String data = sc.nextLine(); if(data.equals("exit")) { break; } DatagramPacket packet = new DatagramPacket (data.getBytes(), data.length(), InetAddress.getByName("172.16.63.18"), 6666); socket.send(packet); } socket.close(); } }
package cn.jkc.net; import java.net.DatagramPacket; import java.net.DatagramSocket; public class Main04 { /** * UDP传输优化recive * @param args * @throws SocketException */ @SuppressWarnings("resource") public static void main(String[] args) throws Exception { DatagramSocket socket = new DatagramSocket(6666); DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); while(true) { socket.receive(packet); byte[] arr = packet.getData(); int vla_len = packet.getLength();//有效长度 String ip = packet.getAddress().getHostAddress(); int port = packet.getPort(); System.out.println (ip + ":" + port + ":" + new String(arr, 0, vla_len)); } } }
TCP:
package cn.jkc.net.tcp; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; /** * 客户端 * @author admin * */ public class Client { public final static String ip = "172.16.63.18"; public final static int port = 65533; public static void main(String[] args) throws Exception { Socket socket = new Socket(ip, port); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintStream ps = new PrintStream(socket.getOutputStream()); System.out.println(br.readLine()); ps.println("客户端发送..."); socket.close(); } }
package cn.jkc.net.tcp; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket(Client.port); while(true) { final Socket socket = server.accept(); new Thread() { public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintStream ps = new PrintStream(socket.getOutputStream()); ps.println("服务器已收到!"); System.out.println(br.readLine()); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); } } }