网络编程基本知识与实践(一)

本文介绍了网络编程的基本概念,包括网络通信原理、C/S与B/S模式的区别,并通过Java代码实例演示了如何搭建简单的Server与Client应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网络编程基本知识与实践(一)

基本理论知识:
网络通信:
网络:多台电脑通过网络完成信息传递,网络指的是多台电脑构成的环境。
网络基本元素:网卡、网线、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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值