Day 9 狂神说Java基础笔记(网络编程08-12,多线程01-02)

一、网络编程

08-Tomcat

  • 服务端
    • 自定义 S
    • Tomcat S
  • 客户端
    • 自定义 C
    • 浏览器 B

09-12

https://www.cnblogs.com/shangxinfeng/p/6617636.html
Eclipse中的快捷键快速生成常用代码(例如无参、带参构造,set、get方法),以及Java中重要的内存分析(栈、堆、方法区、常量池)

Teacher.class 
package chat;
public class Teacher {
 public static void main(String[] args) {
  new Thread(new TalkSend(5555, "localhost", 8888)).start();;
  new Thread(new TalkReceive(9999, "学生")).start();
 }
}
Student.class 
package chat;
public class Student {
 public static void main(String[] args) {
  //开启两个线程
  new Thread(new TalkSend(7777, "localhost", 9999)).start();;
  new Thread(new TalkReceive(8888, "老师")).start();
 }
}
TalkSend .class 
package chat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
//多线程
public class TalkSend implements Runnable{
 DatagramSocket socket =null;
 BufferedReader reader=null; 
 private int fromPort;
 private String toIP;
 private int toPort; 
 //初始化数据:Alt+Shift+s+o
 public TalkSend(int fromPort, String toIP, int toPort) {
  this.fromPort = fromPort;
  this.toIP = toIP;
  this.toPort = toPort;
  try {
   socket=new DatagramSocket(fromPort);
   reader=new BufferedReader(new InputStreamReader(System.in));
  } catch (SocketException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  while (true) {
   String data;
   DatagramPacket packet;
   try {
    data = reader.readLine();
    byte[] datas=data.getBytes();
    packet=new DatagramPacket(datas, 0, data.length(), new InetSocketAddress(this.toIP, this.toPort));
    socket.send(packet);
    if (data.startsWith("bye")) {
     break;
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }     
  }
  socket.close();
 }
}
TalkReceive.class
package chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable{
 DatagramSocket socket=null;
 private int port;
 private String msgFrom;
 public TalkReceive(int port, String msgFrom) {
  this.port = port;
  this.msgFrom=msgFrom;
  try {
   socket=new DatagramSocket(port);
  } catch (SocketException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  while (true) {
   try {
    byte[] container=new byte[1024];
    DatagramPacket packet=new DatagramPacket(container, 0, container.length);
    socket.receive(packet);
    byte[] data=packet.getData();
    String receiveData = new String(data,0,data.length);
    System.out.println(msgFrom+": "+receiveData);
    if (receiveData.startsWith("bye")) {
     break;
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }   
  }
  socket.close();
 }  
}

12-URL统一资源定位符

package network;
import java.net.MalformedURLException;
import java.net.URL;
public class URLDemo {
 public static void main(String[] args) {
  try {
   URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=kuangshen&password=123");
   System.out.println(url.getProtocol());
   System.out.println(url.getHost());
   System.out.println(url.getPath());
  } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

开启Tomcat,先创建文件
Day 9 狂神说Java基础笔记(网络编程08-12,多线程01-02)_第1张图片

package network;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class URLDemo {
 public static void main(String[] args) {
  try {
   URL url = new URL("http://localhost:8080/zhengyao/Security.txt");
   //连接到这个资源
   HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
   InputStream inputStream=urlConnection.getInputStream();
   FileOutputStream fos = new FileOutputStream("Security.txt");
   byte[] buffer= new byte[1024];
   int len;
   while ((len=inputStream.read(buffer))!=-1) {
    fos.write(buffer,0,len);    
   }
   fos.close();
   inputStream.close();
   urlConnection.disconnect();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   System.out.println(e);
  }
 }
}

== 注意:作业,实现聊天室==

二、多线程

01

02-基础

  • Process:进程,指令和数据的有序集合,静态概念。
  • Thread:进程,执行程序的一次执行过程,动态概念,一个进程至少包含一个线程,线程是CPU调度和执行的单位。
  • main主线程

你可能感兴趣的:(狂神学习,socket,多线程)