• java网络编程 TCP程序


    在java中,使用Socket完成TCP(传输控制协议)程序开发,使用此类可以方便地建立可靠的,双向的,持续的,点对点通信连接

    服务端: ServerSocket    程序段:Socket

    ServerSocket 1、建立服务端 2、accept()方法建立Socket

    Socket 建立客户端指定要连接的服务端和连接端口

    实例1:创建TCP服务端与程序端   服务端向程序端发送 Hello

    服务端TCP:

    import java.io.*;
    import java.net.*;
    
    public class TestServer {
        public static void main(String[] args) throws Exception {
            ServerSocket server = new ServerSocket(8888);
            System.out.println("建立服务端");        
            Socket client = server.accept();
            System.out.println("客户端使用端口:" + client.getLocalPort());//
            OutputStream os = client.getOutputStream();
            PrintStream psteam = new PrintStream(os);
            psteam.println("hello,client"); 
            os.close();//关闭打印流
            client.close();//client是否需要关闭?@需要的
            server.close();//关闭服务端
        }    
    }

    客户端TCP:

    import java.io.*;
    import java.net.*;
    
    public class TestClient {
        public static void main(String[] args) throws Exception {
            Socket client = new Socket("192.168.0.121",8888);
            System.out.println("建立客户端,连入服务器");    
            System.out.println("服务端监听端口:" + client.getLocalPort());//getLocalPort方法取得使用端口
            InputStream is = client.getInputStream();
            BufferedReader bfreader = new BufferedReader(new InputStreamReader(is));
            String str = bfreader.readLine();
            System.out.println(str);
            bfreader.close();
            client.close();
        }    
    }

    结果:

    r1

    上面的服务端每次执行完毕后都会退出,先通过循环accept方法,使服务端在一个客户端执行完毕后,可以重新等待其他客户端连接:

    服务端TCP:

    import java.io.*;
    import java.net.*;
    
    public class TestServer {
        public static void main(String[] args) throws Exception {
            ServerSocket ser = new ServerSocket(8888);
            while(true) {
                boolean flag = true;
                BufferedReader bur = null;
                PrintStream prs = null;
                Socket client = ser.accept();
                System.out.println("有客户端连入");
                bur = new BufferedReader(
                                    new InputStreamReader(client.getInputStream()));    
                prs = new PrintStream(client.getOutputStream());
                String str = null;
                    while (flag) {
                        str = bur.readLine();
                        if (str == null) {
                            flag = false;    
                        }    else if (str.equalsIgnoreCase("exit")) {
                         flag = false;    
                        }else {
                            prs.println("Echo" +"-----"+ str);    
                        }
                    }
                    System.out.println("客户端退出");
                    bur.close();
                    prs.close();
                    client.close();
            }
            //ser.close();这一句不用写@错误:无法访问语句无法访问语句
        }    
    }

    客户端TCP:

    import java.io.*;
    import java.net.*;
    
    public class TestClient {
        public static void main(String[] args) throws Exception {
            Socket client = new Socket("192.168.0.121",8888);
            BufferedReader bur1 = new BufferedReader(new InputStreamReader(System.in));
            BufferedReader bur2 = new BufferedReader(new InputStreamReader(client.getInputStream()));
            PrintStream prs = new PrintStream(client.getOutputStream());
            boolean flag = true;
            String str1 = null;
            String str2 = null;
            System.out.println("输入信息: ");
            while (flag) {
                str1 = bur1.readLine();
                prs.println(str1);
                if (str1 == null || str1.equalsIgnoreCase("exit")) {
                    flag = false;    
                }    else {
                    str2 = bur2.readLine();
                    System.out.println(str2);    
                }
            }
            bur1.close();
            bur2.close();
            prs.close();
            client.close();
        }    
    }

    结果:

    r3

    上面程序可以实现循环客户端访问,但只能一个客户端进行访问,所以需要多线程

    主程序:

    import java.net.*;
    
    public class TestServer {
        public static void main(String[] args) throws Exception {
            ServerSocket server = new ServerSocket(8888);
            Socket client = null;//!!!!!!!!!!
            while (true) {
                client = server.accept();//出现重复定义了怎么办?变量名重复怎么办?在循环语句里面不要定义变量
                new Thread(new ThreadServer(client)).start();    
            }
        }    
    }

    服务端TCP:

    import java.io.*;
    import java.net.*;
    
    public class ThreadServer implements Runnable {
        private Socket client;
        
        public ThreadServer(Socket client) {
            this.client = client;    
        }    
        
        public void run() {
            try {
                PrintStream prs = new PrintStream(client.getOutputStream());
                BufferedReader bur = new BufferedReader(new InputStreamReader(client.getInputStream()));
                boolean flag = true;
                String str = null;
                System.out.println("客户端登入");
                while (flag) {
                    str = bur.readLine();
                    if (str == null || str.equalsIgnoreCase("exit")) {
                        flag = false;    
                    } else {
                        prs.println( "------" + "Echo" + str);    
                    }        
                }
                prs.close();
                bur.close();
                client.close();
                System.out.println("客户端登出");
            }catch (IOException e) {
                e.printStackTrace();    
            }
            
        }
    }

    结果:

    r4

  • 相关阅读:
    13、【Hive】hive在启动beeline客户端时报错:User: xxx is not allowed to impersonate xxx
    12、【Hive】使用JDBC方式访问HIve,启动客户端时报错java.lang.NoClassDefFoundError: org/apache/tez/dag/api/TezConfiguration
    11、【Hive】配置hive元数据访问后,使用hive时报错FAILED: HiveException java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.me
    10、【Hive】hive配置完hive.metastore.uris后报错
    使用Dell iDRAC服务器远程控制安装操作系统简要图解
    使用Windows命令行reg控制注册表键值
    注册表REG文件编写大全
    AD 复制状态检查
    Windows server 2012 利用ntdsutil工具实现AD角色转移及删除域控方法
    利用WSUS部署更新程序
  • 原文地址:https://www.cnblogs.com/yhwsy/p/5826438.html
Copyright © 2020-2023  润新知