java实训day02

Java的多线程:

        1)继承Thread类

        通过JDK提供的Thread类,重写Thread类的run方法即可。

        

public class CreateThreadDemo4_Task implements Runnable {

    @Override
    public void run() {
		
        while (true) {
            // 输出线程的名字,与主线程名称相区分
            printThreadInfo();
            try {
                // 线程休眠一秒
                Thread.sleep(1000);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    
    private static void printThreadInfo() {
        System.out.println("当前运行的线程名为: " + Thread.currentThread().getName());
    }
}

2.实现Runnable接口

public class running implements Runnable {

    @Override
    public void run() {
		// 每隔1s中输出一次当前线程的名字
        while (true) {
            // 输出线程的名字,与主线程名称相区分
            printThreadInfo();
            try {
                // 线程休眠一秒
                Thread.sleep(1000);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 输出当前线程的信息
     */
    private static void printThreadInfo() {
        System.out.println("当前运行的线程名为: " + Thread.currentThread().getName());
    }
}

TCP通讯协议
    可靠
    请求-响应    服务器
    
    客户端向服务器发送请求
    服务器接收请求
    服务器向客户端发送响应
    Socket套接字
    利用io流实现数据传输

服务端:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
 
public class Main {
    public static void main(String[] args) {
        ServerSocket ss= null;
        try {
            ss = new ServerSocket(8888);
            Socket socket=ss.accept();
 
 
            int len=0;
            byte[]bytes=new byte[1024];
            len=socket.getInputStream().read(bytes);
            String req=new String(bytes,0,len,"utf-8");
            String req1=req.split("\r\n")[0].split(" ")[1];
            String pathName=req1;//已知客户端的意图是获取index.html
            // 读取磁盘中的index.html文件,并且通过socket发送给浏览器
            FileInputStream fis=new FileInputStream("webapp"+pathName);
            //将本地读取的文件发送给客户端
            OutputStream os=socket.getOutputStream();
 
 
            os.write("HTTP /1.1 200 ok\r\n".getBytes("utf-8"));
            os.write("Content-Type:text/html\r\n".getBytes("utf-8"));
            File file=new File("webapp"+pathName);
            os.write(("Content-Length:" + file.length() + "\r\n").getBytes("utf-8"));
            os.write("\r\n".getBytes("utf-8"));
 
 
            while ((len=fis.read(bytes))!=-1){
                os.write(bytes,0,len);
            }
            } catch (IOException e) {
            throw new RuntimeException(e);
            }
        }
    }

客户端

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
 
public class Client {
    public static void main(String[] args) {
        try {
            // 创建客户端Socket对象,指定服务器的IP地址和端口号
            Socket socket = new Socket("10.13.18.200", 9999);
            // 发送请求给服务器
            String request = "这是客户端的请求";
            OutputStream os = socket.getOutputStream();
            os.write(request.getBytes("utf-8"));
            // 接收服务器的响应
            InputStream is = socket.getInputStream();
            byte[] bytes = new byte[1024];
            int len = is.read(bytes);
            String response = new String(bytes, 0, len, "utf-8");
            System.out.println("收到服务器的响应:" + response);
            // 关闭连接
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值