The Java socket itself acts as Tomcat

Source: Internet
Author: User

 PackageTest.com.ict.testcountry;ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;Importjava.net.ConnectException;Importjava.net.InetSocketAddress;ImportJava.net.ServerSocket;ImportJava.net.Socket;Importjava.net.SocketTimeoutException;/*** Use socket to send and receive HTTP protocol messages *@authorLuoguansong [email protected]*/ Public classSockethttp { Public Static voidMain (string[] args) {Thread threadreceive=NewThread (Newtestreceivehttp ()); Threadreceive.start ();//thread threadsend=new Thread (new Testsendhttp ());//Threadsend.start ();    }} classTestsendhttpImplementsrunnable{@Override Public voidrun () {Try{String path= "/"; String Host= "localhost"; intPort = 50000; Socket Socket=NewSocket (); Inetsocketaddress Address=Newinetsocketaddress (host, Port); Socket.connect (Address,3000); OutputStreamWriter OSW=NewOutputStreamWriter (Socket.getoutputstream (), "Utf-8"); Osw.write ("GET" + path + "http/1.1\r\n"); Osw.write ("Host:" + host + "\ r \ n"); //the HTTP protocol must be followed by a newline at the end of the message header to notify the server that it is sent, or the server waitsOsw.write ("\ r \ n");            Osw.flush ();                         Socket.shutdownoutput (); BufferedReader BufferedReader=NewBufferedReader (NewInputStreamReader (Socket.getinputstream (), "Utf-8")); String Line=NULL;  while(line = Bufferedreader.readline ())! =NULL) {System.out.println (line);            } osw.close ();            Bufferedreader.close ();        Socket.close (); }Catch(connectexception e) {System.out.println ("Connection Failed"); }Catch(sockettimeoutexception e) {System.out.println ("Connection timed out"); }Catch(Exception e) {System.out.println (E.getmessage ()); }    }} classTestreceivehttpImplementsrunnable{@Override Public voidrun () {ServerSocket server;        Socket socket; Try{Server=NewServerSocket (50000); System.out.println ("Waiting for a 50000 port request");  while(true) {Socket=server.accept (); if(socket!=NULL){                    NewThread (NewTestreveivethread (socket)). Start (); }            }        }Catch(Exception e) {System.out.println (exception); }    }} classTestreveivethreadImplementsrunnable{socket socket;  PublicTestreveivethread (socket s) {socket=s; }     Public voidrun () {Try{BufferedReader BufferedReader=NewBufferedReader (NewInputStreamReader (Socket.getinputstream (), "Utf-8")); OutputStreamWriter OSW=NewOutputStreamWriter (Socket.getoutputstream (), "Utf-8"); String Line=NULL;  while((Line=bufferedreader.readline ())! =NULL) {System.out.println (line); if(Line.equals ("")){                     Break; }            }            //simulates an HTTP request to a Web site, and then forwards the content to the current HTTP request//String Path = "/";//String host = "Www.oschina.net";//int port = n;//Socket Socket2 = new socket (host, port);//OutputStreamWriter osw2 = new OutputStreamWriter (Socket2.getoutputstream (), "utf-8"); //osw2.write ("GET" + path + "http/1.1\r\n");//Osw2.write ("Host:" + host + "\ r \ n");//osw2.write ("\ r \ n");//Osw2.flush ();//BufferedReader bufferedReader2 = new BufferedReader (New InputStreamReader (Socket2.getinputstream (), "Utf-8") );//String line2 = null;//While ((line2 = Bufferedreader2.readline ()) = null) {//osw.write (line2+ "\ r \ n");//          }//bufferedreader2.close ();//osw2.close ();//socket2.close ();Osw.write ("http/1.1 ok\r\n"); Osw.write ("Server:apache-coyote/1.1\r\n"); Osw.write ("SET-COOKIE:JSESSIONID=03493794995CE31A0F131787B6C6CBB2; path=/; Httponly\r\n "); Osw.write ("Content-type:text/html;charset=utf-8\r\n"); Osw.write ("Transfer-encoding:chunked\r\n"); Osw.write ("Date:tue, 02:48:27 gmt\r\n"); Osw.write ("\ r \ n"); Osw.write ("C9\r\n"); Osw.write ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" >\r\n "); Osw.write ("); Osw.write ("); Osw.write ("<body>\r\n"); Osw.write ("This is class Com.serv.myServ, using the GET method\r\n"); Osw.write ("</body>\r\n"); Osw.write ("); Osw.write ("\ r \ n"); Osw.write ("0"); Osw.write ("\ r \ n"); Osw.write ("\ r \ n");            Osw.flush ();            Bufferedreader.close ();            Osw.close ();        Socket.close (); }Catch(Exception e) {System.out.println ("Client accepts exception" +e.getmessage ()); }    }}

Enter localhost:50000 in the browser

The Java socket itself acts as Tomcat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.