首页 文章

Java Simple Client Server应用程序

提问于
浏览
1

对不起,我现在正在编码12个小时,现在我有一个“脑力劳动” .

我做了一个小型的Client Server程序 .

客户:

public void send(String send) {
    DataOutputStream out;
    Socket client;

        try {
            client = new Socket("192.168.0.138", port);
            out = new DataOutputStream(client.getOutputStream());
            out.writeChars(send + '\n');
            Thread.sleep(100L);
        } catch (IOException ex) {
            System.err.println("Can't connect to Server!");
        } catch (InterruptedException ex) {
            System.err.println("Cant sleep!");
        }
}

服务器:

public static void main(String[] args) throws IOException {
    int port = 5000;
    String cIn;
    System.out.println("Running on Port 5000");
    ServerSocket sock = new ServerSocket(port);
    Socket client;
    BufferedReader inFromClient;
    while (true) {
        client = sock.accept();
        inFromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
        cIn = inFromClient.readLine();
        System.out.println("" + cln);
    }
}

现在我的问题 . 当我输入新数据时,如何使我的字符串(数据)以循环方式发送到服务器 .

如果一个正常的while循环,我的字符串将永久发送到服务器 . 如果我改变我的字符串,那没关系 . 如果我改变我的String,我会认为新的String正在发送到服务器 .

我很抱歉我的英语不好 . 我希望你能理解 .

3 回答

  • 0

    如果我恢复: - 您需要一个控制台应用程序(或窗口/ awt / swing ...)或主要客户端应用程序,它采取字符串,有时更改此字符串 . - 此字符串必须由您的函数“send”连续发送,并使用最后一个字符串

    我建议你:

    1 - 修复循环(1秒,2秒,x秒?)

    2 - 使用共享变量(在关键部分或同步),您的主客户端应用程序会编写它,并在您需要时更改它,并且您的“发送”功能每x秒读取一次并发送它 .

    您的客户可能看起来像这样:

    // SHARED VARIABLE
    static String warning="";
    final static Object warning_sync=new Object();
    
    // Alert function 
    class Thread_alert extends Thread 
        {
        // YOUR CODE
        public void send(String send) {
            DataOutputStream out;
            Socket client;
            int port=80;
    
                try {
                    client = new Socket("192.168.0.138", port);
                    out = new DataOutputStream(client.getOutputStream());
                    out.writeChars(send + '\n');
                    Thread.sleep(100L);
                } catch (IOException ex) {
                    System.err.println("Can't connect to Server!");
                } catch (InterruptedException ex) {
                    System.err.println("Cant sleep!");
                }
        }
    
    
        public Thread_alert()
            {
            super();
            }
    
        public void run() 
            {
            while (true)
                {
                // WHAT YOU HAVE TO DO
    
                synchronized(warning_sync)
                    {
                    System.err.println("WARN: "+warning);
                    send(warning);
                    }
    
                // Sleep 5 seconds
                try {
                    Thread.sleep(5000);
                    } 
                    catch (InterruptedException e)
                        {
                        e.printStackTrace();
                        }
                }
                // while (true)
            }
            // public void run()
    }
    // class Thread_alert
    
    
    
    public void console_client () 
    {
    // START THE THREAD
    Thread_alert lethread=new Thread_alert();
    lethread.start();   
    
    // INPUT LOOP
     Scanner s = new Scanner(System.in);
     String line;
    
     while ((line=s.nextLine())!=null)
         {
        System.out.println("STRING:'"+line+"'"); 
    
        // Fix the warning
        synchronized(warning_sync)
            {
            warning=line;
            }
    
        // bonus
        // IF STOP: STOP
        if (warning.equals("STOP")) 
            {
            lethread.stop();
            break;
            }
         } 
        // while ((line=s.nextLine())!=null)
    
     // safe
     s.close();
    }
    
  • 0

    如何用一个新的线程发送数据,这个线程在循环中发送数据 . 当您输入一些新数据时,中断旧线程并启动一个新线程,依此类推?

  • 0

    我认为你需要在while循环结束时添加一个bufferedReader .

    while (true) {
        client = sock.accept();
        inFromClient = new BufferedReader(new  InputStreamReader(client.getInputStream()));
        cIn = inFromClient.readLine();
        System.out.println("" + cln);
        inFromClient.close() //add this
    }
    

相关问题