前言:
本文内容:TCP文件上传实现、初识Tomcat、UDP消息发送
推荐免费Java网络编程实战讲解视频:【狂神说Java】网络编程实战讲解_哔哩哔哩_bilibili
TCP文件上传实现
文件上传
客户端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package com.jokerdig.netDemo;
import java.io.*; import java.net.InetAddress; import java.net.Socket;
public class TcpDemo02 { public static void main(String[] args) throws Exception{ Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000); OutputStream out = socket.getOutputStream(); FileInputStream fileInputStream = new FileInputStream(new File("OIP.jpg")); byte[] buffer = new byte[1024]; int len; while((len=fileInputStream.read(buffer))!=-1){ out.write(buffer,0,len); }
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] bytes = new byte[2014]; int len1; while((len1=inputStream.read(bytes))!=-1){ byteArrayOutputStream.write(bytes,0,len1); } System.out.println(byteArrayOutputStream.toString()); fileInputStream.close(); out.close(); socket.close();
} }
|
服务端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| package com.jokerdig.netDemo;
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket;
public class TcpServerDemo { public static void main(String[] args) { ByteArrayOutputStream byteStream = null; InputStream inputStream = null; ServerSocket serverSocket = null; Socket socket = null; try { serverSocket = new ServerSocket(9988); socket = serverSocket.accept(); inputStream = socket.getInputStream();
byteStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1) { byteStream.write(buffer, 0, len); } System.out.println(byteStream.toString()); } catch (IOException e) { e.printStackTrace(); } finally { try { if (byteStream != null) { byteStream.close(); } if (inputStream != null) { inputStream.close(); } if (socket != null) { socket.close(); } if (serverSocket != null) { serverSocket.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
|
初识Tomcat
Tomcat下载
服务端
客户端
解决服务器运行乱码:
Tomcat安装目录>conf>logging文件
1 2 3 4
| java.util.logging.ConsoleHandler.level = FINE java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter java.util.logging.ConsoleHandler.encoding = GBK
|
UDP消息发送
发短信,不用连接,需要知道对方的地址。
发送端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.jokerdig.netDemo;
import java.io.IOException; import java.net.*;
public class UdpDemo3 { public static void main(String[] args) throws IOException { DatagramSocket datagramSocket = new DatagramSocket(); String manage="你好,服务器!"; InetAddress localhost = InetAddress.getByName("localhost"); int port =9090; DatagramPacket packet = new DatagramPacket(manage.getBytes(),0, manage.getBytes().length, localhost, port); datagramSocket.send(packet); datagramSocket.close(); } }
|
接收端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.jokerdig.netDemo;
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException;
public class UdpDemo3_1 { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(9090); byte[] buffer = new byte[1024]; int len; DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); socket.receive(packet); System.out.println(packet.getAddress().getHostAddress()); System.out.println(new String(packet.getData(),0,packet.getLength())); socket.close(); } }
|