网络编程基本知识与实践(一)
基本理论知识:
网络通信:
网络:多台电脑通过网络完成信息传递,网络指的是多台电脑构成的环境。
网络基本元素:网卡、网线、IP、Port端口(每个使用网络传输的软件都需要一个通信端口)
计算机基本作用:接收数据、处理数据、发送数据
IP+Port封装成Socket对象
C/S模式:client客户端+server服务端模式
B/S模式:browser浏览器+server服务器模式(HTTP协议超文本传输协议)
服务器程序操作界面不属于网络通信开发
输入输出流要点:
1、 输出流发送信息时,使用\r\n换行结束,因为使用BufferedReader的readLine方法按行读取
2、 必须调用flush推送数据,输出流才能发送到另一端
3、 信息的最后必须要有明确的结束符,例如”@:end”
首先搭起来Server+Client基本架构
ServerMain.java
public class ServerMain {
public static void main(String[] args) {
Server server = new Server();
if (server.init()){
server.start();
}
//server.stop();
}
}
Server.java
public class Server {
private int port;
public Server() {
this.port = 8888;
}
public Server(int port) {
this.port = port;
}
public boolean init(){
System.out.println("初始化管理员用户,进而进行登陆验证");
return true;
}
public void start(){
try {
//创建对象,对象的属性和方法调用(API学习核心内容)
ServerSocket serverSocket = new ServerSocket(this.port);
PrintUtil.serverPrint("端口开放成功");
while (true){
PrintUtil.serverPrint("调用方法准备接受连接");
//每个客户端都有一个独立的空间
Socket client = serverSocket.accept();//接收客户端连接,一次客户端的连接套接字
System.out.println(client.getInetAddress());
StreamUtils streamUtils = new StreamUtils(client);
streamUtils.readAndprint();
PrintUtil.serverPrint("接收完数据");
streamUtils.write("Server");
PrintUtil.serverPrint("发送完数据");
streamUtils.closeStream();
}
} catch (IOException e) {
e.printStackTrace();
PrintUtil.serverPrint("端口开发失败");
}
}
public void stop(){
}
}
ClientMain.java
public class ClientMain {
public static void main(String[] args) {
// 创建客户端对象,并设置服务器端口号和地址
Client client = new Client("127.0.0.1",8888);
//启动客户端连接
client.connect();
//关闭客户端
}
}
Client.java
public class Client {
private String IP;
private int port;
public Client() {
this.IP="127.0.0.1";
this.port=8888;
}
public Client(String IP, int port) {
this.IP = IP;
this.port = port;
}
public void connect(){
try {
Socket client = new Socket(this.IP,this.port);
PrintUtil.clientPrint("服务器连接成功");
StreamUtils streamUtils = new StreamUtils(client);
streamUtils.write("Client");
PrintUtil.clientPrint("发送完完数据");
streamUtils.readAndprint();
PrintUtil.clientPrint("接受信息完毕!");
streamUtils.closeStream();
} catch (IOException e) {
e.printStackTrace();
}
}
}
工具辅助类:
public class PrintUtil {
public final static String NEXTLINE="\r\n";
public final static String END = "";
public static void serverPrint(String str){
System.out.println("&&&服务端&&&:"+str);
}
public static void clientPrint(String str){
System.out.println("&&&客户端&&&:"+str);
}
}
public class StreamUtils {
private Socket client;
public StreamUtils(Socket client) {
this.client = client;
}
public void readAndprint() throws IOException{
this.readAndprint(PrintUtil.END);
}
public void readAndprint(String endFlag) throws IOException{
InputStream in = client.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String msg = "";
while (!PrintUtil.END.equals(msg = br.readLine())) {
System.out.println(msg);
}
}
public void write(String str) throws IOException {
this.write(str,PrintUtil.NEXTLINE);
}
public void write(String str,String endFlag) throws IOException {
OutputStream out = client.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
bw.write(str + PrintUtil.NEXTLINE);
bw.write(endFlag);
bw.flush();
}
public void closeStream() throws IOException{
client.close();
}
}