Java 网络:InetAddress类的应用以及通过Socket实现TCP编程

InetAddress对域名进行解析是使用本地机器配置或者网络命名服务(如域名系统(Domain Name System,DNS)和网络信息服务(Network Information Service,NIS))来实现。对于DNS来说,本地需要向DNS服务器发送查询的请求,然后服务器根据一系列的操作,返回对应的IP地址,为了提高效率,通常本地会缓存一些主机名与IP地址的映射,这样访问相同的地址,就不需要重复发送DNS请求了。

在java.net.InetAddress类同样采用了这种策略。在默认情况下,会缓存一段有限时间的映射,对于主机名解析不成功的结果,会缓存非常短的时间(10秒)来提高性能。

如何获取InetAddress对象


InetAddress类没有提供构造器提供了下面两个静态方法来获取InerAdress实例:

static InetAddress getByAddress(byte[] addr):根据原始IP地址来获取对应的InetAddress实例。

static InetAddress getByName(String host):根据主机获取对应的InetAddress实例。

Inet还提供了一些方法来获取InetAddress实例对应的IP地址和主机名(可查API文档)。

实例:通过Socket实现TCP编程

==================

java代码如下:


Client.java


/*

  • 客户端

*/

public class Client {

public static void main(String[] args) {

  try {
  	//1.创建客户端Socket,指定服务器地址和端口
  	Socket socket=new Socket("localhost", 8888);
  	//2.获取输出流,向服务器端发送信息
  	OutputStream os=socket.getOutputStream();//字节输出流
  	PrintWriter pw=new PrintWriter(os);//将输出流包装为打印流
  	pw.write("用户名:alice;密码:123");
  	pw.flush();
  	socket.shutdownOutput();//关闭输出流
  	//3.获取输入流,并读取服务器端的响应信息
  	InputStream is=socket.getInputStream();
  	BufferedReader br=new BufferedReader(new InputStreamReader(is));
  	String info=null;
  	while((info=br.readLine())!=null){
  		System.out.println("我是客户端,服务器说:"+info);
  	}
  	//4.关闭资源
  	br.close();
  	is.close();
  	pw.close();
  	os.close();
  	socket.close();
  } catch (UnknownHostException e) {
  	e.printStackTrace();
  } catch (IOException e) {
  	e.printStackTrace();
  }

}

}

ServerThread.java


/*

  • 服务器线程处理类

*/

public class ServerThread extends Thread {

// 和本线程相关的Socket

Socket socket = null;

public ServerThread(Socket socket) {

  this.socket = socket;

}

//线程执行的操作,响应客户端的请求

public void run(){

  InputStream is=null;
  InputStreamReader isr=null;
  BufferedReader br=null;
  OutputStream os=null;
  PrintWriter pw=null;
  try {
  	//获取输入流,并读取客户端信息
  	is = socket.getInputStream();
  	isr = new InputStreamReader(is);
  	br = new BufferedReader(isr);
  	String info=null;
  	while((info=br.readLine())!=null){//循环读取客户端的信息
  		System.out.println("我是服务器,客户端说:"+info);
  	}
  	socket.shutdownInput();//关闭输入流
  	//获取输出流,响应客户端的请求
  	os = socket.getOutputStream();
  	pw = new PrintWriter(os);
  	pw.write("欢迎您!");
  	pw.flush();//调用flush()方法将缓冲输出
  } catch (IOException e) {
  	// TODO Auto-generated catch block
  	e.printStackTrace();
  }finally{
  	//关闭资源
  	try {
  		if(pw!=null)
  			pw.close();
  		if(os!=null)
  			os.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();
  	}
  }

}

}

Server.java


/*

  • 基于TCP协议的Socket通信,实现用户登陆
  • 服务器端

*/

public class Server {

public static void main(String[] args) {

  try {
  	//创建一个服务器端Socket,即ServerSocket,指定绑定的端口,并监听此端口
  	ServerSocket serverSocket=new ServerSocket(8888);
  	Socket socket=null;
  	//记录客户端的数量
  	int count=0;
  	System.out.println("***服务器即将启动,等待客户端的连接***");
  	//循环监听等待客户端的连接
  	while(true){
  		//调用accept()方法开始监听,等待客户端的连接
  		socket=serverSocket.accept();
  		//创建一个新的线程
  		ServerThread serverThread=new ServerThread(socket);
  		//启动线程
  		serverThread.start();
  		count++;//统计客户端的数量
  		System.out.println("客户端的数量:"+count);
  		InetAddress address=socket.getInetAddress();
  		System.out.println("当前客户端的IP:"+address.getHostAddress());
  	}
  } catch (IOException e) {
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值