博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java - 网络编程、多线程上传案例
阅读量:6834 次
发布时间:2019-06-26

本文共 5013 字,大约阅读时间需要 16 分钟。

hot3.png

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) {        }    }}

 

转载于:https://my.oschina.net/gwlCode/blog/2876056

你可能感兴趣的文章
解决 com.sun.*包导入错误
查看>>
【WP 8.1开发】如何动态生成Gif动画
查看>>
C#零基础入门08:代码规范
查看>>
关于php的mysqlnd驱动
查看>>
Response
查看>>
人人都看得懂的正则表达式教程
查看>>
python matplotlib 绘图
查看>>
Uncaught RangeError: Maximum call stack size exceeded解决思路
查看>>
运用.net core配合VS 2015制作nuget包
查看>>
JSP三大指令 /9大内置对象 /Javabean / EL
查看>>
WebPack系列:Webpack编译的代码如何在tomcat中使用时静态资源路径不对的问题如何解决...
查看>>
《HelloGitHub》之GitHub Bot
查看>>
node.js常见的一些错误信息
查看>>
[LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列
查看>>
华为的端口范围配置命令
查看>>
CVE-2014-4114 和 CVE-2014-3566
查看>>
数学图形(1.29) cochleoid曲线
查看>>
Claims-Based Authentication and Authorization
查看>>
Oracle11g中数据的倒库和入库操作以及高版本数据导入低版本数据可能引发的问题...
查看>>
计算机的鼻祖---差分机的由来
查看>>