网络编程 D8章

套接字概念

Java 提供了流 socket 和数据报 socket,流 socket 和数据报 socket 各自的作用?

流socket是TCP 数据报socket是UDP

UDP Socket 编程的步骤。

https://www.cnblogs.com/OfflineBoy/p/13873830.html

TCP Socket 编程的步骤。

https://www.cnblogs.com/OfflineBoy/p/13875871.html

TCP Socket 编程(问候)

服务器端程序:


import java.io.*;
import java.net.*;

public class Server {

    public static void main(String args[]) {
        ServerSocket server = null;//定义服务器端 socket
        Socket client = null;//定义用来连接用的 socket
        String s = null;
        DataInputStream in = null;
        DataOutputStream out = null;
        try {
            server = new ServerSocket(3456);
        } catch (IOException e1) {
            System.out.println("Error:" + e1);
        }
        try {
            client = server.accept();//创建 socket 连接,等待请求
            //创建 IO 数据流
            in = new DataInputStream(client.getInputStream());
            out = new DataOutputStream(client.getOutputStream());
            while (true) {
                s = in.readUTF();
                if (s != null) {
                    break;
                }
            }
            out.writeUTF("Hello,I am server.");
            client.close();
        } catch (IOException e) {
            System.out.println("Erro:" + e);
        }
        System.out.println(s);
    }
}

客户端程序:


import java.io.*;
import java.net.*;

public class Client {

    public static void main(String args[]) {
        String s = null;
        Socket mysocket;
        DataInputStream in = null;
        DataOutputStream out = null;
        try {
            mysocket = new Socket("localhost", 3456);
            in = new DataInputStream(mysocket.getInputStream());
            out = new DataOutputStream(mysocket.getOutputStream());
            out.writeUTF("hello!");
            while (true) {
                s = in.readUTF();
                if (s != null) {
                    break;
                }
            }
            mysocket.close();
        } catch (IOException e) {
            System.out.println("not connected");
        }
        System.out.println(s);
    }
}

TCP Socket 编程(简单聊天)

服务器:


import java.net.*;
import java.io.*;

class Server1 {

    static public void main(String args[]) throws IOException {
        ServerSocket server = null;
        Socket socket = null;
        InputStream is = null;
        OutputStream os = null;
        DataInputStream in = null;
        PrintStream out = null;
        try { //在端口 5000 注册服务
            server = new ServerSocket(5000);
            socket = server.accept();//侦听连接请求,等待连接
            System.out.println("************服务器端界面************");
            System.out.println("与客户端连接成功!");
            System.out.println("对话内容为:");
            System.out.println("等待客户发送信息.....");
            //获取对应的 Socket 的输入/输出流
            is = socket.getInputStream();
            os = socket.getOutputStream();
            //建立数据流
            in = new DataInputStream(is);
            out = new PrintStream(os);//表示向对方输出
            out.println("Welcome!");//表示向对方输出
            String str = in.readLine();//逐行读取
            do {
                System.out.println("客户端说:" + str);
                str = in.readLine();
            } while (!str.trim().equals("BYE")); //如果是“BYE”就退出
            System.out.println("客户想要离开");
        } catch (Exception e) //捕获程序异常
        {
            System.out.println("Error:" + e);
        } finally {
            is.close();//关闭输入流
            os.close();//关闭输出流
            in.close();//关闭数据输入流
            socket.close();//关闭 socket
        }
    }
}

客户端:


import java.net.*;
import java.io.*;

class Client1 {

    static public void main(String args[]) throws IOException {
        Socket socket = null;
        InputStream is = null;
        OutputStream os = null;
        DataInputStream in = null;
        PrintStream out = null;
        String d = null;
        try { //向本地服务器申请链接
            //注意端口号要与服务器保持一致:5000
            socket = new Socket("localhost", 5000);
            System.out.println("***********客户端界面*************");
            System.out.println("与服务器连接成功!");
            System.out.println("");
            System.out.println("对话内容为:");
            System.out.println("");
            //获取对应的 Socket 的输入/输出流
            is = socket.getInputStream();
            os = socket.getOutputStream();
            //建立数据流
            in = new DataInputStream(is);
            out = new PrintStream(os);
            d = in.readLine();
            System.out.println("服务器说:" + d);
            byte a[] = new byte[100];
            System.out.println("请输入信息:");
            System.in.read(a);
            String b = new String(a, 0);
            b = b.trim();//去掉输入的第一个字符前的空格
            while (!b.equals("BYE")) {
                //如果输入的不是“BYE”,就向对方输出
                out.println(b);
                System.in.read(a);
                b = new String(a, 0);
                b = b.trim();
            }
            out.println(b);
        } catch (Exception e)//捕获程序异常
        {
            System.out.println("Error:" + e);
        } finally {
            is.close();//关闭输入流
            os.close();//关闭输出流
            socket.close();//关闭 socket
        }
    }
}
posted @ 2021-06-15 21:41  一个经常掉线的人  阅读(61)  评论(0)    收藏  举报