标签:键盘输入 获取数据 sock 经典 服务 flag closeable 面向 bye
网络编程:进行服务器端与客户端编程的开发操作实现。
java.net:网络操作包
客户端/服务器模式(Client / Server)
C/S的开发非常的繁琐,因为开发人员需要维护两套程序。(运维哭)
优点:安全性能高;使用的是自己的连接端口、通信协议
public class ServerSocket
extends Object
implements Closeable
public ServerSocket(int port) throws IOException;
参数:port ==> 设置监听信息包的端口号
public Socket accept() throws IOException;
public OutputStream getOutputStream() throws IOException;
public class Socket
extends Object
implements Closeable
public Socket(String host , int port);
连接主机的IP或域名(本机:localhost/127.0.0.1),并定义通信端口号port
套接字(也可以就叫“套接字”)。套接字是两台机器间通信的端点。
public InputStream getInputStream();
public OutputStream getOutputStream() throws IOException;
套接字的输入输出流:在客户端套接字的输入流获取服务端的套接字的输出流
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
// c/s ==> 服务端
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(1024);// 端口号 port
// 连接客户端
System.out.println(">>> 等待客户端连接 ... <<<");
Socket client = server.accept();//等待客户端发来连接请求
PrintStream out = new PrintStream(client.getOutputStream());
// 利用打印流完成输出,向客户端输出信息;
// client.getOutputStream()返回的是输出流的对象
out.println("Hello,World!");
out.close();
client.close();
server.close();
}
}
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
// c/s ==> 客户端
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket client = new Socket("localhost",1024); // 连接服务端
// 取得客户端的输入数据流对象 ==> 接收来自服务器端的输出信息(流)
Scanner scan = new Scanner(client.getInputStream());
// client.getInputStream()返回的是输入流的对象
scan.useDelimiter("\n");
if (scan.hasNext()) {
System.out.println(scan.next());
}
scan.close();
client.close();
}
}
客户端随意的向服务器端输送信息,服务端接收信息后,在信息前面加上 “ECHO” 标记后返回。
public class EchoServer {
public static void main(String [] args) throws Exception{
ServerSocket server = new ServerSocket(512);
Socket client = server.accept();//接收客户端的连接
Scanner scan = new Scanner(client.getInputStream());//接收套接字输入流(对象)
PrintStream out = new PrintStream(client.getOutputStream());//设置套接字输出流(对象)
scan.useDelimiter("\n");
System.out.println(">>> 连接准备 <<<");
boolean flag = true ; //控制连接的结束
while (flag) {
if (scan.hasNext()) {
String str = scan.next() ; //客户端发送的内容
if (str.equalsIgnoreCase("bye")) {
flag = false;
out.println("bye!");
} else {
out.println("ECHO:" + str);
}
}
}
client.close();
out.close();
server.close();
scan.close();
}
}
public class EchoClient {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket client = new Socket("localhost",512);
Scanner scan = new Scanner(client.getInputStream());
PrintStream out = new PrintStream(client.getOutputStream());
Scanner in = new Scanner(System.in);// 利用扫描流从键盘输入
in.useDelimiter("\n");
scan.useDelimiter("\n");
boolean flag = true;
while (flag) {
if (in.hasNext()) {
String str = in.next();//获取数据(输入数据)
out.println(str);
if(str.equalsIgnoreCase("bye")) {
flag = false ;
}
if(scan.hasNext()) {
System.out.println(scan.next());
}
}
}
client.close();
scan.close();
out.close();
in.close();
}
}
服务开发的基础要素:网络支持类、IO、多线程
标签:键盘输入 获取数据 sock 经典 服务 flag closeable 面向 bye
原文地址:https://www.cnblogs.com/wangyuyang1016/p/11197099.html