简单了解websocket

ping、ipconfig

1
2
3
4
5
6
7
8
9
10
Process p = Runtime.getRuntime().exec("ping " + "192.168.1.108");
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
if (line.length() != 0)
sb.append(line + "\r\n");
}
System.out.println("本次指令返回的消息是:");
System.out.println(sb.toString());
1
2
3
4
5
6
7
8
9
10
Process p = Runtime.getRuntime().exec("ipconfig");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
while ((line=bufferedReader.readLine())!=null){
if (line.length()!=0){
stringBuilder.append(line+"\r\n");
}
}
System.out.println(stringBuilder.toString());

建立连接

1
2
3
4
5
6
7
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(3000);
Socket accept = serverSocket.accept();
System.err.println(accept+"连接了");
}
}
1
2
3
4
5
6
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 3000);
System.err.println(socket);
}
}

收发数字

1
2
3
4
5
6
7
8
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(3000);
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
System.err.println(inputStream.read());
}
}
1
2
3
4
5
6
7
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 3000);
OutputStream outputStream = socket.getOutputStream();
outputStream.write(100);
}
}

收发字符串

1
2
3
4
5
6
7
8
9
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(3000);
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
System.err.println(dataInputStream.readUTF());
}
}
1
2
3
4
5
6
7
8
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 3000);
OutputStream outputStream = socket.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
dataOutputStream.writeUTF("段友元");
}
}

控制台输入

1
2
3
4
5
6
7
8
9
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(3000);
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
System.err.println(dataInputStream.readUTF());
}
}
1
2
3
4
5
6
7
8
9
10
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 3000);
OutputStream outputStream = socket.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
Scanner scanner = new Scanner(System.in);
dataOutputStream.writeUTF(scanner.next());
}
}

服务器与客户端聊天实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(3000);
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
OutputStream outputStream = accept.getOutputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
while (true){
System.err.println(dataInputStream.readUTF());
Scanner scanner = new Scanner(System.in);
dataOutputStream.writeUTF("服务器:"+scanner.next());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 3000);
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
DataInputStream dataInputStream = new DataInputStream(inputStream);
while (true){
Scanner scanner = new Scanner(System.in);
dataOutputStream.writeUTF("客户端:"+scanner.next());
System.err.println(dataInputStream.readUTF());
}
}
}