import java.net.InetAddress;
public class Address {
public static void main(String[] args) {
String str="www.taobao.com";
try {
InetAddress add1=InetAddress.getByName(str);
System.out.println("------淘宝的主服务器地址-------\n"+add1);
InetAddress[] add2=InetAddress.getAllByName(str);
System.out.println("------淘宝的所有服务器地址------");
for (InetAddress taobao : add2) {
System.out.println(taobao);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.package page3_143;
import java.io.Serializable;
public class User implements Serializable{
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public User(String name, String pwd) {
this.name = name;
this.pwd = pwd;
}
}
package page3_143;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Fuwuqi {
public static void main(String[] args) {
try {
ServerSocket serverSocket=new ServerSocket(7865);
Socket socket=serverSocket.accept();
InputStream is=socket.getInputStream();
ObjectInputStream ois=new ObjectInputStream(is);
User user=(User)ois.readObject();
System.out.println("我是服务器,客户登录信息为:"+user.getName()+","+user.getPwd());
if(user.getName().equals("白小飞")) {
System.out.println("存在该用户,登录成功!");
}else {
System.out.println("对不起,没有该用户,已通知客户端登录失败!");
}
socket.shutdownInput();
OutputStream os=socket.getOutputStream();
String mess=null;
if(user.getName().equals("白小飞")) {
mess="欢迎你,登录成功!";
}else {
mess="对不起,没有该用户,登录失败!";
}
os.write(mess.getBytes());
os.close();
ois.close();
is.close();
socket.close();
serverSocket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package page3_143;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Kehuduan {
public static void main(String[] args) {
Socket socket=null;
try {
Scanner input=new Scanner(System.in);
socket=new Socket("localhost", 7865);
OutputStream os=socket.getOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(os);
System.out.println("请输入用户名:");
String name=input.nextLine();
System.out.println("请输入密码:");
String pwd=input.nextLine();
User user=new User(name,pwd);
oos.writeObject(user);
socket.shutdownOutput();
InputStream is=socket.getInputStream();
String mess=null;
BufferedReader br=new BufferedReader(new InputStreamReader(is));
while((mess=br.readLine())!=null) {
System.out.println("我是客户端,服务器的响应为:"+mess);
}
br.close();
is.close();
oos.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}