TCP通信
使用TCP协议实现客户端与服务端通信
在客户端使用InetAddress获取ip,socket连接服务器并发送消息,ServerSocket建立服务端端口,
accept等待用户连接,当用户连接后通过使用io流的方式获取用户发送的信息。
public class TCPs2 { public static void main(String[] args) { // 服务端 ServerSocket socket = null; Socket accept = null; InputStream inputStream = null; ByteArrayOutputStream outputStream = null; try { socket = new ServerSocket(9999); // 等代客户端连接 accept = socket.accept(); inputStream = accept.getInputStream(); // 管道流 outputStream = new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int len; len = inputStream.read(bytes); while (len != -1){ outputStream.write(bytes, 0, len); } System.out.println(outputStream.toString()); } catch (IOException e) { e.printStackTrace(); }finally { // 关闭流 try { outputStream.close(); inputStream.close(); accept.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
public class TCPs1 { public static void main(String[] args) { // 客户端 try { InetAddress byName = InetAddress.getByName("127.0.0.1"); int port = 9999; // 创建连接 Socket socket = new Socket(byName, port); // 发送信息 OutputStream stream = socket.getOutputStream(); stream.write("发发接口".getBytes()); stream.close(); socket.close(); } catch (Exception e) { e.printStackTrace(); } } }
客户端向服务器发送文件
public static void main(String[] args) throws Exception{ // 创建连接 Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000); // 输出流 OutputStream outputStream = socket.getOutputStream(); // 文件流 FileInputStream stream = new FileInputStream(new File("src单元测试.xls")); byte[] bytes = new byte[1024]; int len; while ((len=stream.read(bytes))!=-1){ outputStream.write(bytes,0,len); } // 通知服务器我已经传输完了 socket.shutdownOutput(); // 接收信息 InputStream inputStream = socket.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int len2; byte[] buffer = new byte[1024]; while ((len2=inputStream.read(buffer))!=-1){ byteArrayOutputStream.write(buffer,0,len2); } } }
public class Dome2 { public static void main(String[] args) throws Exception{ // 创建服务 ServerSocket serverSocket = new ServerSocket(9000); // 监听客户连接 Socket socket = serverSocket.accept(); // Scanner scanner = new Scanner(System.in); // 获取输入流 InputStream inputStream = socket.getInputStream(); // 文件输出 FileOutputStream fileOutputStream = new FileOutputStream(new File("receive")); byte[] bytes = new byte[1024]; int len; while ((len=inputStream.read(bytes))!=-2){ fileOutputStream.write(bytes,0,len); } // 通知客户端 OutputStream outputStream = socket.getOutputStream(); outputStream.write("我收到信息了".getBytes()); } }
使用UDP协议实现客户端与服务端通信
UDP协议不像TCP协议那样需要彼此之间建立连接,也不区别客户端与服务端,只需要知道地址就可以发送包的方式给对方传递信息,有点类似于发短信的方式。(不管你要不要,反正我发给你)
public static void main(String[] args) throws Exception{ // 需要等待客服端的连接 // 开放端口 DatagramSocket socket = new DatagramSocket(9000); // 接收数据包 byte[] bytes = new byte[1024]; DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length); socket.receive(packet); // 堵塞接收 System.out.println(packet.getAddress().getHostAddress()); System.out.println(new String(packet.getData(),0,packet.getLength())); socket.close(); }
public static void main(String[] args) throws Exception{ // 建立一个socket DatagramSocket socket = new DatagramSocket(); // 建立包 String msg = "hello"; // 接收人 InetAddress loc = InetAddress.getByName("127.0.0.1"); int port = 9000; DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, loc, port); // 发送包 socket.send(packet); }