https server can't be running,who have some suggestions.

bing8206bing8206 Junior MemberPosts: 2

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.KeyStore;
import java.util.StringTokenizer;

import javax.net.ServerSocketFactory;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;


public class HttpsServer {
private static int HTTPS_PORT = 8000;
private static final String keystore = "serverkey.jks";
private static final char keystorepass[] = "123456".toCharArray();
private static final char keypassword[] = "123456".toCharArray();
public String aaa;

/**
* @param args
*/
public ServerSocket getServer() throws Exception {
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(keystore), keystorepass);

KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, keypassword);
SSLContext sslcontext = SSLContext.getInstance("SSLv3");
//SSLContext sslcontext = SSLContext.getInstance("TLSv1");
sslcontext.init(kmf.getKeyManagers(), null, null);
ServerSocketFactory ssf = sslcontext.getServerSocketFactory();
SSLServerSocket serversocket = (SSLServerSocket)ssf.createServerSocket(HTTPS_PORT);
return serversocket;
}
public void run()
{
ServerSocket listen;
try
{
listen = getServer();
while(true)
{
Socket client = listen.accept();
ProcessConnection cc = new ProcessConnection(client);
System.out.print(cc.getName()+ "runing\n");
}
}
catch(Exception e)
{
System.out.println("Exception: " + e.getMessage());
}
}
public static void main(String argv[]) throws Exception
{
HttpsServer httpserver = new HttpsServer();
httpserver.run();
}
public void runhttps()throws Exception {
HttpsServer httpserver = new HttpsServer();
httpserver.run();
}
public String Gettestingstring(){
return aaa;
}
}

class ProcessConnection extends Thread
{
Socket client;
BufferedReader is;
DataOutputStream os;
String Testingreceive;
public ProcessConnection(Socket s)
{
client = s;
try
{
is = new BufferedReader(new InputStreamReader(client.getInputStream()));
os = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
System.out.println("Exception: "+e.getMessage());
}
this.start();
}

public void run()
{
try
{
String request = is.readLine();
System.out.println( "Request Receive: "+request );
Testingreceive = request;
StringTokenizer st = new StringTokenizer( request );
if ( (st.countTokens() >= 2) && st.nextToken().equals("GET") )
{
if ((request = st.nextToken()).startsWith("/"))
request = request.substring( 1 );
if (request.equals(""))
request = request + "index.html";
File f = new File(request);
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
shipDocument(os, f);

}
else
{
os.writeBytes( "400 Bad Request" );
}
client.close();
}
catch (Exception e)
{
System.out.println("Exception: " + e.getMessage());
}
}


public static void shipDocument(DataOutputStream out, File f) throws Exception
{
try
{
System.out.print("f:"+f.toString());
//DataInputStream in=new DataInputStream(new FileInputStream("fff.txt"));
DataInputStream in = new DataInputStream(new FileInputStream(f));
int len = (int) f.length();

byte[] buf = new byte[len];
in.readFully(buf);
in.close();
out.writeBytes("<html><head><title>success</title></head><body>\r\n\r\n");
out.writeBytes("HTTP/1.0 200 OK\r\n");
//out.writeBytes("Content-Length: " + f.length() +"\r\n");
out.writeBytes("Content-Length: " +f.length() +"\r\n");
out.writeBytes("Content-Type: text/html\r\n\r\n");
out.write(buf);
out.writeBytes("</body></html>");
out.flush();

//out.writeBytes("<html><head><title>success</title></head><body>\r\n\r\n");
//out.writeBytes("HTTP/1.0 400 "+ "\r\n");
//out.writeBytes("Content-Type: text/html\r\n\r\n");
//out.writeBytes("</body></html>");
//out.flush();
}
catch (Exception e)
{
out.writeBytes("<html><head><title>error</title></head><body>\r\n\r\n");
//out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");
out.writeBytes("HTTP/1.0 400 " + e.toString()+ "\r\n");
out.writeBytes("Content-Type: text/html\r\n\r\n");
out.writeBytes("</body></html>");
out.flush();
}
finally
{
out.close();
}
}

}
Sign In or Register to comment.