获取客户端IP地址的代码
//获取客户端IP
InetAddress ia= socket.getInetAddress();
String ip=ia.getHostAddress();
System.out.println("此客户端的IP为"+ip);
LoginThread 类
package tcpscoket_obj_29;
import ;
import ;
import ;
import ;
import ;
//线程类:处理客户请求(接受请求 获取客户端IP 返回响应)
public class LoginThread extends Thread{
Socket socket=null;
//通过构造方法完成对Socket的接收
public LoginThread(Socket socket){
=socket;
}
//处理客户端请求
public void run(){
try{
//3.通过对象输入流获取用户请求信息(反序列化的过程)
InputStream is=();
ObjectInputStream ois=new ObjectInputStream(is);
User user=(User)();
("客户端说:"+()+"-"+());
//获取客户端IP
InetAddress ia= ();
String ip=();
("此客户端的IP为"+ip);
//通过输出流给客户端一个响应
OutputStream os=();
String reply="欢迎登录!";
(());
//4.关闭相应的数据流和Socket
();
();
();
();
}catch (Exception e) {
// TODO: handle exception
();
}
}
}