Java Sockets
Mon Jan 29 2024 04:10:17 GMT+0000 (Coordinated Universal Time)
Saved by
@NAME
import java.util.Scanner;
import java.net.*;
import java.io.*;
public class HttpServerDemo{
public static void main(String[] args) throws IOException{
int port = 3001;
ServerSocket serverSocket = new ServerSocket(port);
Sysytem.out.println("Server started on port " + port);
while(true){
Socket socket = serverSocket.accept();
System.err.println("Client connected");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String s;
while((s = in.readLine() != null )){
System.err.println(s);
if(s.isEmpty()){
break;
}
}
OutputStream clientOuput = socket.getOutputStream();
clientOutput.write("HTTP/1.1 200 OK\r\n".getBytes());
clientOutput.write("\r\n".getBytes());
clientOutput.write("<b>Hello World</b>".getBytes());
clientOutput.write("\r\n\r\n".getBytes());
clientOutput.Flush();
System.out.println("Client connection ended");
in.close();
clientOuput.close();
}
}
}
content_copyCOPY
Comments