一、多线程
1.Thread//线程类
2.run//线程类中的运行方法
3.start//原神启动
4.Thread.currentThread().getName()//获取当前线程的方法
示例:
public class Main {
public static void main(String[] args) {
Thread t1=new MyThread();
Thread t2=new MyThread();
Thread t3=new MyThread();
t1.start();
t2.start();
t3.start();
}
}
class MyThread extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
}
二、网络编程的一些东西
网络编程(不同主机之间的数据传输)
三要素
1.ip 主机的唯一标识
2.端口 主机所运行的程序的唯一标识
65536 0-1023知名端口
1024-49151 用户程序进程
49152-65535 私有动态端口
3.协议 tcp/ip 可靠
三、客户与服务器
客户端向服务器发送请求
服务器接收请求
服务器向客户端发送响应
客户端接收响应
Socket套接字
利用io流实现数据传输
老师说配个图,那就找一个吧
四、tcp socket网络信息传输
server
1.ServerSocket
new ServerSocket(port)
2.当有用户访问,创建套接字
Socket s=ss.accept();
3.读取请求 获取InputStream socket.getInputStream();
int len=is.read(bytes);
new String(bytes,0,len,"utf-8")
4.发送响应OutputStream socket.getOutputStream();
os.write("".getBytes ("utf-8"));
public class Main1 {
public static void main(String[] args) {
//创建服务器
//创建serversocket对象
try {
//启动服务器
ServerSocket ss=new ServerSocket(9999);
//等待请求
Socket s=ss.accept();
//每个socket都是一个io对象
//输入流读取客户端发送过来的请求
InputStream is=s.getInputStream();
byte[] bytes=new byte[1024];
int len=0;
len=is.read(bytes);
String req=new String(bytes,0,len,"utf-8");
System.out.println("收到"+req);
//响应服务器
OutputStream os=s.getOutputStream();
String resp="响应";
os.write(resp.getBytes("utf-8"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
client 客户端
1.Socket s=new Socket("ip或者域名",port);
localhost 本机的域名
127.0.0.1 本机ip
2.发送请求
3.接收响应
public class Client {
public static void main(String[] args) throws IOException {
// 创建套接字对象访问服务器端口
Socket s = new Socket("127.0.0.1", 9999);
OutputStream os = s.getOutputStream();
os.write("这里是客户端,收到请回答".getBytes("utf-8"));
InputStream is = s.getInputStream();
int len = 0;
byte[]bytes = new byte[1024];
len = is.read(bytes);
System.out.println(new String(bytes, 0, len, "utf-8"));
}
}
五、结合实际
1.现阶段服务器不是1对1,而是为多个客户端提供服务
2.网络编程,多线程
3.步骤
1.ServerScocke ss=new ServerSocket(port)
2.while(true){
Socket s=ss.accept();
3.new ServerThread(s).start;
}
4.编写线程类,传递Socket对象,要实现的功能,就通过run方法调用,或者直接写在run方法中
@Override
public void run() {
try {
//文件的保存位置
// FileInputStream fis=new FileInputStream("img/xxx");
String uuid= UUID.randomUUID().toString();
FileOutputStream fos=new FileOutputStream("img/"+uuid);
//读取客户端发送的文件
byte[]bytes=new byte[1024];
int len=0;
len=s.getInputStream().read(bytes);
fos.write(bytes,0,len);
fos.close();
File file=new File("img/"+uuid);
file.renameTo(new File("img/"+uuid+".txt"));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
接收用户发送的文件,并且保存,简单的上传,
解决文件重名 UUID.randomUUID().toString();
String uuid= UUID.randomUUID().toString();
六、记一下
1.获取到浏览器的请求头部,并且分析请求
2.建立管道 磁盘文件->java内存 fis java内存=>客户端浏览器
3.发送消息头,下面一般固定的,老师不让问为什么,那不问了,不想动脑。
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"));
4.数据响应
好了,又水了一天,瓦洛兰特,启动!!!