Sampingan
0

Apa itu Multithread??

=> suatu aplikasi server dimana server dapat melayani beberapa permintaan client

Berikut script dari Multithread

================================================================

import java.io.*;
import java.net.*;
import java.util.Date;
import java.util.StringTokenizer;

public class ServerMultiThread implements Runnable {

static final File WEB_ROOT = new File(“.”);
static final String DEFAULT_FILE = “index.html”;
static final int PORT = 80;
Socket link;

public ServerMultiThread(Socket connect) {
this.link = connect;
}

public static void main(String[] args) {
try {
ServerSocket servSock = new ServerSocket(PORT); //membuat objek server
System.out.println(“\nMenunggu koneksi…\n”);
while (true) {
ServerMultiThread server = new ServerMultiThread(servSock.accept()); //merubah status objek server ke waiting state
Thread threadRunner = new Thread(server);
threadRunner.start();
}
} catch (IOException e) {
System.err.println(“Server error: ” + e);
}
}

public void run() {
BufferedReader input = null;
PrintWriter output = null;
BufferedOutputStream dataOutput = null;
String fileDiminta = null;

try {
input = new BufferedReader(new InputStreamReader(link.getInputStream())); //langkah ke tiga
output = new PrintWriter(link.getOutputStream()); //langkah ke tiga
dataOutput = new BufferedOutputStream(link.getOutputStream()); //langkah ke tiga

String pesan = input.readLine();  //langkah ke 4, menerima pesan dari browser

StringTokenizer parse = new StringTokenizer(pesan);
String metode = parse.nextToken().toUpperCase();
fileDiminta = parse.nextToken().toLowerCase();

System.out.println(“alamat file yang diminta: ” + fileDiminta);

if (!metode.equals(“GET”)) {
output.println(“HTTP/1.0 501 Not Implemented”);
output.println(“Server: Java HTTP Server 1.0”);
output.println(“Date: ” + new Date());
output.println(“Content-Type: text/html”);
output.println();
output.println(“<HTML>”);
output.println(“<HEAD><TITLE>Not Implemented</TITLE>”
+ “</HEAD>”);
output.println(“<BODY>”);
output.println(“<H2>501 Not Implemented: ” + metode
+ ” method.</H2>”);
output.println(“</BODY></HTML>”);
output.flush();

return;
}

if (fileDiminta.endsWith(“/”))
fileDiminta = fileDiminta + DEFAULT_FILE;

File file = new File(WEB_ROOT, fileDiminta);
int ukuran = (int) file.length();

String tipe;
if (fileDiminta.endsWith(“.htm”) || fileDiminta.endsWith(“.html”))
tipe = “text/html”;
else if (fileDiminta.endsWith(“.gif”))
tipe = “image/gif”;
else if (fileDiminta.endsWith(“.jpg”) || fileDiminta.endsWith(“.jpeg”))
tipe = “image/jpeg”;
else if (fileDiminta.endsWith(“.class”) || fileDiminta.endsWith(“.jar”))
tipe = “applicaton/octet-stream”;
else
tipe = “text/plain”;

FileInputStream bacaFile = null;
byte fileData[] = new byte[ukuran];

try {
bacaFile
= new FileInputStream(file);
bacaFile.read(fileData);
} finally {
try {
bacaFile.close();
} catch (Exception ex) {
}
}

output.println(“HTTP/1.0 200 OK”);
output.println(“Server: Java HTTP Server 1.0”);
output.println(“Date: ” + new Date());
output.println(“Content-type: ” + tipe);
output.println(“Content-length: ” + file.length());
output.println();
output.flush();

dataOutput.write(fileData, 0, ukuran);
dataOutput.flush();

} catch (FileNotFoundException fnfe) {
output.println(“HTTP/1.0 404 File Not Found”);
output.println(“Server: Java HTTP Server 1.0”);
output.println(“Date: ” + new Date());
output.println(“Content-Type: text/html”);
output.println();
output.println(“<HTML>”);
output.println(“<HEAD><TITLE>File Not Found</TITLE>”
+ “</HEAD>”);
output.println(“<BODY>”);
output.println(“<H2>404 File Not Found: ” + fileDiminta + “</H2>”);
output.println(“</BODY>”);
output.println(“</HTML>”);
output.flush();
} catch (IOException ioe) {
System.err.println(“Server Error: ” + ioe);
} finally {
System.out.println(“ip client peng’akses: ” + link.getInetAddress().getHostAddress());
try {
input.close();
output.close();
dataOutput.close();
link.close();      //penutupan koneksi
System.out.println(“koneksi dengan client langsung tertutup”);
} catch (Exception ex) {
}
}
}
}

=============================================================

===Videonya disini http://www.youtube.com/watch?v=xioAgKmx4Hw

Semoga bermanfaat kakak🙂

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s