Java流读取和写入无法同时工作

问题描述 投票:-1回答:1

服务器代码-

public class ServerPro
{
    ServerSocket server;
    Socket socket;
    BufferedReader read;
    BufferedWriter write;
    String temp;
    ServerPro()
    {

        Thread thread1=new Thread("1st")
         {

           public void run()
           {
               try 
               {
                   do
                 {

                    temp=read.readLine();
                   System.out.println(temp);

                 }while(true);

               }
               catch(Exception e)
               {
                   System.out.print("Line 30 error     :"+e.toString());
               }

           }
         };
         Thread thread2=new Thread("2nd")
                 {
                     public void run()
                     {
                         try 
                           {
                             Scanner scan=new Scanner(System.in);



                               do
                             {


                                write.write(scan.next());
                                write.newLine();
                                write.flush();


                             }while(true);

                           }
                           catch(Exception e)
                           {
                               System.out.print("Line 53 error     :"+e.toString());
                           }
                     }
                 };
        try
        {

          server=new ServerSocket(5198);
          socket=server.accept();
          System.out.println("Connected ");
          read=new BufferedReader(new InputStreamReader(socket.getInputStream()));
          write=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

          thread1.start();
          thread2.start();
        }
        catch(Exception e)
        {
            System.out.println("Line 72 error     :"+e.toString());
        }

    }
    protected void close() {
        try
        {

            read.close();
            write.close();
            socket.close();
            server.close();
        }
        catch(Exception e)
        {
            System.out.print(e.toString());
        }


    }
    public static void main(String args[])
    {
        ServerPro obj=new ServerPro();
    }

客户代码-

public class ClientPro
{
    Socket socket;
    BufferedReader read;
    BufferedWriter write;
    String temp;
    ClientPro()
    {
        Thread thread1=new Thread("1st")
         {
            
           public void run()
           {
               try 
               {
                   do
                 {
                    temp=read.readLine();
                    System.out.print(temp);
                 }while(true);
                   close();
               }
               catch(Exception e)
               {
                   System.out.print(e.toString());
               }
            
           }
         };
         Thread thread2=new Thread("2nd")
                 {
                     public void run()
                     {
                         try 
                           {
                             Scanner scan=new Scanner(System.in);
                               do
                             {
                                
                                write.write(scan.nextLine());
                                write.newLine();
                                write.flush();
                                
                             }while(true);
                               close();
                           }
                           catch(Exception e)
                           {
                               System.out.print(e.toString());
                           }
                     }
                 };
        try
        {
          
          socket=new Socket("198.123.45.242",5198);
          read=new BufferedReader(new InputStreamReader(socket.getInputStream()));
          write=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
          thread1.start();
          thread2.start();
        }
        catch(Exception e)
        {
            System.out.print(e.toString());
        }

        
    }
    protected void close() {
        try
        {
            read.close();
            write.close();
            socket.close();
        }
        catch(Exception e)
        {
            System.out.print(e.toString());
        }
        

    }
    public static void main(String args[])
    {
        ClientPro obj=new ClientPro();
    }
}

上面的代码仅显示Connected并且不起作用。总而言之,两个程序都调用了两个线程,分别使用BufferedReader和BufferedWriter从流中读取和写入流。据我说,只有写流循环不起作用,但是从流循环读取根本不起作用。

注意:暂时不做,但是我打算继续聊天,直到客户端或服务器按下Exit为止。

java multithreading sockets bufferedreader bufferedwriter
1个回答
-1
投票

我正在更改此答案,您在做什么应该工作。看来您应该仔细一点。这是一个基于您的代码的示例。

import java.net.Socket;
import java.net.ServerSocket;
import java.io.*;

public class ChatSocket{
    Socket socket;
    BufferedReader read;
    BufferedWriter write;
    volatile boolean alive = true;
    public ChatSocket(Socket socket){
        this.socket = socket;
        try{
            read=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            write=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        } catch(Exception e){
            throw new RuntimeException(e);
        }
    }
    public void listen(){
        try{
            while(true){
                String s = read.readLine();
                System.out.println("read: " + s);
                if(s==null){
                    alive = false;
                    return;
                }
            }
        } catch(Exception e){
            throw new RuntimeException(e);
        } 
    }
    public void write(String s) throws Exception{
            write.write(s);
            write.newLine();
            write.flush();
    }
    public static void main(String[] args) throws Exception{
        ChatSocket cs;
        if("server".equals(args[0])){
            Socket s = new ServerSocket(9993).accept();
            System.out.println("accepted");
            cs = new ChatSocket(s);
            cs.write("Welcome to chat socket");
            new Thread( cs::listen ).start(); 
        } else{
            Socket s = new Socket("localhost", 9993);
            cs = new ChatSocket(s);
            new Thread( cs::listen ).start();
            cs.write("heard yah");
        }
        BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ));
        while(cs.alive){
            cs.write(reader.readLine());
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.