一起重新开始学大数据-java篇-DAY25-TCP协议

java篇-DAY25-TCP协议

一起重新开始学大数据-java篇-DAY25-TCP协议

3.TCP通信程序

3.1 TCP通信原理

TCP通信协议是一种可靠的网络协议,它在通信的两端各建立一个Socket对象,从而在通信的两端形成网络虚拟链路,一旦建立了虚拟的网络链路,两端的程序就可以通过虚拟链路进行通信
Java对基于TCP协议的的网络提供了良好的封装,使用Socket对象来代表两端的通信端口,并通过Socket产生IO流来进行网络通信
Java为客户端提供了Socket类,为服务器端提供了ServerSocket类

3.2TCP发送数据

发送数据的步骤

创建客户端的Socket对象
(Socket) Socket(String host,int port)
获取输出流,写数据
OutputStream getOutputStream()
释放资源
void close()

package javaDemo.NetDemo.Linkprotocol.TCP.Demo;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
//发送数据的步骤
//     创建客户端的Socket对象(Socket)
//     获取输出流,写数据
//     释放资源
public class ClientDemo {
    public static void main(String[] args) throws IOException {
//         创建客户端的Socket对象(Socket)
//      Socket (InetAddress address,int port)创主流套接子并将其连接到指定IP地址的指定端口号
//        Socket socket = new Socket(InetAddress.getByName("192.168.142.1"), 4399);
//      Socket (String host,int port)创建流套接字并将其连接到指定主机上的指定端口号
        Socket socket = new Socket("192.168.142.1", 4399);
//        获取输出流,写数据
//        OutputStream getOutputStream() 返回此套接字的输出流
        OutputStream os = socket.getOutputStream();
        os.write("周杰伦YYDS".getBytes());
        //     释放资源
        socket.close();
    }
}

3.3 TCP接收数据

接收数据的步骤

创建服务器端的Socket对象
(ServerSocket) ServerSocket(int port)
监听客户端连接,返回一个Socket对象
Socket accept()
获取输入流。读数据,并把数据显示在控制台
InputStream getInputStream()
释放资源
void close()

package javaDemo.NetDemo.Linkprotocol.TCP.Demo;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/*  接收数据的步骤
       ①创建服务器端的Socket对象(ServerSocket)
        ②获取输入流。读数据,并把数据显示在控制台
        ③释放资源
*/
public class ServerDemo {
    public static void main(String[] args) throws IOException {
//      ①创建服务器端的Socket对象(ServerSocket)
        ServerSocket ss = new ServerSocket(4399);
        Socket s = ss.accept();
//      ②获取输入流。读数据,并把数据显示在控制台
        InputStream is = s.getInputStream();
        byte[] bytes = new byte[1024];
//        int length=is.read(bytes);
//        String data = new String(bytes, 0, length);
        System.out.println("数据是:"+new String(bytes,0,is.read(bytes)));
        s.close();
        ss.close();
    }
}

运行服务端后,运行客户端:

一起重新开始学大数据-java篇-DAY25-TCP协议

3.4 TCP通信程序练习

练习1

客户端:发送数据,接收服务器反馈
服务器:接收数据。给出反馈

ClientDemo.java

package javaDemo.NetDemo.Linkprotocol.TCP.homework;
//练习1
//客户端:发送数据,接收服务器反馈
//服务器:接收数据。给出反馈
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/*
     客户端,发送数据,接收服务器反馈
 */
public class ClientDemo {
    public static void main(String[] args) throws IOException {
        //创建客户端的socket对象(Socket)
        Socket socket = new Socket("192.168.142.1", 4399);
        //获取输出流,写数据
        OutputStream os = socket.getOutputStream();
        os.write("周杰伦yyds!".getBytes());
        //接受服务器反馈
        InputStream is = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int len=is.read(bytes);
        String data=new String(bytes,0,len);
        System.out.println("客户端"+data);
        //释放资源
//        is.close();
//        os.close();//这两个用不上,因为都是根据socket得到的
        socket.close();
    }
}

ServerDemo.java

package javaDemo.NetDemo.Linkprotocol.TCP.homework;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/*
    服务器,接受数据,给出反馈
*/
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        //创建服务器端的socket对象(ServerSocket)
        ServerSocket ss = new ServerSocket(4399);
        //监听客户端连接,返回一个Socket对象
        Socket s = ss.accept();
        //获取输入流,读数据,并把数据显示在控制台
        InputStream is = s.getInputStream();
        byte[] bytes = new byte[1024];
        int len=is.read(bytes);
        String data = new String(bytes, 0, len);
        System.out.println("服务端:" + data);
        //给出反馈
        OutputStream os = s.getOutputStream();
        os.write("数据已收到".getBytes());
        //释放资源
//        s.close();
        ss.close();
    }
}

客户端控制台:

一起重新开始学大数据-java篇-DAY25-TCP协议

服务端控制台:
一起重新开始学大数据-java篇-DAY25-TCP协议

练习2

客户端:数据来自于键盘录入,直到输入的数据是886,发送数据结束
服务器:接收到的数据在控制台输出

ClientDemo.java

package javaDemo.NetDemo.Linkprotocol.TCP.homework2;
import java.io.*;
import java.net.Socket;
/*
    客户端:数据来自于键盘录入,直到输入的数据是886,发送数据结束
*/
public class ClientDemo {
    public static void main(String[] args) throws IOException {
        //创建客户端Socket对象
        Socket socket = new Socket("192.168.142.1",4399);
        //数据来自于键盘录入,直到输入的数据是886,发送数据结束
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        String line="";
        while ((line=br.readLine())!=null){
        if ("886".equals(line)){
            break;
        }
        //获取输出流对象
//        OutputStream os = socket.getOutputStream();
//        os.write(line.getBytes());
            bw.write(line);
            bw.newLine();
            bw.flush();
        }socket.close();
    }
}

ServerDemo.java

package javaDemo.NetDemo.Linkprotocol.TCP.homework2;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/*
    服务器,接受到数据在控制台输出
*/
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        //创建服务器Socket对象
        ServerSocket serverSocket = new ServerSocket(4399);
        //监听客户端的连接,返回一个对应的Socket对象
        Socket s = serverSocket.accept();
        //获取输入流
        InputStream is = s.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line=br.readLine())!=null){
            System.out.println(line);

        }
        serverSocket.close();
    }
}

练习3

客户端:数据来自于键盘录入,直到输入的数据是886,发送数据结束
服务器:接收到的数据写入文本文件

ClientDemo.java

package javaDemo.NetDemo.Linkprotocol.TCP.homework3;

import java.io.*;
import java.net.Socket;

/*
    客户端:数据来自于键盘录入,直到输入的数据是886,发送数据结束
*/
public class ClientDemo {
    public static void main(String[] args) throws IOException {
        //创建客户端Socket对象
        Socket socket = new Socket("192.168.142.1",4399);
        //数据来自于键盘录入,直到输入的数据是886,发送数据结束
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        String line="";
        while ((line=br.readLine())!=null){
        if ("886".equals(line)){
            break;
        }
        //获取输出流对象
//        OutputStream os = socket.getOutputStream();
//        os.write(line.getBytes());
            bw.write(line);
            bw.newLine();
            bw.flush();
        }socket.close();
    }
}

ServerDemo.java

package javaDemo.NetDemo.Linkprotocol.TCP.homework3;

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

/*
    服务器,接受到数据写入文本文件
*/
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        //创建服务器Socket对象
        ServerSocket serverSocket = new ServerSocket(4399);
        //监听客户端的连接,返回一个对应的Socket对象
        Socket s = serverSocket.accept();
        //获取输入流
        InputStream is = s.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        //数据写入文本
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\ideafile\\Bigdata\\data\\receive1.txt"));
        String line;
        while ((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        //释放资源
        serverSocket.close();
    }
}

执行ServerDemo,然后执行client在client的控制台输入信息
一起重新开始学大数据-java篇-DAY25-TCP协议

打开对应的文本文件结果:
一起重新开始学大数据-java篇-DAY25-TCP协议

练习4

客户端:数据来自于文本文件
服务器:接收到的数据写入文本文件

ClientDemo.java

package javaDemo.NetDemo.Linkprotocol.TCP.homework4;
import java.io.*;
import java.net.Socket;
/*
    客户端:数据来自于文本文件
*/
public class ClientDemo {
    public static void main(String[] args) throws IOException {
        //创建客户端Socket对象
        Socket socket = new Socket("192.168.142.1",4399);
       //数据来自于文本文件,封装文本文件的数据
        BufferedReader br = new BufferedReader(new FileReader("D:\\ideafile\\Bigdata\\data\\1.txt"));
        //封装输出流写数据
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        String line="";
        while ((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }socket.close();
    }
}

ServerDemo.java

package javaDemo.NetDemo.Linkprotocol.TCP.homework4;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/*
    服务器,接受到数据写入文本文件
*/
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        //创建服务器Socket对象
        ServerSocket serverSocket = new ServerSocket(4399);
        //监听客户端的连接,返回一个对应的Socket对象
        Socket s = serverSocket.accept();
        //获取输入流
        InputStream is = s.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        //数据写入文本
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\ideafile\\Bigdata\\data\\receive.txt"));
        String line;
        while ((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        //释放资源
        serverSocket.close();
    }
}

执行Server等待客户端client输入,执行client输入从本地文件1.txt中获取的信息
一起重新开始学大数据-java篇-DAY25-TCP协议

Server将收到的信息写入到一个新的文本文件receive2.txt中
一起重新开始学大数据-java篇-DAY25-TCP协议

练习5

客户端:数据来自于文本文件,接收服务器反馈
服务器:接收到的数据写入文本文件,给出反馈
出现问题:程序一直等待
原因:读数据的方法是阻塞式的
解决办法:自定义结束标记;使用shutdownOutput(方法(推荐)

ClientDemo.java

package javaDemo.NetDemo.Linkprotocol.TCP.homework5;
import java.io.*;
import java.net.Socket;
/*
    客户端:数据来自于文本文件
*/
public class ClientDemo {
    public static void main(String[] args) throws IOException {
        //创建客户端Socket对象
        Socket socket = new Socket("192.168.142.1",4399);
        //数据来自于文本文件,封装文本文件的数据
        BufferedReader br = new BufferedReader(new FileReader("D:\\ideafile\\Bigdata\\data\\1.txt"));
        //封装输出流写数据
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        String line="";
        while ((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
//        //自定义结束标记(不好用,玩意文件有自定义标记)
//        bw.write("886");
//        bw.newLine();
//        bw.flush();
//        public void shutdownOutput()
        socket.shutdownOutput();

        //接受反馈
        BufferedReader brC = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String data=brC.readLine();
        System.out.println("服务器反馈"+data);
        //释放资源
        socket.close();
    }
}

ServerDemo.java

package javaDemo.NetDemo.Linkprotocol.TCP.homework5;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/*
    服务器,接受到数据写入文本文件,给出反馈
*/
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        //创建服务器Socket对象
        ServerSocket serverSocket = new ServerSocket(4399);
        //监听客户端的连接,返回一个对应的Socket对象
        Socket s = serverSocket.accept();
        //获取输入流
        InputStream is = s.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        //数据写入文本
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\ideafile\\Bigdata\\data\\receive3.txt"));
        String line;
        while ((line=br.readLine())!=null){
//            if ("886".equals(line)){//因为特殊标记不好用,舍去
//                break;
//            }
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        //给出反馈
        BufferedWriter bwS = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        bwS.write("文件上传成功");
        bwS.newLine();
        bwS.flush();
        //释放资源
        serverSocket.close();
    }
}

练习6

客户端:数据来自于文本文件,接收服务器反馈
服务器:接收到的数据写入文本文件,给出反馈,代码用线程进行封装,为每一个客户端开启一个线程

ClientDemo.java

package javaDemo.NetDemo.Linkprotocol.TCP.homework6;
import java.io.*;
import java.net.Socket;

/*
    客户端:数据来自于文本文件
*/
public class ClientDemo {
    public static void main(String[] args) throws IOException {
        //创建客户端Socket对象
        Socket socket = new Socket("192.168.142.1",4399);
        //数据来自于文本文件,封装文本文件的数据
        BufferedReader br = new BufferedReader(new FileReader("D:\\ideafile\\Bigdata\\data\\1.txt"));
        //封装输出流写数据
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        String line="";
        while ((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
//        //自定义结束标记(不好用,玩意文件有自定义标记)
//        bw.write("886");
//        bw.newLine();
//        bw.flush();
//        public void shutdownOutput()
        socket.shutdownOutput();

        //接受反馈
        BufferedReader brC = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String data=brC.readLine();
        System.out.println("服务器反馈"+data);
        //释放资源
        socket.close();
    }
}

ServerDemo.java

package javaDemo.NetDemo.Linkprotocol.TCP.homework6;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/*
    服务器,接受到数据写入文本文件,给出反馈,代码用线程进行封装,为每一个客户端开启一个线程
*/
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        //创建服务器Socket对象
        ServerSocket serverSocket = new ServerSocket(4399);
        while (true) {
            //监听客户端的连接,返回一个对应的Socket对象
            Socket s = serverSocket.accept();
           new Thread(new ServerThread(s)).start();

        //释放资源(while循环不再调用)
//        serverSocket.close();
    }
}}

ServerThread.java

package javaDemo.NetDemo.Linkprotocol.TCP.homework6;
import java.io.*;
import java.net.Socket;
public class ServerThread implements Runnable{
    private Socket s;
    public ServerThread(Socket s) {
        this.s=s;
    }
    @Override
    public void run() {
        //接受数据写到文本文件
        //获取输入流
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            //数据写入文本
//            BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\ideafile\\Bigdata\\data\\receive3.txt"));
           //解决文件名称冲突问题
            int count=0;
            File file = new File("D:\\ideafile\\Bigdata\\data\\receive" + count + ".txt");
            while (file.exists()){
                count++;
                file = new File("D:\\ideafile\\Bigdata\\data\\receive" + count + ".txt");
            }
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            String line;
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
                bw.flush();
            }
            //给出反馈
            BufferedWriter bwS = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bwS.write("文件上传成功");
            bwS.newLine();
            bwS.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        }
}

结果:

一起重新开始学大数据-java篇-DAY25-TCP协议

练习7

需求:(TCP,对象流)搞个登录系统

User

package javaDemo.NetDemo.Linkprotocol.TCP.Homework7;
import java.io.Serializable;
public class User implements Serializable {
    private String id;
    private int password;
    public User() {
    }

    public User(String id, int password) {
        this.id = id;
        this.password = password;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getPassword() {
        return password;
    }

    public void setPassword(int password) {
        this.password = password;
    }
}

Client

package javaDemo.NetDemo.Linkprotocol.TCP.Homework7;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws Exception{
        Socket s = new Socket("127.0.0.1",10086);
       while (true){
           Scanner scanner = new Scanner(System.in);
           System.out.println("请输入用户名");
           String id = scanner.next();
           System.out.println("请输入密码");
           int password = scanner.nextInt();
           ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
           oos.writeObject(new User(id,password));
           BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
           System.out.println("反馈信息:"+br.readLine());
           System.out.println("----是否退出?----\n    1.继续\n   2.退出");
           int exit = scanner.nextInt();
           if (exit==2){
               break;
           }
       }
        s.close();

    }
}

Server

package javaDemo.NetDemo.Linkprotocol.TCP.Homework7;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(10086);
        while (true){
            Socket s = ss.accept();
            ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
            User user=(User)ois.readObject();
            if (!user.getId().equals("root")){
                backsend1(s,1);
            }else if (user.getId().equals("root")&&user.getPassword()!=123456){
                backsend1(s,2);
            }else{
                backsend1(s,3);
            }
            ss.close();
        }
    }
    public static void backsend1(Socket s,Integer i) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        switch (i){
            case 1:
                bw.write("用户名不存在");
                bw.newLine();
                bw.flush();
                break;
            case 2:
                bw.write("密码错误");
                bw.newLine();
                bw.flush();
            case 3:
                bw.write("登录成功!");
                bw.newLine();
                bw.flush();

        }

    }
}

一起重新开始学大数据-java篇-DAY25-TCP协议
一起重新开始学大数据-java篇-DAY25-TCP协议

一起重新开始学大数据-java篇-DAY25-TCP协议
|
|
|
|
|
|
|
|

上一章节-java篇-DAY24-网络编程入门、UDP协议

下一章节-随缘更新

一起重新开始学大数据-java篇-DAY25-TCP协议

上一篇:【IO编程】7.转换流


下一篇:Java字符串导出指定格式文件