java26
作者:互联网
一.学习内容
网络编程
练习代码
(1)服务器端代码
package com.demo02;
import java.io.*;
import java.net.*;
import java.util.*;
public class ServerDemo {
public static void main(String[] args) {
try (
ServerSocket server =new ServerSocket(4259);
Socket socket=server.accept();
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream());
Scanner input=new Scanner(System.in);
){
System.out.println("客户端:"+is.readLine());
System.out.println("服务器端:");
String line=input.nextLine();
while(!line.equals("bye")){
os.println(line);
os.flush();
System.out.println("客户端:"+is.readLine());
System.out.println("服务器端:");
line=input.nextLine();
}
}catch(IOException e) {
System.out.println("发生异常:"+e);
}
}
}
(2)客户端代码
package com.demo02;
import java.io.*;
import java.net.*;
import java.util.*;
public class ClinentDemo {
public static void main(String[] args) {
try(
Socket socket=new Socket("127.0.0.1",4259);
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os=new PrintWriter(socket.getOutputStream());
Scanner input=new Scanner(System.in);
){
System.out.println("客户端:");
String line=input.nextLine();
while(!line.equals("bye")){
os.println(line);
os.flush();
System.out.println("服务器端:"+is.readLine());
System.out.println("客户端:");
line=input.nextLine();
}
}catch(IOException e) {
System.out.println("发生异常:"+e);
}
}
}
(3)查询IP
package com.demo02;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class FindIpDemo {
public static void main(String[] args) {
try {//查询网络地址
InetAddress idAddress1 =InetAddress.getByName("127.0.0.1");
System.out.println(idAddress1);
InetAddress idAddress2=InetAddress.getLocalHost();
System.out.println(idAddress2);
InetAddress idAddress3=InetAddress.getByName("localhaost");
System.out.println(idAddress3);
//查询网站地址
InetAddress idAddress4=InetAddress.getByName("www.baidu.com");
System.out.println(idAddress4);
//常用方法 System.out.println(idAddress4.getAddress());
System.out.println(idAddress4.getHostAddress());
System.out.println(idAddress4.getHostName());
System.out.println(idAddress4.getCanonicalHostName());
System.out.println();
}catch(UnknownHostException e) {
e.printStackTrace();
}
}
}
(4)通过URL获取文件内容
package com.demo02;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
public class MyURL {
void display() {
byte buffer[]=new byte[50];
System.out.println("请你输入要获取文件的URL地址");
try {
int count=System.in.read(buffer);
String addr=new String(buffer,0,count);
URL url=new URL(addr);
URLConnection c =url.openConnection();
c.connect();
System.out.println("内容类型:"+c.getContentType());
System.out.println("内容编号:"+c.getContentEncoding());
System.out.println("内容长度:"+c.getContentLength());
System.out.println("创建日期:"+new Date(c.getDate()));
System.out.println("最后修改日期:"+new Date(c.getLastModified()));
System.out.println("终止日期:"+new Date(c.getExpiration()));
}catch(IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MyURL app=new MyURL();
app.display();
}
}
标签:java,java26,System,import,println,new,out 来源: https://blog.csdn.net/m0_55680205/article/details/117701870