-
ip地址和端口号组成了所谓的socket查看全部
-
端口号用来区别一台主机上不同应用程序的通信。查看全部
-
package com.study.socket; import javax.sound.sampled.Port; import java.io.IOException; import java.net.*; /** * UDP Client客服端 */ public class UDPClient { public static void main(String[] args) throws IOException { //1.定义消息 服务端ip地址 端口号 String msg = "用户名:sw;密码:123456"; InetAddress address = InetAddress.getByName("DESKTOP-VQBUTGQ"); int port = 8000; //2.创建数据报 byte[] data = msg.getBytes(); DatagramPacket packet = new DatagramPacket(data, data.length, address , port ); //3.创建socket DatagramSocket socket = new DatagramSocket(); //4.发送消息 socket.send(packet); //5.接收反馈信息 byte[] data1 = new byte[1024]; DatagramPacket packet1 = new DatagramPacket(data1 , 0 , data1.length); socket.receive(packet1); String rt = new String(data1,0,packet1.getLength()); System.out.println("这是客户端,服务端反馈信息为: " + rt); //5.释放资源 socket.close(); } }
package com.study.socket; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; /** * UDP 服务器端 */ public class UDPServer { public static void main(String[] args) { try { //1.建立socket并制定端口号 DatagramSocket socket = new DatagramSocket(8000); int num = 0; while (true){ UDPServerThread thread = new UDPServerThread(socket); thread.setPriority(4); thread.run(); num++; System.out.println("服务器端接受了 " + num + " 个客户端请求!"); } } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
package com.study.socket; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; /** * UDP server 多线程实现 */ public class UDPServerThread extends Thread{ DatagramSocket socket = null; public UDPServerThread(DatagramSocket socket){ this.socket = socket; } @Override public void run() { try { byte[] data = new byte[1024]; DatagramPacket packet = new DatagramPacket(data,data.length); //3.接收客户端发送的数据信息 socket.receive(packet);//此方法在接收到数据报之前一直阻塞 //4.读取数据报信息 String msg = new String(data,0,packet.getLength()); System.out.println("UDP server服务器接收到的信息: " + msg); //5.反馈信息 String rt = "欢迎您!"; byte[] data1 = rt.getBytes(); InetAddress address = packet.getAddress(); int port = packet.getPort(); DatagramPacket packet1 = new DatagramPacket(data1, data1.length , address , port ); socket.send(packet1); } catch (IOException e) { e.printStackTrace(); } } }
查看全部 -
package com.study.socket; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; /** * socket编程模拟客服端 */ public class Client { public static void main(String[] args) { //1.创建客户端socket Socket socket = null; try { socket = new Socket("127.0.0.1",8888); //2.获取输出流 OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream()); PrintWriter pw = new PrintWriter(osw); pw.write("用户名:polly;密码:809"); pw.flush(); //4.释放资源 pw.close(); osw.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
package com.study.socket; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * socket编程模拟服务器端 */ public class Server { public static void main(String[] args) { try { //1.建立服务端socket ServerSocket serverSocket = new ServerSocket(8888); int num = 0; while(true){ //2.监听端口 Socket socket = serverSocket.accept(); ServerThread st = new ServerThread(socket); st.setPriority(4);//设置线程的优先级,范围是【1,10】 默认是5 st.start(); num++; System.out.println("服务器端接受 " + num + " 个客户端请求!"); } } catch (IOException e) { e.printStackTrace(); } } }
package com.study.socket; import java.io.*; import java.net.Socket; /** * 服务器线程实现 */ public class ServerThread extends Thread { Socket socket = null; public ServerThread(Socket socket){ this.socket = socket; } @Override public void run() { if(socket != null){ //读取输入流 InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; OutputStreamWriter osw = null; PrintWriter pw = null; try { is = socket.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); System.out.println("服务器读取客户端信息:" + socket.getLocalAddress() + " : " + socket.getLocalPort() + " " + br.readLine()); //返回响应流 osw = new OutputStreamWriter(socket.getOutputStream()); pw = new PrintWriter(osw); pw.write("请求已接到!"); pw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { //关闭资源 try { if(pw != null) pw.close(); if(osw != null) osw.close(); if(br != null) br.close(); if(isr != null) isr.close(); if(is != null) is.close(); if(socket != null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
查看全部 -
基于udp通信:
查看全部 -
Socket socket=serverSocket.accept();
查看全部 -
URL--统一资源定位符=协议名称:资源名称
java.net提供URL类表示URL
1.URL类中的常用方法:
//创建一个URL对象
URL url = new URL("http://www.baidu.com");
URL url1 = new URL(url, "/index.html?usrname=lqq#test");
//url的常用方法
System.out.println("URL主机名称:"+url1.getHost());
System.out.println("URL协议:"+url1.getProtocol());
System.out.println("URL端口:"+url1.getPort());
System.out.println("URL文件路径:"+url1.getPath());
System.out.println("URL查询字符串:"+url1.getQuery());
System.out.println("URL相对路径:"+url1.getRef());
System.out.println("URL文件名:"+url1.getFile());
2.通过URL获取html文件内容:
public static void main(String[] args) throws IOException {
URL url=new URL("http://www.snnu.edu.cn");
InputStream is =url.openStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader bufferedReader = new BufferedReader(isr);
String data=bufferedReader.readLine();
while (data!=null){
System.out.println(data);
data=bufferedReader.readLine();
}
bufferedReader.close();
isr.close();
is.close();
}查看全部 -
网络基础知识
地址、端口号、协议
java 提供的网络功能有四大类
InetAddress :用于标识网络上的硬件资源
URL:统一资源定位符 通过URL可以直接读取或写入网络上的数据
Sockets:使用TCP协议实现网络通信的Socket相关的类
Datagram:使用UDP协议,将数据保存在数据报中,通过网络进行通信。
查看全部 -
通信模型啊
查看全部 -
InetAddress.getLocalhost();获取InetAddress实例
查看全部 -
Socket编程
1、网络基础知识
2、InetAddress类
3、URL
4、TCP编程(Socket)
5、UDP编程(Datagram)
IP、端口、协议(共同语言)
tcp/ip模型 物理层、数据链路层、网络层、传输层、应用层
端口号范围0~65535,0~1023为系统保留
Java网络功能四大类:InetAddress、URL、Socket、Datagram
查看全部 -
多线程的知识点
查看全部 -
Socket通信模型
查看全部 -
实际应用时是以对象的形式进行传输
查看全部 -
不必关闭输入输出流
查看全部
举报