1InetAddress 类
/**
- java封装了一个IP地址类。
- @author Administrator
*/
public class InetAddressDemo {
public static void main(String[] args) throws Exception {
InetAddress in = InetAddress.getLocalHost();
System.out.println("本机名称:"+in.getHostName());
System.out.println("IP地址:"+in.getHostAddress());
InetAddress baidu = InetAddress.getByName("www.baidu.com");
System.out.println("名称:"+baidu.getHostName());
System.out.println("IP地址:"+baidu.getHostAddress());
InetAddress add = InetAddress.getByName("192.168.1.47");
System.out.println("名称:"+add.getHostName());
}
}
2URL 统一资源定位器
/**
- URL 统一资源定位器。
- @author Administrator
*/
public class URLDemo {
public static void main(String[] args) throws Exception {
URL url = new URL("http://192.168.1.189:8080/news/show.do?name=abc&id=123");
System.out.println(url.getHost());
System.out.println(url.getPath());// 资源路径。
System.out.println(url.getFile());
System.out.println(url.getPort());
System.out.println(url.getProtocol());
System.out.println(url.getQuery());
}
}
3ServletSocket服务器端
/**
- 服务端:
- @author Administrator
*/
public class ServerDemo {
public static void main(String[] args) {
// 启动程序:
ServerDemo sd = new ServerDemo();
try {
sd.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 开启连接。
public void start() throws IOException{
// 开启服务器:
ServerSocket ss = new ServerSocket(8888);// 0-1024 不能用。系统保留的。
// 使用循环获取连接。
while(true){
System.out.println("等待客户端连接进入……");
Socket s = ss.accept();// 监听客户端。 线程阻塞。
System.out.println("连接成功");
// 启动线程。
new Service(s).start();
}
}
// 使用线程内部类。进行与客户端进行通信。
class Service extends Thread{
// 获取Socket对象。
Socket s;
// 使用构造方法。获取Socket对象。
public Service(Socket s){
this.s = s;
}
// 使用run方法。
@Override
public void run() {
// 获取输入流
try {
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
out.write("hello".getBytes());
out.flush();
// 接受客户端回应。再响应。
Scanner s = new Scanner(in);
while(true){
String str = s.nextLine().trim();
System.out.println(str);// 客户端说的话。
// 响应。
Scanner input = new Scanner(System.in);
String str1 = input.next();
out.write(str1.getBytes());
out.write("\n".getBytes());
out.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
4ClientSocket 客户端
/**
- 客户端:
*/
public class ClientDemo {
public static void main(String[] args) {
ClientDemo client = new ClientDemo();
try {
client.open();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 服务端方法。
public void open() throws Exception, IOException{
// 创建socket创建连接:
Socket socket = new Socket("localhost", 8888);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
// 读取线程中的内容。发送给服务端。
new Reader(out).start();
// 读取服务端的内容。写到本地。
new Writer(in).start();
}
// 读取:本地的信息。发送给服务端。 向服务器说话
class Reader extends Thread{
// 获得输出流。
OutputStream out;
public Reader(OutputStream out){
this.out = out;
}
@Override
public void run() {
// 向服务器发送信息。
Scanner input = new Scanner(System.in);
String str = "";
while(true){
str = input.nextLine();
try {
out.write(str.getBytes());
out.write("\n".getBytes());
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 服务端向我说的话。
class Writer extends Thread{
InputStream in;// 就是 服务端 发送来的消息。
public Writer(InputStream in){
this.in = in;
}
@Override
public void run() {
// 消息打印在本地。
Scanner s = new Scanner(in);
while(true){
System.out.println(s.nextLine().trim());
}
}
}
}