Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Java how to handle HTTP GET request after establishing TCP connection

ServerSocket serverSock = new ServerSocket(6789);
Socket sock = serverSock.accept();

InputStream sis = sock.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(sis));
String request = br.readLine(); // Now you get GET index.html HTTP/1.1`
String[] requestParam = request.split(" ");
String path = requestParam[1];

PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
File file = new File(path);
if (!file.exists()) {
     out.write("HTTP 404"); // the file does not exists
}
FileReader fr = new FileReader(file);
BufferedReader bfr = new BufferedReader(fr);
String line;
while ((line = bfr.readLine()) != null) {
    out.write(line);
}

bfr.close();
br.close();
out.close();
Comment

PREVIOUS NEXT
Code Example
Java :: how to search element in sorted array using java 
Java :: Java instanceof in Interface 
Java :: least count of words required to construct a target string 
Java :: does not have a NavController set on 21312310 kotlin 
Java :: take string , double and int value from useer using java 
Java :: reference to an instance method of an arbitrary object of a particular type 
Java :: node constructor 
Java :: java schleife abbrechen 
Java :: android studio doesnt work when in full screen mac os 
Java :: how to show the hex detail of a file in java 
Java :: How to Register a Custom Auto-Configuration? 
Java :: minecraft block java 
Java :: android how to get position of a row in listview 
Java :: public class HelloWorld { public static void main( String[] argv ) { int a=4%2*3-1/0; System.out.println(a); } } 
Java :: springboot body 
Java :: I/System.out: ep: android.os.NetworkOnMainThreadException 
Java :: java web.xml 
Java :: draaiom 
Java :: Retrieve Image from java database. 
Java :: Java Target annotations attributes 
Java :: permuatrion sequence 
Java :: steps to accomplish jdbc connection in java 
Java :: Duplicate entry Exception 
Java :: How can i stub Instant object using powermock 
Java :: java create a hashmap 
Java :: reponse entity as result spring controller 
Java :: how to insert a char at n position java 
Java :: java singleton design pattern 
Java :: how to uncomment a block of statements in java 
Java :: object class java 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =