UDP
发短信:不用连接,但需要知道对方的地址
发送消息
- 发送端
package Address;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClient1 {
public static void main(String[] args) throws Exception {
/* 1.建立一个Socket */
DatagramSocket socket = new DatagramSocket();
/* 2.建个包 */
String msg = "HELLO";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(),
msg.getBytes().length,localhost,port);
/* 3.发送包 */
socket.send(packet);
/* 4.关闭流 */
socket.close();
}
}
- 接收端
package Address;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/* 还是要等待客户端的连接 */
public class UDPServer1 {
public static void main(String[] args) throws Exception {
/* 1.开发端口 */
DatagramSocket socket = new DatagramSocket(9090);
/* 2.接收数据包 */
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(),0,packet.getLength()));
/* 3.关闭连接 */
socket.close();
}
}
实现多线程通信
package chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TalkSend implements Runnable {
DatagramSocket socket = null;
BufferedReader reader = null;
@SuppressWarnings("unused")
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 (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
try {
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet);
if(data.equals("bye")) {
break;
}
}catch (Exception e) {
e.printStackTrace();
}
}
socket.close();
}
}
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;
@SuppressWarnings("unused")
private int port;
private String msgFrom;
public TalkReceive(int port, String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(6666);
} 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.equals("bye")) {
break;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
socket.close();
}
}
package chat;
public class TalkStudent {
public static void main(String[] args) {
/* 开启两个线程 */
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
package chat;
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
URL
- URL:统一资源定位符,定位互联网上的某一个资源
- 协议://IP地址:端口号/项目名
package Address;
import java.net.MalformedURLException;
import java.net.URL;
public class URLTest1 {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/helloworld/index.jsp?user");
System.out.println(url.getProtocol());//协议
System.out.println(url.getHost());//主机IP
System.out.println(url.getPort());//端口
System.out.println(url.getPath());//文件
System.out.println(url.getFile());//全路径
System.out.println(url.getQuery());//参数
}
}