- 十二、网络编程
- 网络编程IP 地址和端口
- 获取本机IP地址
- 使用java 执行ping命令
- 网络编程Socket
- 建立连接
- 收发数字
- 收发字符串
- 多线程聊天
- 练习
- 1)练习-判断本网段有多少可用的ip地址
- 2)练习-聊天机器人数据库
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Exercise {
public static void main(String[] args) throws UnknownHostException {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost.getHostAddress());
}
}
使用java 执行ping命令
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
public class Exercise {
public static void main(String[] args) throws IOException {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost.getHostAddress());
Process p = Runtime.getRuntime().exec("ping 192.168.0.101 ");
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(),"GBK"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine())!= null){
sb.append(line+"n");
}
System.out.println(sb);
}
}
网络编程Socket
建立连接
server
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept();
System.out.println("建立连接:"+s);
s.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
client
import java.io.IOException;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1",8888);
System.out.println(s);
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
收发数字
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept();
System.out.println("建立连接:"+s);
InputStream is = s.getInputStream();
System.out.println(is.read());
is.close();
s.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1",8888);
System.out.println(s);
OutputStream op = s.getOutputStream();
op.write(110);
op.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
收发字符串
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept();
System.out.println("建立连接:"+s);
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readUTF());
dis.close();
s.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1",8888);
System.out.println(s);
OutputStream op = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(op);
dos.writeUTF("你好");
dos.close();
op.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
多线程聊天
server
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept();
System.out.println("建立连接:"+s);
new Thread(new SendTread(s)).start();
new Thread(new ReceiveTread(s)).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
client
import java.io.IOException;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1",8888);
System.out.println(s);
new Thread(new SendTread(s)).start();
new Thread(new ReceiveTread(s)).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
SendTread
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class SendTread implements Runnable{
public Socket s = null;
public SendTread(Socket s) {
this.s = s;
}
@Override
public void run() {
try {
OutputStream op = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(op);
while (true) {
Scanner scan = new Scanner(System.in);
String str = scan.next();
dos.writeUTF(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
ReceiveTread
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class ReceiveTread implements Runnable{
public Socket s = null;
public ReceiveTread(Socket s) {
this.s = s;
}
@Override
public void run() {
try {
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
while (true) {
System.out.println("来自:"+s.getInetAddress()+":"+s.getPort()+"<----"+dis.readUTF());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
练习
1)练习-判断本网段有多少可用的ip地址
首先获取 获取本机IP地址,比如说是192.168.2.100。 那么本网段的ip地址就是从 192.168.2.1 到 192.168.2.255
再通过使用java 执行ping命令 判断这些ip地址能否使用,把能够使用的ip打印出来
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Exercise {
public static void main(String[] args) throws IOException{
for (int i = 1; i < 255; i++) {
Process p = Runtime.getRuntime().exec("ping -n 1 192.168.0."+i);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(),"GBK"));
String line = null;
Boolean b = false;
while ((line = br.readLine())!= null){
if(line.contains("无法访问目标主机")){
b = true;
break;
}
}
br.close();
if(!b){
System.out.println("该ip可以ping通:"+"ping 192.168.0."+i);
}
}
}
}
2)练习-聊天机器人数据库
首先创建一个数据库android(android现在是手机操作系统,其本意是机器人的意思)
然后创建一个表dictionary,字段:
id int
receive varchar(100) receive 表示受到的信息
response varchar(100) response 表示回应的信息
在这个表里准备一些数据:接收到指定字符串,返回相应指定字符串
开发一个程序,当从scanner读取到消息,发给Server服务端,服务端用这个消息到表dictionary中找到对应的相应,返回出去。 看上去就像在自动回应一样。
如果一个recieve有多条response,则随机返回一条
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ServerAI {
public static void main(String[] args) {
String url = "jdbc:mysql://127.0.0.1:3306/android?characterEncoding=UTF-8";
String username = "root";
String password = "admin";
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try(Connection c = DriverManager.getConnection(url,username,password);
Statement statement = c.createStatement();) {
ServerSocket ss = new ServerSocket(9999);
Socket s = ss.accept();
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
while (true) {
String sql = "select * from dictionary where receive ="+"'"+dis.readUTF()+"'";
System.out.println(sql);
ResultSet rs = statement.executeQuery(sql);
String response = null;
List list = new ArrayList<>();//同样问题的多个响应放在这里
while (rs.next()){
response = rs.getString(3);
list.add(response);
}
int index = (int)(Math.random()*list.size());//随机索引
//输入未知内容的反应
if(list.size() == 0){
dos.writeUTF("我不知道你在说啥");
}else {
dos.writeUTF(list.get(index));
}
list.clear();
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class ClientAI {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1",9999);
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
while (true) {
Scanner scan = new Scanner(System.in);
dos.writeUTF(scan.nextLine());
System.out.println(dis.readUTF());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}



