如何使namesList数组位置在多个客户端上相同(同步)? (java多线程套接字)

时间:2017-06-04 16:35:43

标签: java multithreading sockets

程序运行的屏幕截图(顶部是服务器,右下角是第一个客户端,左下角是第二个客户端):

Screenshot of program running

上面屏幕截图的问题是,左下方窗口User name: something has been added to position 0 in the namesList array上显示的消息应该是User name: something has been added to position 1 in the namesList array。位置需要为1而不是0的原因是因为名字应该是存储在第一个位置,第二个名称应存储在第二个位置(以使名称列表实际存储所有名称)

问题是使namesList数组和位置同步。

编辑:似乎threads[i]具有全局可访问性,是否有可能我应该使用对象创建文件名的arraylist以某种方式存储连接客户端的名称而不是常规字符串数组来存储它们?

我可以对以下代码进行哪些更改,以使数组同步并可供所有客户端访问?

ChatThreads.java代码:

import java.io.*;
import java.net.*;
import java.util.*;
class ClientThreads extends Thread
{
  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 static String[] namesList = new String[4];
  public int iteration = 0;
  public static String[] responses = new String[50];
  public int responseCount = 0;

  public ClientThreads(Socket clientSocket, ClientThreads[] threads, String name)
  {
    this.clientSocket = clientSocket;
    this.threads = threads;
    maxClientsCount = threads.length;
    this.name1 = name;
  }

  @SuppressWarnings("deprecation")
  public void run() 
  {
    int maxClientsCount = this.maxClientsCount;
    ClientThreads[] threads = this.threads;
    for(int i = 0; i < namesList.length; i++)
     {
         namesList[i] = "";
     }
     for(int j = 0; j < responses.length; j++)
     {
         responses[j] = "";
     }
    try 
    {
      is = new DataInputStream(clientSocket.getInputStream());
      os = new PrintStream(clientSocket.getOutputStream());
      String name;
      String firstName;
      while (true) 
      {
        os.println("What is your name?");
        name = is.readLine().trim();
          break;
      }
      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)
      {
        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())
            {
                responses[responseCount] = fileOwner + "`owns a file";
                responseCount++;
                os.println(fileOwner);
                namesList[iteration] = fileOwner;
                System.out.println("User named: " + fileOwner + " has been added to position " + iteration + " in the namesList array.");
                iteration++;
                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");
                  }
                }
            }
          }
        }
        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) 
    {
    }
  }
}

ChatClient.java代码:

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

public class ChatClient implements Runnable 
{

  private static Socket clientSocket = 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[4];
  public int iteration = 0;
  public static String[] responses = new String[50];
  public int responseCount = 0;
  public static void main(String[] args)
  {
     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";
    int filePort = Integer.valueOf(args[1]);
    try 
    {
      clientSocket = new Socket(host, portNumber);
      inputLine = new BufferedReader(new InputStreamReader(System.in));
      os = new PrintStream(clientSocket.getOutputStream());
      is = new DataInputStream(clientSocket.getInputStream());
    } 
    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 
      {

        new Thread(new ChatClient()).start();
        while (!closed)
        {
          os.println(inputLine.readLine() );
        }

        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;
                  System.out.println("User named: " + responseLine + " has been added to position " + iteration + " in the namesList array." );
                  iteration++;
              }
              else
              {
                  System.out.println(responseLine);  
                  responses[responseCount] = responseLine;
              }
          }
      }
      closed = true;
    } 
    catch (IOException e)
    {
      System.err.println("IOException:  " + e);
    }
  }
}

ChatServer.java代码

import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer
{

  public static ServerSocket serverSocket = null;
  public static Socket clientSocket = null;
  public static final int maxClientsCount = 10;
  public static final ClientThreads[] threads = new ClientThreads[maxClientsCount];

  public static void main(String args[])
  {

      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;

        for (i = 0; i < maxClientsCount; i++)
        {
            if (threads[i] == null)
            {
                String name = "";
                (threads[i] = new ClientThreads(clientSocket, threads, name)).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);
      }
    }
  }
}

ServerThread.java代码:

import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.IOException;
import java.net.Socket;
import java.net.ServerSocket;

public class ServerThread
{

  private static ServerSocket serverSocket = null;
  private static Socket clientSocket = null;
  private static final int maxClientsCount = 10;
  private static final ClientThreads[] threads = new ClientThreads[maxClientsCount];

  public static void main(String args[])
  {

     int portNumber = Integer.valueOf(args[1]).intValue();
     if(args.length <3)
     {
         System.out.println("waiting for conennections on port" + portNumber + " ...\n ");
     }
    try 
    {
      serverSocket = new ServerSocket(portNumber);
    }
    catch (IOException e)
    {
      System.out.println(e);
    }
    while (true) 
    {
      try
      {
        clientSocket = serverSocket.accept();
        int i = 0;
        for (i = 0; i < maxClientsCount; i++) 
        {
          if (threads[i] == null) 
          {
            String name = "";
            (threads[i] = new ClientThreads(clientSocket, threads, name)).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);
      }
    }
  }
}

我的问题是:如何在具有位置的多个客户端中使namesList数组相同?

0 个答案:

没有答案