java网络编程
时间:2021-02-24 13:08:52
收藏:0
阅读:0
定义:
- 网络可以使不同物理位置的计算机达到资源共享和通信的目的。
- 相关的包:java.net
- 提供了两种通信协议:(传输层协议)
- TCP (transmission Control Protocol)传输控制协议,可靠的传输协议,传输前采用“三方握手”的方式建立连接,以保证传输的可靠性。
- UDP ( User Datagram Protocol ) 数据报协议,是不可靠的传输协议,发送出去的数据不一定接收得到,聊天工具一般采用此种协议。
- IP地址:Internet Protocol Address ;IP地址=网络地址(用于识别)+主机地址(用于识别该网络中的主机)
主要类:
- InetAddress类,主要表示IP地址,有两个子类 Inet4Address、Inet6Address
public static void main(String[] args) throws UnknownHostException {
InetAddress locAdd = InetAddress.getLocalHost();
InetAddress biaduAdd = InetAddress.getByName("www.baidu.com");
System.out.println("我的Ip地址"+locAdd.getHostAddress());
System.out.println("本地主机名"+locAdd.getHostName());
System.out.println("百度的Ip地址"+biaduAdd.getHostAddress());
try {
System.out.println("本机是否可达"+locAdd.isReachable(2000));
} catch (IOException e) {
e.printStackTrace();
}
}
- URL(uniform resource locator)统一资源定位符 ,打开网络传输
- try { //指定操作的url URL url = new URL("http","www.mldnjava.cn",80,"/index.html"); InputStream input = url.openStream(); Scanner sc = new Scanner(input); sc.useDelimiter("\n");//设置读取分隔符 while(sc.hasNext()){ System.out.println(sc.next()); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
- URLConnection,通过它可以建立与远程服务器的连接,检查远程资源的属性。
- try { URL url1 = new URL("http://www.mldnjava.cn"); try { URLConnection urlc = url1.openConnection(); System.out.println("内容的大小"+urlc.getContentLength()); System.out.println("内容的类型"+urlc.getContentType()); System.out.println("内容的编码"+urlc.getContentEncoding()); } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); }
- URLEncoder 和 URLDecoder
String keyword = "hello 所有人"; try { String encode = URLEncoder.encode(keyword,"UTF-8"); System.out.println("after encoding====="+encode); String decode = URLDecoder.decode(encode,"UTF-8"); System.out.println("after docode====="+decode); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }
运行结果:
//after encoding=====hello+%E6%89%80%E6%9C%89%E4%BA%BA
//after docode=====hello 所有人
TCP程序设计
java中使用socket(套接字)完成TCP程序的开发,使用此类可以方便的建立可靠的、持续的、双向的、点对点的通信连接。
- SeverSocket类,主要用于服务器端的开发,用于接收客户端的请求;
public Socket accept() throws IOException;
在服务器端,每次运行时都要使用accept()方法等待客户端连接,此方法执行后,服务器将进入阻塞状态,直到客户端连接后程序才可以向下继续执行。返回值为客户端对象。
public class HelloServer { public static void main(String[] args) { ServerSocket server = null; Socket client = null; PrintStream out =null; try { server = new ServerSocket(8888); System.out.println("服务器运行,等待客户端连接"); client =server.accept(); String str = "hello World"; out = new PrintStream(client.getOutputStream()); out.println(str); out.close(); client.close(); server.close(); } catch (IOException e) { e.printStackTrace(); } } }
- Socket类。
在客户端,可以通过Socket类的getInputStream()方法取服务器的输出信息,在服务端可以用getOutputStream()方法取得客户端的输出信息;
public class HelloClient { public static void main(String[] args) throws IOException { Socket client = new Socket("localhost",8888); BufferedReader buf = null; buf = new BufferedReader( new InputStreamReader( client.getInputStream())); String str = buf.readLine(); System.out.println("服务器端输出内容:"+str); client.close(); buf.close(); } }
待续。。。
评论(0)