TCP协议案例

Java学习笔记53(网络编制程序:TCP公约案例),学习笔记tcp

简单来说的案例

客户端:

图片 1

package demo;

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", 7000);
        OutputStream out = socket.getOutputStream();
        out.write("hello".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();
    }
}

View Code

劳务器端:

图片 2

package demo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(7000);
        Socket socket = server.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();
        server.close();
    }
}

View Code

先运转服务器端,再起步顾客端,服务器端打字与印刷:hello,然后顾客端打字与印刷:收到

细心区分这里的流对象

 

接下去在那么些案例基础上做八个图形上传案例

图表上传的本色正是文本复制

 

客户端:

图片 3

package demo1;

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", 7000);
        OutputStream out = socket.getOutputStream();
        FileInputStream fis = new FileInputStream("d:\666.jpeg");
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = fis.read(bytes)) != -1) {
            out.write(bytes, 0, len);
        }
        socket.shutdownOutput();
        // 服务器端读不到-1,不会终止,调用这个方法可以终止
        InputStream in = socket.getInputStream();
        len = in.read(bytes);
        System.out.println(new String(bytes, 0, len));
        fis.close();
        socket.close();
    }
}

View Code

劳务器端:

图片 4

package demo1;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

public class TCPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(7000);
        Socket socket = server.accept();
        InputStream in = socket.getInputStream();
        File upload = new File("d:\upload");
        if (!upload.exists()) {
            upload.mkdirs();
        }
        String filename = System.currentTimeMillis() + new Random().nextInt(999999) + ".jpeg";
        FileOutputStream fos = new FileOutputStream(upload + File.separator + filename);
        // 这里是处理图片重名的问题
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = in.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
        }
        socket.getOutputStream().write("上传成功".getBytes());
        fos.close();
        socket.close();
        server.close();
    }
}

View Code

 

即便七个顾客端一同上传,就需求二十八线程的技艺,每种顾客端占叁个线程

客商端无需修改

服务器端:

图片 5

package demo1;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(7000);
        while (true) {
            Socket socket = server.accept();
            new Thread(new Upload(socket)).start();
        }
    }
}

View Code 图片 6

package demo1;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
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("d:\upload");
            if (!upload.exists()) {
                upload.mkdirs();
            }
            String filename = System.currentTimeMillis() + new Random().nextInt(999999) + ".jpeg";
            FileOutputStream fos = new FileOutputStream(upload + File.separator + filename);
            // 这里是处理图片重名的问题
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = in.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
            socket.getOutputStream().write("上传成功".getBytes());
            fos.close();
            socket.close();
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}

View Code

 

简易的案例 客商端: package demo; import java.io.IOException; import java.io.InputStream; import j...

一句话来讲的案例

客户端:

图片 7图片 8

package demo;

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", 7000);
        OutputStream out = socket.getOutputStream();
        out.write("hello".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();
    }
}

View Code

劳务器端:

图片 9图片 10

package demo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(7000);
        Socket socket = server.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();
        server.close();
    }
}

View Code

先运维服务器端,再开发银行客商端,服务器端打印:hello,然后顾客端打字与印刷:收到

留意区分这里的流对象

 

接下去在那个案例基础上做四个图纸上传案例

图片上传的精神即是文件复制

 

客户端:

图片 11图片 12

package demo1;

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", 7000);
        OutputStream out = socket.getOutputStream();
        FileInputStream fis = new FileInputStream("d:\666.jpeg");
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = fis.read(bytes)) != -1) {
            out.write(bytes, 0, len);
        }
        socket.shutdownOutput();
        // 服务器端读不到-1,不会终止,调用这个方法可以终止
        InputStream in = socket.getInputStream();
        len = in.read(bytes);
        System.out.println(new String(bytes, 0, len));
        fis.close();
        socket.close();
    }
}

View Code

劳动器端:

图片 13图片 14

package demo1;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

public class TCPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(7000);
        Socket socket = server.accept();
        InputStream in = socket.getInputStream();
        File upload = new File("d:\upload");
        if (!upload.exists()) {
            upload.mkdirs();
        }
        String filename = System.currentTimeMillis() + new Random().nextInt(999999) + ".jpeg";
        FileOutputStream fos = new FileOutputStream(upload + File.separator + filename);
        // 这里是处理图片重名的问题
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = in.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
        }
        socket.getOutputStream().write("上传成功".getBytes());
        fos.close();
        socket.close();
        server.close();
    }
}

View Code

 

倘诺三个客户端一同上传,就须求多线程的本事,每种客商端占多个线程

客户端不须求修改

劳动器端:

图片 15图片 16

package demo1;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(7000);
        while (true) {
            Socket socket = server.accept();
            new Thread(new Upload(socket)).start();
        }
    }
}

View Code

图片 17图片 18

package demo1;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
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("d:\upload");
            if (!upload.exists()) {
                upload.mkdirs();
            }
            String filename = System.currentTimeMillis() + new Random().nextInt(999999) + ".jpeg";
            FileOutputStream fos = new FileOutputStream(upload + File.separator + filename);
            // 这里是处理图片重名的问题
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = in.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }
            socket.getOutputStream().write("上传成功".getBytes());
            fos.close();
            socket.close();
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}

View Code

 

本文由365bet体育在线官网发布于关于计算机,转载请注明出处:TCP协议案例

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。