以下是所需结果的屏幕截图:(服务器在顶部,左下角有一个客户端,右下角有一个客户端)
以下是该计划当前结果的屏幕截图: (顶部的服务器,左下角的一个客户端,右下角的一个客户端):
在上面的屏幕截图中,在左下角和右下角窗口中,命令行参数java ChatClient -l 4021 -p 3000
有两个数字(4021
和3000
)。数字4021
是顶部窗口(服务器)需要文件传输的内容,因此必须以某种方式将其发送到服务器,但不幸的是,发送此数字可能会导致某些内容被阻塞,这意味着{{{永远不会显示1}}表示某些内容必须出错,所以我将行What is your name?
放在了调试目的。
我的问题是:我可以对以下代码进行哪些更改,以便显示所需的屏幕截图输出而不是当前屏幕截图的输出(即System.out.println("this line in chatClient.java is before the inputLine.readLine() is called");
的输出, What is your name?
等)?
Enter your message
代码:
ChatClient.java
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);
}
}
}
代码:
ChatServer.java
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;
}
}
代码:
ClientThreads.java
我的问题是:我可以对上面列出的代码进行哪些更改,以获得所需的结果屏幕截图?