如何使这些客户端在发送端口号(java多线程套接字)后不阻塞输出

如何使这些客户端在发送端口号(java多线程套接字)后不阻塞输出,java,multithreading,sockets,Java,Multithreading,Sockets,以下是所需结果的屏幕截图:(服务器在顶部,一个客户端在左下角,一个客户端在右下角) * 以下是程序当前结果的屏幕截图: (服务器在顶部,一个客户端在左下角,一个客户端在右下角): 在上面的屏幕截图中,在左下角和右下角窗口中,命令行参数javachatclient-l4021-p3000有两个数字(4021和3000)。数字4021是顶部窗口(服务器)的内容需要进行文件传输,因此必须以某种方式将其发送到服务器,但不幸的是,发送此号码可能会导致某些内容被阻止,这意味着您的名字是什么?的正确输出从未显

以下是所需结果的屏幕截图:(服务器在顶部,一个客户端在左下角,一个客户端在右下角)

*

以下是程序当前结果的屏幕截图: (服务器在顶部,一个客户端在左下角,一个客户端在右下角):

在上面的屏幕截图中,在左下角和右下角窗口中,命令行参数
javachatclient-l4021-p3000
有两个数字(
4021
3000
)。数字
4021
是顶部窗口(服务器)的内容需要进行文件传输,因此必须以某种方式将其发送到服务器,但不幸的是,发送此号码可能会导致某些内容被阻止,这意味着
您的名字是什么?
的正确输出从未显示,这表明一定是出了问题,因此我放置了行
System.out.println(“chatClient.java中的这一行在调用inputLine.readLine()之前”);
用于调试

我的问题是:我可以对以下代码进行哪些更改,以允许显示所需屏幕截图的输出,而不是当前屏幕截图的输出(即,
您叫什么?
输入您的消息
等)

ChatClient.java
code:

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

public class ChatClient implements Runnable 
{
  public static int once = 0;
  private static Socket clientSocket = null;
  private static Socket fileSocket = null;
  private static PrintStream os = null;
  private static DataInputStream is = null;
  private static BufferedReader inputLine = null;
  private static boolean closed = false;
  public static String[] namesList = new String[10];
  public int iteration = 0;
  public static String[] responses = new String[50];
  public int responseCount = 0;
  public int filePort = 0;

  public static void main(String[] args)
  {

      int filePort = Integer.valueOf(args[1]);
     for(int i = 0; i < namesList.length; i++)
     {
         namesList[i] = "";
     }
     for(int j = 0; j < responses.length; j++)
     {

         responses[j] = "";
     }
    int portNumber = Integer.valueOf(args[3]);
    String host = "localhost";

    try 
    {

      clientSocket = new Socket(host, portNumber);

      os = new PrintStream(clientSocket.getOutputStream());
      is = new DataInputStream(clientSocket.getInputStream());
      inputLine = new BufferedReader(new InputStreamReader(System.in));
    } 
    catch (UnknownHostException e)
    {
      System.err.println("Don't know about host " + host);
    } catch (IOException e) 
    {
      System.err.println("Couldn't get I/O for the connection to the host "
          + host);
    }
    if (clientSocket != null && os != null && is != null)
    {
      try 
      {

        while (!closed)
        {
             if(once==0)
             {
                 System.out.println("this line in chatClient.java is before outputstream is created");
                  OutputStream os2 = clientSocket.getOutputStream();
                  OutputStreamWriter osw = new OutputStreamWriter(os2);
                  BufferedWriter bw = new BufferedWriter(osw);
                  bw.write("The file port is: `"+ filePort);
                  System.out.println("The file port is: `" + filePort);

                  once=3;
                  os.println("The file port is: `" + filePort);


                  os.println(inputLine.readLine() );


             }
                once=3;
        }
        new Thread(new ChatClient()).start();

        os.close();
        is.close();
        clientSocket.close();
      } catch (IOException e)
      {
        System.err.println("IOException:  " + e);
      }
    }
  }
@SuppressWarnings("deprecation")
public void run()
{
    String responseLine = "";
    try 
    {
      while ((responseLine = is.readLine()) != null)
      {
          if(responseLine.equals("x") || responseLine.equals("X"))
          {
              System.exit(0);
          }
          if(responseLine.equals("Who owns the file?") && !responseLine.isEmpty() && responseLine != null)
          {
              responseCount++;
              System.out.println(responseLine);
              responses[responseCount] = "234782375920192831";

          }
          if(responseLine.equals("Which file do you want?"))
          {
              responseCount++;
              System.out.println(responseLine);
              responses[responseCount] = responseLine;

          }
          if(responseLine.equals("What is your name?"))
          {
              responseCount++;
              System.out.println(responseLine);
              responses[responseCount] = responseLine;

          }
          if(responseLine.equals("m") || responseLine.equals("M"))
          {
              responseCount++;
              System.out.println("Enter your message: ");
              responses[responseCount] = responseLine;

          }
          if(responseLine.equals("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n"))
          {

              responseCount++;
              System.out.println(responseLine);  
              responses[responseCount] = responseLine;

          }
          if(responseLine != null && !responseLine.isEmpty() && !responseLine.equals("What is your name?") && !responseLine.equals("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n") && !responseLine.equals("Who owns the file?") && !responseLine.equals("Which file do you want?"))
          {
              responseCount++;
              if(responses[responseCount-1].equals("234782375920192831"))
              {
                  namesList[iteration] = responseLine;
                  iteration++;
              }
              else
              {
                  System.out.println(responseLine);  
                  responses[responseCount] = responseLine;
              }
          }
      }
      closed = true;
    } 
    catch (IOException e)
    {
      System.err.println("IOException:  " + e);
    }
  }
}
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer
{
  public static List<String> listName = Collections.synchronizedList(new ArrayList<String>());
  List<String> l = Collections.synchronizedList(new ArrayList<String>());
  public static ServerSocket serverSocket = null;
  public static ServerSocket fileSocket = null;
  public static Socket clientSocket = null;
  public static final int maxClientsCount = 10;
  public static final ClientThreads[] threads = new ClientThreads[maxClientsCount];
  public static String[] namesList = new String[10];
 // public ChatClient arrayOfNames = new ChatClient;
  public static int filePort = 0;

  public static void main(String args[])
  {
      synchronized (listName)
        {
            //Iterator i = listName.iterator(); // Must be in synchronized block

             Iterator<String> i = listName.listIterator();
        }
      if(args.length <3)
      {
          int portNumber = Integer.valueOf(args[1]).intValue();
          System.out.println("waiting for connections on port " + portNumber + " ...\n ");
      }
    try 
    {

      int portNumber1 = Integer.valueOf(args[1]).intValue();
      serverSocket = new ServerSocket(portNumber1);
    } 
    catch (IOException e)
    {
      System.out.println(e);
    }

    while (true) 
    {
      try
      {
        clientSocket = serverSocket.accept();
        int i = 0;



        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String something;
        System.out.println("this line is executed in chatserver.java  before in.readLine()");

        something = in.readLine();
        String string = something;
        String[] parts = string.split("`");
        String part1 = parts[0];
        String part2 = parts[1]; 
        System.out.println("The file port is: " + part2);
        filePort = strToInt(part2);
        for (i = 0; i < maxClientsCount; i++)
        {
            if (threads[i] == null)
            {
                String name = "";
                (threads[i] = new ClientThreads(clientSocket, threads, name, namesList, listName, filePort)).start();
                break;
            }
        }

        if (i == maxClientsCount)
        {
          PrintStream os = new PrintStream(clientSocket.getOutputStream());
          os.println("Server too busy. Try later.");
          os.close();
          clientSocket.close();
        }
      }
      catch (IOException e) 
      {
        System.out.println(e);
      }
    }
  }
  public static int strToInt( String str )
  {
     int i = 0;
     int num = 0;
     boolean isNeg = false;

     //Check for negative sign; if it's there, set the isNeg flag
     if (str.charAt(0) == '-') {
         isNeg = true;
         i = 1;
     }

     //Process each character of the string;
     while( i < str.length()) {
         num *= 10;
         num += str.charAt(i++) - '0'; //Minus the ASCII code of '0' to get the value of the charAt(i++).
     }

     if (isNeg)
         num = -num;
     return num;
 }

}
import java.io.*;
import java.net.*;
import java.util.*;
class ClientThreads extends Thread
{ 
  public int once = 0;
  public String name1 = "";
  private String clientName = null;
  private DataInputStream is = null;
  private PrintStream os = null;
  private Socket clientSocket = null;
  private final ClientThreads[] threads;
  private int maxClientsCount;
  public int position = 0;
  public int filePort = 0;
  public static List<String> listName = Collections.synchronizedList(new ArrayList<String>());
  List<String> l = Collections.synchronizedList(new ArrayList<String>());
  public String[] namesList = new String[10];
  public ClientThreads(Socket clientSocket, ClientThreads[] threads, 
          String name, String[] namesList, List<String> listName, int filePort)
  {
    this.clientSocket = clientSocket;
    this.threads = threads;
    maxClientsCount = threads.length;
    this.name1 = name;
    this.namesList = namesList;
    this.filePort = filePort;

  }



  @SuppressWarnings("deprecation")
public void run() 
  {
    int maxClientsCount = this.maxClientsCount;
    ClientThreads[] threads = this.threads;
    synchronized (listName)
    {
        //Iterator i = listName.iterator(); // Must be in synchronized block

         ListIterator<String> i = listName.listIterator();
    }
    try 
    {
     if(once==0)
     {
          OutputStream os2 = clientSocket.getOutputStream();
          OutputStreamWriter osw = new OutputStreamWriter(os2);
          BufferedWriter bw = new BufferedWriter(osw);
          once=3;
     }
        once=3;
      System.out.println("This line is executed before inputstream and output stream are created");
      is = new DataInputStream(clientSocket.getInputStream());
      os = new PrintStream(clientSocket.getOutputStream());
      String name;
      while (true) 
      {
        os.println("What is your name?");
        name = is.readLine().trim();


          break;
      }
      if(listName != null)
      {
          synchronized(listName)
          {
              if(!listName.contains(name))
              {
                  listName.add(name);
                  os.println("The name " + name + " has been added to the list. "
                    + "The list now contains " + listName);
              }

          }
      }
      synchronized (this)
      {
        for (int i = 0; i < maxClientsCount; i++)
        {
          if (threads[i] != null && threads[i] == this)
          {
            clientName = "@" + name;
            break;
          }
        }
        for (int i = 0; i < maxClientsCount; i++)
        {
          if (threads[i] != null)
          {
            threads[i].os.println(name + " has connected.");
            os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");
          }
        }
      }
      while (true)
      {
        synchronized(listName)
        {
            if(!listName.contains(name))
            {
                listName.add(name);
                os.println("The name " + name + " has been added to the list. "
                        + "The list now contains " + listName);
            }

        }
        String line = is.readLine();
        if (line.startsWith("/quit")) 
        {
            break;
        }
        else
        {
            //os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");
        }

        if(line.equals("x") || line.equals("X"))
        {
            os.println("x");
            System.exit(0);
        }
        if(line.equals("m") || line.equals("M"))
        {
          os.println("Enter your message: ");
        }
        if(line.equals("f") || line.equals("F"))
        {
          os.println("Who owns the file?");
          boolean keep = true;
          while (keep == true)
          {
            String fileOwner = is.readLine();

            if(fileOwner !=null && !fileOwner.isEmpty())
            {
                os.println(fileOwner);
                System.out.println(fileOwner);
                System.out.println();
                //namesList[position] = fileOwner;

            //  os.println("The client connected name: " + fileOwner + 
                //      " has been added to position " + position +
                //      " in the namesList array " + namesList[position]);
                //position++;





                keep = false;
                os.println("Which file do you want?");
                boolean keep2 = true;
                while(keep2==true)
                {
                  String filename = is.readLine();
                  if(filename !=null && !filename.isEmpty())
                  {
                     // os.println(filename);
                      keep2 = false;
                      os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");

                  }
                }

            }
          }
        }
        if (line.startsWith("@")) 
        {
          String[] words = line.split("\\s", 2);
          if (words.length > 1 && words[1] != null) 
          {
            words[1] = words[1].trim();
            if (!words[1].isEmpty())
            {
              synchronized (this)
              {
                for (int i = 0; i < maxClientsCount; i++)
                {
                  if (threads[i] != null && threads[i] != this  && threads[i].clientName != null && threads[i].clientName.equals(words[0])) 
                  {
                      threads[i].os.println(name + ": " + words[1]);

                    this.os.println(name + ": " + words[1]);
                    this.os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");

                    break;
                  }
                }
              }
            }
          }
        }
        else 
        {
          synchronized (this)
          {
            for (int i = 0; i < maxClientsCount; i++) 
            {
              if (!line.equals("x") && !line.equals("X") && !line.equals("f") && !line.equals("F") && !line.equals("m") && !line.equals("M") && threads[i] != null && threads[i].clientName != null && !threads[i].clientName.equals("m") && !threads[i].clientName.equals("M")) 
              {
                threads[i].os.println(name + ": " + line + "\n");
                this.os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");
              }
            }
          }
        }
      }
      synchronized (this)
      {
        for (int i = 0; i < maxClientsCount; i++)
        {
          if (threads[i] != null && threads[i] != this && threads[i].clientName != null)
          {
            threads[i].os.println(name + "has disconnected.");
          }
        }
      }
      synchronized (this)
      {
        for (int i = 0; i < maxClientsCount; i++)
        {
          if (threads[i] == this) 
          {
            threads[i] = null;
          }
        }
      }
      is.close();
      os.close();
      clientSocket.close();
    } catch (IOException e) 
    {
    }
  }
}
ClientThreads.java
code:

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

public class ChatClient implements Runnable 
{
  public static int once = 0;
  private static Socket clientSocket = null;
  private static Socket fileSocket = null;
  private static PrintStream os = null;
  private static DataInputStream is = null;
  private static BufferedReader inputLine = null;
  private static boolean closed = false;
  public static String[] namesList = new String[10];
  public int iteration = 0;
  public static String[] responses = new String[50];
  public int responseCount = 0;
  public int filePort = 0;

  public static void main(String[] args)
  {

      int filePort = Integer.valueOf(args[1]);
     for(int i = 0; i < namesList.length; i++)
     {
         namesList[i] = "";
     }
     for(int j = 0; j < responses.length; j++)
     {

         responses[j] = "";
     }
    int portNumber = Integer.valueOf(args[3]);
    String host = "localhost";

    try 
    {

      clientSocket = new Socket(host, portNumber);

      os = new PrintStream(clientSocket.getOutputStream());
      is = new DataInputStream(clientSocket.getInputStream());
      inputLine = new BufferedReader(new InputStreamReader(System.in));
    } 
    catch (UnknownHostException e)
    {
      System.err.println("Don't know about host " + host);
    } catch (IOException e) 
    {
      System.err.println("Couldn't get I/O for the connection to the host "
          + host);
    }
    if (clientSocket != null && os != null && is != null)
    {
      try 
      {

        while (!closed)
        {
             if(once==0)
             {
                 System.out.println("this line in chatClient.java is before outputstream is created");
                  OutputStream os2 = clientSocket.getOutputStream();
                  OutputStreamWriter osw = new OutputStreamWriter(os2);
                  BufferedWriter bw = new BufferedWriter(osw);
                  bw.write("The file port is: `"+ filePort);
                  System.out.println("The file port is: `" + filePort);

                  once=3;
                  os.println("The file port is: `" + filePort);


                  os.println(inputLine.readLine() );


             }
                once=3;
        }
        new Thread(new ChatClient()).start();

        os.close();
        is.close();
        clientSocket.close();
      } catch (IOException e)
      {
        System.err.println("IOException:  " + e);
      }
    }
  }
@SuppressWarnings("deprecation")
public void run()
{
    String responseLine = "";
    try 
    {
      while ((responseLine = is.readLine()) != null)
      {
          if(responseLine.equals("x") || responseLine.equals("X"))
          {
              System.exit(0);
          }
          if(responseLine.equals("Who owns the file?") && !responseLine.isEmpty() && responseLine != null)
          {
              responseCount++;
              System.out.println(responseLine);
              responses[responseCount] = "234782375920192831";

          }
          if(responseLine.equals("Which file do you want?"))
          {
              responseCount++;
              System.out.println(responseLine);
              responses[responseCount] = responseLine;

          }
          if(responseLine.equals("What is your name?"))
          {
              responseCount++;
              System.out.println(responseLine);
              responses[responseCount] = responseLine;

          }
          if(responseLine.equals("m") || responseLine.equals("M"))
          {
              responseCount++;
              System.out.println("Enter your message: ");
              responses[responseCount] = responseLine;

          }
          if(responseLine.equals("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n"))
          {

              responseCount++;
              System.out.println(responseLine);  
              responses[responseCount] = responseLine;

          }
          if(responseLine != null && !responseLine.isEmpty() && !responseLine.equals("What is your name?") && !responseLine.equals("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n") && !responseLine.equals("Who owns the file?") && !responseLine.equals("Which file do you want?"))
          {
              responseCount++;
              if(responses[responseCount-1].equals("234782375920192831"))
              {
                  namesList[iteration] = responseLine;
                  iteration++;
              }
              else
              {
                  System.out.println(responseLine);  
                  responses[responseCount] = responseLine;
              }
          }
      }
      closed = true;
    } 
    catch (IOException e)
    {
      System.err.println("IOException:  " + e);
    }
  }
}
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer
{
  public static List<String> listName = Collections.synchronizedList(new ArrayList<String>());
  List<String> l = Collections.synchronizedList(new ArrayList<String>());
  public static ServerSocket serverSocket = null;
  public static ServerSocket fileSocket = null;
  public static Socket clientSocket = null;
  public static final int maxClientsCount = 10;
  public static final ClientThreads[] threads = new ClientThreads[maxClientsCount];
  public static String[] namesList = new String[10];
 // public ChatClient arrayOfNames = new ChatClient;
  public static int filePort = 0;

  public static void main(String args[])
  {
      synchronized (listName)
        {
            //Iterator i = listName.iterator(); // Must be in synchronized block

             Iterator<String> i = listName.listIterator();
        }
      if(args.length <3)
      {
          int portNumber = Integer.valueOf(args[1]).intValue();
          System.out.println("waiting for connections on port " + portNumber + " ...\n ");
      }
    try 
    {

      int portNumber1 = Integer.valueOf(args[1]).intValue();
      serverSocket = new ServerSocket(portNumber1);
    } 
    catch (IOException e)
    {
      System.out.println(e);
    }

    while (true) 
    {
      try
      {
        clientSocket = serverSocket.accept();
        int i = 0;



        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String something;
        System.out.println("this line is executed in chatserver.java  before in.readLine()");

        something = in.readLine();
        String string = something;
        String[] parts = string.split("`");
        String part1 = parts[0];
        String part2 = parts[1]; 
        System.out.println("The file port is: " + part2);
        filePort = strToInt(part2);
        for (i = 0; i < maxClientsCount; i++)
        {
            if (threads[i] == null)
            {
                String name = "";
                (threads[i] = new ClientThreads(clientSocket, threads, name, namesList, listName, filePort)).start();
                break;
            }
        }

        if (i == maxClientsCount)
        {
          PrintStream os = new PrintStream(clientSocket.getOutputStream());
          os.println("Server too busy. Try later.");
          os.close();
          clientSocket.close();
        }
      }
      catch (IOException e) 
      {
        System.out.println(e);
      }
    }
  }
  public static int strToInt( String str )
  {
     int i = 0;
     int num = 0;
     boolean isNeg = false;

     //Check for negative sign; if it's there, set the isNeg flag
     if (str.charAt(0) == '-') {
         isNeg = true;
         i = 1;
     }

     //Process each character of the string;
     while( i < str.length()) {
         num *= 10;
         num += str.charAt(i++) - '0'; //Minus the ASCII code of '0' to get the value of the charAt(i++).
     }

     if (isNeg)
         num = -num;
     return num;
 }

}
import java.io.*;
import java.net.*;
import java.util.*;
class ClientThreads extends Thread
{ 
  public int once = 0;
  public String name1 = "";
  private String clientName = null;
  private DataInputStream is = null;
  private PrintStream os = null;
  private Socket clientSocket = null;
  private final ClientThreads[] threads;
  private int maxClientsCount;
  public int position = 0;
  public int filePort = 0;
  public static List<String> listName = Collections.synchronizedList(new ArrayList<String>());
  List<String> l = Collections.synchronizedList(new ArrayList<String>());
  public String[] namesList = new String[10];
  public ClientThreads(Socket clientSocket, ClientThreads[] threads, 
          String name, String[] namesList, List<String> listName, int filePort)
  {
    this.clientSocket = clientSocket;
    this.threads = threads;
    maxClientsCount = threads.length;
    this.name1 = name;
    this.namesList = namesList;
    this.filePort = filePort;

  }



  @SuppressWarnings("deprecation")
public void run() 
  {
    int maxClientsCount = this.maxClientsCount;
    ClientThreads[] threads = this.threads;
    synchronized (listName)
    {
        //Iterator i = listName.iterator(); // Must be in synchronized block

         ListIterator<String> i = listName.listIterator();
    }
    try 
    {
     if(once==0)
     {
          OutputStream os2 = clientSocket.getOutputStream();
          OutputStreamWriter osw = new OutputStreamWriter(os2);
          BufferedWriter bw = new BufferedWriter(osw);
          once=3;
     }
        once=3;
      System.out.println("This line is executed before inputstream and output stream are created");
      is = new DataInputStream(clientSocket.getInputStream());
      os = new PrintStream(clientSocket.getOutputStream());
      String name;
      while (true) 
      {
        os.println("What is your name?");
        name = is.readLine().trim();


          break;
      }
      if(listName != null)
      {
          synchronized(listName)
          {
              if(!listName.contains(name))
              {
                  listName.add(name);
                  os.println("The name " + name + " has been added to the list. "
                    + "The list now contains " + listName);
              }

          }
      }
      synchronized (this)
      {
        for (int i = 0; i < maxClientsCount; i++)
        {
          if (threads[i] != null && threads[i] == this)
          {
            clientName = "@" + name;
            break;
          }
        }
        for (int i = 0; i < maxClientsCount; i++)
        {
          if (threads[i] != null)
          {
            threads[i].os.println(name + " has connected.");
            os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");
          }
        }
      }
      while (true)
      {
        synchronized(listName)
        {
            if(!listName.contains(name))
            {
                listName.add(name);
                os.println("The name " + name + " has been added to the list. "
                        + "The list now contains " + listName);
            }

        }
        String line = is.readLine();
        if (line.startsWith("/quit")) 
        {
            break;
        }
        else
        {
            //os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");
        }

        if(line.equals("x") || line.equals("X"))
        {
            os.println("x");
            System.exit(0);
        }
        if(line.equals("m") || line.equals("M"))
        {
          os.println("Enter your message: ");
        }
        if(line.equals("f") || line.equals("F"))
        {
          os.println("Who owns the file?");
          boolean keep = true;
          while (keep == true)
          {
            String fileOwner = is.readLine();

            if(fileOwner !=null && !fileOwner.isEmpty())
            {
                os.println(fileOwner);
                System.out.println(fileOwner);
                System.out.println();
                //namesList[position] = fileOwner;

            //  os.println("The client connected name: " + fileOwner + 
                //      " has been added to position " + position +
                //      " in the namesList array " + namesList[position]);
                //position++;





                keep = false;
                os.println("Which file do you want?");
                boolean keep2 = true;
                while(keep2==true)
                {
                  String filename = is.readLine();
                  if(filename !=null && !filename.isEmpty())
                  {
                     // os.println(filename);
                      keep2 = false;
                      os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");

                  }
                }

            }
          }
        }
        if (line.startsWith("@")) 
        {
          String[] words = line.split("\\s", 2);
          if (words.length > 1 && words[1] != null) 
          {
            words[1] = words[1].trim();
            if (!words[1].isEmpty())
            {
              synchronized (this)
              {
                for (int i = 0; i < maxClientsCount; i++)
                {
                  if (threads[i] != null && threads[i] != this  && threads[i].clientName != null && threads[i].clientName.equals(words[0])) 
                  {
                      threads[i].os.println(name + ": " + words[1]);

                    this.os.println(name + ": " + words[1]);
                    this.os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");

                    break;
                  }
                }
              }
            }
          }
        }
        else 
        {
          synchronized (this)
          {
            for (int i = 0; i < maxClientsCount; i++) 
            {
              if (!line.equals("x") && !line.equals("X") && !line.equals("f") && !line.equals("F") && !line.equals("m") && !line.equals("M") && threads[i] != null && threads[i].clientName != null && !threads[i].clientName.equals("m") && !threads[i].clientName.equals("M")) 
              {
                threads[i].os.println(name + ": " + line + "\n");
                this.os.println("Enter an option: 'm' = message, 'f' = file request, 'x' = exit\n");
              }
            }
          }
        }
      }
      synchronized (this)
      {
        for (int i = 0; i < maxClientsCount; i++)
        {
          if (threads[i] != null && threads[i] != this && threads[i].clientName != null)
          {
            threads[i].os.println(name + "has disconnected.");
          }
        }
      }
      synchronized (this)
      {
        for (int i = 0; i < maxClientsCount; i++)
        {
          if (threads[i] == this) 
          {
            threads[i] = null;
          }
        }
      }
      is.close();
      os.close();
      clientSocket.close();
    } catch (IOException e) 
    {
    }
  }
}
import java.io.*;
导入java.net。*;
导入java.util.*;
类ClientThreads扩展线程
{ 
公共int一次=0;
公共字符串名称1=“”;
私有字符串clientName=null;
私有DataInputStream为空;
私有PrintStream os=null;
私有套接字clientSocket=null;
私有最终客户端线程[]个线程;
私有int MaxClientScont;
公共int位置=0;
公共int filePort=0;
public static List listName=Collections.synchronizedList(新的ArrayList());
List l=Collections.synchronizedList(新的ArrayList());
公共字符串[]名称列表=新字符串[10];
公共ClientThreads(套接字clientSocket,ClientThreads[]线程,
字符串名称,字符串[]名称列表,列表名称,int文件端口)
{
this.clientSocket=clientSocket;
this.threads=线程;
MaxClientScont=threads.length;
this.name1=名称;
this.namesList=namesList;
this.filePort=filePort;
}
@抑制警告(“弃用”)
公开募捐
{
int MaxClientScont=this.MaxClientScont;
ClientThreads[]threads=this.threads;
已同步(列表名)
{
//迭代器i=listName.Iterator();//必须在同步块中
ListIterator i=listName.ListIterator();
}
尝试
{
如果(一次==0)
{
OutputStream os2=clientSocket.getOutputStream();
OutputStreamWriter osw=新的OutputStreamWriter(os2);
BufferedWriter bw=新的BufferedWriter(osw);
一次=3;
}
一次=3;
System.out.println(“在创建inputstream和output stream之前执行此行”);
is=新的DataInputStream(clientSocket.getInputStream());
os=新的打印流(clientSocket.getOutputStream());
字符串名;
while(true)
{
println(“你叫什么名字?”);
name=is.readLine().trim();
打破
}
if(listName!=null)
{
已同步(列表名)
{
如果(!listName.contains(name))
{
listName.add(name);
println(“名称“+name+”已添加到列表中。”
+“列表现在包含”+列表名);
}
}
}
已同步(此)
{
对于(int i=0;i