UDP
发送方
package package1;import java.io.IOException;import java.net.*;public class Demo { public static void main(String[] args) throws IOException { function(); } public static void function() throws IOException { //发送 byte[] data = "你好UDP".getBytes(); InetAddress inetAddress = InetAddress.getByName("127.0.0.1"); DatagramPacket packet = new DatagramPacket(data, data.length, inetAddress, 5000); DatagramSocket socket = new DatagramSocket(); socket.send(packet); socket.close(); }}
接收方
package package1;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;public class Demo1 { public static void main(String[] args) throws IOException { function(); } public static void function() throws IOException { //接收 DatagramSocket socket = new DatagramSocket(5000); //最大值为 1024 * 64 byte[] data = new byte[1024]; DatagramPacket packet = new DatagramPacket(data, data.length); socket.receive(packet); int length = packet.getLength(); String ip = packet.getAddress().getHostAddress(); int port = packet.getPort(); System.out.println(ip + " " + " " + port + new String(data, 0, length)); socket.close(); }}
TCP
服务器
package tcpdemo;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;/** * TCP服务器 */public class TCPServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8888); Socket socket = serverSocket.accept(); InputStream in = socket.getInputStream(); byte[] data = new byte[1024]; int len = in.read(data); System.out.println(new String(data, 0, len)); //服务端向客户端回数据 OutputStream out = socket.getOutputStream(); out.write("收到!".getBytes()); socket.close(); serverSocket.close(); }}
客户端
package tcpdemo;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;/** * TCP客户端 */public class TCPClient { public static void main(String[] args) throws IOException { Socket socket = new Socket("127.0.0.1", 8888); OutputStream outputStream = socket.getOutputStream(); outputStream.write("服务器".getBytes()); //读取服务器发回的数据 InputStream in = socket.getInputStream(); byte[] data = new byte[1024]; int len = in.read(data); System.out.println(new String(data, 0, len)); socket.close(); }}
多线程上传案例
客户端
package updateimage;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;public class TCPClient { public static void main(String[] args) throws IOException { Socket socket = new Socket("127.0.0.1", 8000); OutputStream outputStream = socket.getOutputStream(); FileInputStream fis = new FileInputStream("/Users/mac/Desktop/image.png"); int len = 0; byte[] data = new byte[1024]; while ((len = fis.read(data)) != -1) { outputStream.write(data, 0, len); } //给服务器写终止序列 socket.shutdownOutput(); //读取服务器的上传成功 InputStream in = socket.getInputStream(); len = in.read(data); System.out.println(new String(data, 0, len)); fis.close(); socket.close(); }}
服务器
package updateimage;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class TCPThreadServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8000); while (true) { Socket socket = serverSocket.accept(); new Thread(new Upload(socket)).start(); } }}
package updateimage;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.Socket;import java.util.Random;public class Upload implements Runnable { private Socket socket; public Upload(Socket socket) { this.socket = socket; } public void run() { try { InputStream in = socket.getInputStream(); File upload = new File("/Users/mac/Desktop/image"); if (!upload.exists()) { upload.mkdirs(); } //文件名 域名+毫秒值+6位随机数 String filename = "itcast" + System.currentTimeMillis() + new Random().nextInt(999999) + ".jpg"; FileOutputStream fos = new FileOutputStream(upload + File.separator + filename); byte[] data = new byte[1024]; int len = 0; while ((len = in.read(data)) != -1) { fos.write(data, 0, len); } socket.getOutputStream().write("上传成功".getBytes()); fos.close(); socket.close(); } catch (IOException ex) { } }}