Tmocat
服务端
- 自定义S
- Tomcat服务器S:Java后台开发
客户端
- 自定义C
- 浏览器B
UDP
1.发短信 不需要连接
发送端
//不需要建立连接
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception{
//1.建立一个Socket
DatagramSocket socket = new DatagramSocket();
//2.创建发送对象和包
String msg="NIHAO";
InetAddress localhost = InetAddress.getByName("localhost");
int port=9000;
//数据,数据长度,发送对象
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length,localhost,port);
//3.发送包
socket.send(packet);
//4.关闭流
socket.close();;
}
}
接收端
//等待客户端连接!
public class UdpSeverDemo01 {
public static void main(String[] args) throws Exception {
//开放端口
DatagramSocket socket = new DatagramSocket(9000);
// 接受数据包
byte[] buffer=new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet); //阻塞接收
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData()));
// 关闭连接
socket.close();
}
}
实现聊天
发送方
public class UdpSenderDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
//准备数据 控制台读取System in
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true){String data=reader.readLine();
byte[] datas=data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length,new InetSocketAddress("localhost",6666));
if (data.equals("bye")) {
break;}
socket.send(packet);
}
socket.close();
}
}
接收方
public class UdpReceiverDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while (true) { //准备接收包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);//阻塞式接收包裹
//断开连接 bye
byte[] data = packet.getData();
String receiveDatas = new String(data, 0, data.length);
System.out.println(receiveDatas);
if (receiveDatas.equals("bye")) {
break;
}
}
socket.close();
}
}
在线咨询
两个都可以是发送方和接收方 ——多线程
明确五个变量
发送方:
- fromPort 发送者的主机端口,socket从这个端口往外接
- toIP socket接到的IP
- toPort socket接到的端口 也是接收方要识别的端口
接收方:
-
port socket从这个端口往外接,也是发送方要识别的端口
-
msgFrom 咨询时的昵称
发送方
import java.io.BufferedReader;
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;
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) {
e.printStackTrace();
}
}
@Override
public void run() {
try{
while (true){String data=reader.readLine();
byte[] datas=data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length,new InetSocketAddress(this.toIP,this.toPort));
if (data.equals("bye")) {
break;}
socket.send(packet);
}
}catch (Exception e){
e.printStackTrace();
}
socket.close();
}
}
接收方
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) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) { //准备接收包裹
try {
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);//阻塞式接收包裹
byte[] data = packet.getData();
String receiveDatas = new String(data, 0, data.length);
System.out.println(msgFrom+receiveDatas);
if (receiveDatas.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
//断开连接 bye
}
socket.close();
}
}
学生端启动
public class TalkStudent {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"laoshi")).start();
}
}
老师端启动
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"xuesheng")).start();
}
}
URL
统一资源定位符:定位互联网上的某个资源
DNS域名解析
协议://ip地址:端口/项目名/资源
public class URLDemo01 {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/helloword/index.jsp?username=kuangshen&password=123");
System.out.println(url.getProtocol()); //协议名
System.out.println(url.getHost()); //主机名
System.out.println(url.getPort()); //端口号
System.out.println(url.getPath()); //文件地址
System.out.println(url.getFile()); //文件全路径
System.out.println(url.getQuery()); //参数
}
}
输出结果:
下载网络资源
public class UrlDown {
public static void main(String[] args) throws Exception {
//1.下载地址
URL url = new URL("http://localhost:8080/qinjiang/SecurityFile.txt");
//2.连接到这个资源 HTTP
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
InputStream inputStream=urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("SecurityFile.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();
}
}