2,查找域名的IP地址
package network;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressTest {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getByName("www.baidu.com");
System.out.println("------淘宝主服务器的地址-----"+"\n"+address);
} catch (UnknownHostException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
System.out.println("获取失败");
}
InetAddress[] address2;
try {
System.out.println("------淘宝主服务器的地址-----");
address2 = InetAddress.getAllByName("www.taobao.com");
for (int i = 0; i < address2.length; i++) {
System.out.println(address2[i]);
}
} catch (UnknownHostException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
3,模拟用户登入,判断用户名和密码是否正确
package network;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args) {
//创建以个服务器
Socket socket= null;
InputStream is = null;
OutputStream io= null;
ObjectInputStream ois= null;
try {
ServerSocket serverSocket = new ServerSocket(8800);
//打开服务器
socket = serverSocket.accept();
//打开输入和输出流
is= socket.getInputStream();
io = socket.getOutputStream();
ois = new ObjectInputStream(is);
Entity entity= (Entity)ois.readObject();
if(entity.getName().equals("吊炸天")&&entity.getPsword().equals("88888888")) {
System.out.println("我是服务器,客户登入的信息是"+entity.getName()+","
+entity.getPsword());
}else {
System.out.println("我是服务器,客户登入的信息是"+entity.getName()+","
+entity.getPsword());
System.out.println("对不起,没有该用户,已通知客户端登入失败");
}
if(entity.getName().equals("吊炸天")&&entity.getPsword().equals("88888888")){
OutputStream out = socket.getOutputStream();
out.write("恭喜您,登录成功".getBytes());
}else {
OutputStream out = socket.getOutputStream();
out.write("对不起,没有该用户,登入失败".getBytes());
}
ois.close();
io.close();
is.close();
socket.close();
} catch (IOException | ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
package network;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.net.Socket;
import java.net.UnknownHostException;
public class Man {
public static void main(String[] args) {
Socket socket=null;
OutputStream io=null;
InputStream is=null;
ObjectOutputStream oos=null;
try {
socket = new Socket("localhost",8800);
io = socket.getOutputStream();
is= socket.getInputStream();
oos = new ObjectOutputStream(io);
Entity entity=new Entity();
entity.setName("吊炸天");
entity.setPsword("");
oos.writeObject(entity);
socket.shutdownOutput();
InputStream in = socket.getInputStream();
Reader reader = new InputStreamReader(in);
BufferedReader br = new BufferedReader(reader);
String str = br.readLine();
System.out.println("我是客户端,服务器返回的内容是:"+str);
oos.close();
is.close();
io.close();
socket.close();
} catch (UnknownHostException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
JAVA高级特性与实战项目第五章网络编程课后作业
最新推荐文章于 2023-06-29 10:45:00 发布