欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

网络编程(2)

时间:2023-07-10
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 {DatagramSocket socket = new DatagramSocket();String msg = "HELLO";InetAddress localhost = InetAddress.getByName("localhost");int port = 9090;DatagramPacket packet = new DatagramPacket(msg.getBytes(),msg.getBytes().length,localhost,port);socket.send(packet);socket.close();}}

接收端

package Address;import java.net.DatagramPacket;import java.net.DatagramSocket;public class UDPServer1 {public static void main(String[] args) throws Exception {DatagramSocket socket = new DatagramSocket(9090);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()));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();}}@Overridepublic void run() {// TODO Auto-generated method stubwhile(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 blocke.printStackTrace();}}@Overridepublic void run() {// TODO Auto-generated method stubwhile(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 blocke.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());//主机IPSystem.out.println(url.getPort());//端口System.out.println(url.getPath());//文件System.out.println(url.getFile());//全路径System.out.println(url.getQuery());//参数}}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。