我有以下问题....
try
{
clientInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
clientOutput = new BufferedWriter(new PrintWriter(clientSocket.getOutputStream()));
while(true)
{
String clientRequest = "";
String tempStr = clientInput.readLine();
while(tempStr != null && !tempStr.equals("null"))
{
System.out.println(tempStr);
clientRequest += tempStr + " ";
tempStr = clientInput.readLine();
}
//Parse Request
ArrayList<String> tokenArray = parseRequest(clientRequest);
Calendar c = Calendar.getInstance();
switch(tokenArray.get(0))
{
case "GET":
{
clientOutput.write("HTTP/1.1 200 OK\r\n");
clientOutput.write("Date: " + c.getDisplayName(0, Calendar.LONG, Locale.UK).toString() + "\r\n");
clientOutput.write("Server: Java HTTP Server v1.0\r\n");
clientOutput.flush();
break;
//Write File
}
default:
{
clientOutput.write("500\r\n");
clientOutput.flush();
}
}
}
}
每件事都完全正常,直到上clientOutput.write("HTTP.......
线,客户只是一直等待和等待......我试图在每次连续写入后刷新,但什么也没有......但这是奇怪的部分 - 如果我在代码进入while循环之前写入和刷新,case "GET":
完美地写入作品......即
代码确实一直执行到
clientOutput.flush();
break;
//Write File
.
try
{
clientInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
clientOutput = new BufferedWriter(new PrintWriter(clientSocket.getOutputStream()));
clientOutput.write("HTTP/1.1 200 OK\r\n");
clientOutput.flush();
while(true)
{
String clientRequest = "";
String tempStr = clientInput.readLine();
while(tempStr != null && !tempStr.equals("null"))
{
System.out.println(tempStr);
clientRequest += tempStr + " ";
tempStr = clientInput.readLine();
}
//Parse Request
ArrayList<String> tokenArray = parseRequest(clientRequest);
Calendar c = Calendar.getInstance();
switch(tokenArray.get(0))
{
case "GET":
{
clientOutput.write("HTTP/1.1 200 OK\r\n");
clientOutput.write("Date: " + c.getDisplayName(0, Calendar.LONG, Locale.UK).toString() + "\r\n");
clientOutput.write("Server: Java HTTP Server v1.0\r\n");
clientOutput.flush();
break;
//Write File
}
这是客户端的代码
Socket s = new Socket("localhost", 1337);
BufferedReader fromServer = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter toServer = new BufferedWriter(new PrintWriter(s.getOutputStream()));
toServer.write("GET index.html HTTP/1.1\r\n");
toServer.write("HOST: 127.0.0.1\r\n");
toServer.write("Connection: close\r\n");
toServer.write("\r\n");
toServer.write("null\r\n");
toServer.flush();
while(true)
{
String ss = fromServer.readLine();
if(ss != null && !ss.equals("null"))
System.out.println(ss);
}
服务器类:Strydom_A_201103578_P03
public class Strydom_A_201103578_P03
{
Thread[] threadArray = new Thread[5];
int ClientCount = 0;
public Strydom_A_201103578_P03() throws ClientSizeExceededException
{
ServerSocket httpServer = null;
try
{
httpServer = new ServerSocket(1337);
}
catch (IOException ex)
{
Logger.getLogger(Strydom_A_201103578_P03.class.getName()).log(Level.SEVERE, null, ex);
}
while(true)
{
try
{
//Wait for connection
Socket clientSocket = httpServer.accept();
if(ClientCount < 5)
{
threadArray[ClientCount] = new Thread(new clientHandler(clientSocket));
threadArray[ClientCount].start();
ClientCount++;
}
else
{
throw new ClientSizeExceededException();
}
}
catch(IOException ex)
{
}
finally
{
}
}
}
class clientHandler implements Runnable
{
Socket clientSocket;
public clientHandler(Socket clientSocket)
{
this.clientSocket = clientSocket;
}
@Override
public void run()
{
BufferedReader clientInput = null;
BufferedWriter clientOutput = null;
try
{
clientInput = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
clientOutput = new BufferedWriter(new PrintWriter(clientSocket.getOutputStream()));
clientOutput.write(" ");
clientOutput.flush();
while(true)
{
String clientRequest = "";
String tempStr = clientInput.readLine();
while(tempStr != null && !tempStr.equals("null"))
{
System.out.println(tempStr);
clientRequest += tempStr + " ";
tempStr = clientInput.readLine();
}
//Parse Request
ArrayList<String> tokenArray = parseRequest(clientRequest);
Calendar c = Calendar.getInstance();
switch(tokenArray.get(0))
{
case "GET":
{
clientOutput.write("HTTP/1.1 200 OK\r\n");
clientOutput.write("Date: " + c.getDisplayName(0, Calendar.LONG, Locale.UK).toString() + "\r\n");
clientOutput.write("Server: Java HTTP Server v1.0\r\n");
clientOutput.flush();
break;
//Write File
}
default:
{
clientOutput.write("500\r\n");
clientOutput.flush();
}
}
}
}
catch (IOException ex)
{
Logger.getLogger(Strydom_A_201103578_P03.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
try
{
clientInput.close();
clientOutput.close();
}
catch (IOException ex)
{
Logger.getLogger(Strydom_A_201103578_P03.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private ArrayList<String> parseRequest(String tempStr)
{
StringTokenizer httpTokens = new StringTokenizer(tempStr, " ");
ArrayList<String> tokens = new ArrayList<>();
while(httpTokens.hasMoreTokens())
tokens.add(httpTokens.nextToken());
return tokens;
}
}
public static void main(String[] args) throws ClientSizeExceededException
{
new Strydom_A_201103578_P03();
}
}
public class TestClient
{
public TestClient()
{
try
{
Socket s = new Socket("localhost", 1337);
BufferedReader fromServer = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter toServer = new BufferedWriter(new PrintWriter(s.getOutputStream()));
toServer.write("GET index.html HTTP/1.1\r\n");
toServer.write("HOST: 127.0.0.1\r\n");
toServer.write("Connection: close\r\n");
toServer.write("\r\n");
toServer.write("null\r\n");
toServer.flush();
while(true)
{
String ss = fromServer.readLine();
if(ss != null && !ss.equals("null"))
System.out.println(ss);
}
}
catch (UnknownHostException ex)
{
Logger.getLogger(TestClient.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(TestClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args)
{
new TestClient();
}
}
客户端类:TestClient
创建一个项目(或 2)并运行文件