A simple Local Web server developed by Java

Source: Internet
Author: User

// Clientframe. Java

Import java. AWT. borderlayout;
Import java. AWT. event. actionevent;
Import java. AWT. event. actionlistener;
Import javax. Swing .*;
Public class clientframe extends jframe implements actionlistener {
Jbutton sendbutton;
Jtextfield inputfield;
Jtextarea outputarea;
Simpleclient client;
Jbutton exitbutton;
// Initialize the GUI in the constructor
Public clientframe (){
Jlabel label1 = new jlabel ("input :");
Inputfield = new jtextfield (20 );
Jpanel Panel1 = new jpanel ();
Panel1.add (label1 );
Panel1.add (inputfield );

Jlabel label2 = new jlabel ("server return :");
Outputarea = new jtextarea (6, 20 );
Jscrollpane scrollpane = new jscrollpane (outputarea );
Jpanel panel2 = new jpanel ();
Panel2.setlayout (New borderlayout ());
Panel2.add (label2, borderlayout. North );
Panel2.add (scrollpane, borderlayout. center );

Sendbutton = new jbutton ("send ");
Sendbutton. addactionlistener (this );
Exitbutton = new jbutton ("exit ");
Exitbutton. addactionlistener (this );

Jpanel Panel = new jpanel ();
Panel. setlayout (New borderlayout ());
Panel. Add (Panel1, borderlayout. North );
Panel. Add (sendbutton, borderlayout. center );
Panel. Add (exitbutton, borderlayout. East );
Panel. Add (panel2, borderlayout. page_end );

Settitle ("Socket Client ");
This. getcontentpane (). Add (panel );
This. setdefaclocloseoperation (jframe. exit_on_close );
}
Public void actionreceivmed (actionevent AE ){
// Determine whether the event source space is a send button
If (AE. getsource () = sendbutton ){
Try {
Client. sendrequest (inputfield. gettext ());

} Catch (exception ex ){
Ex. printstacktrace ();
}
Outputarea. append (client. getreponse () + "/N ");
}
If (AE. getsource () = exitbutton ){
Outputarea. append ("exited/N ");
// This. setdefaclocloseoperation (jframe. exit_on_close );
// Sendbutton. Hide ();
}
}
Public static void main (string ARGs []) {
Clientframe frame = new clientframe ();
Frame. Pack ();
Frame. Client = new simpleclient ("127.0.0.1", 8888 );
Frame. setvisible (true );
}
}

// Simpleclient. Java
Import java. Io .*;
Import java.net. Socket;
Public class simpleclient {
Printstream out;
Bufferedreader in;

// Constructor
Public simpleclient (string servername, int port ){
Try {
Socket clientsocket = new socket (servername, Port );
Out = new printstream (clientsocket. getoutputstream ());
In = new bufferedreader (New inputstreamreader (clientsocket. getinputstream ()));

} Catch (exception e ){
System. Out. println ("unable to connect to the server ");
}
}
// Send the request
Public void sendrequest (string request ){
Out. println (request );
System. Out. println ("client sends a request:" + request );
}
Public String getreponse (){
String STR = new string ();
Try {
STR = in. Readline ();
System. Out. println ("the client returns the server:" + Str );
} Catch (ioexception e ){

}
Return STR;
}
}
// Simpleserver. Java

 

Import java. Io .*;
Import java.net. serversocket;
Import java.net. Socket;
Public class simpleserver {
Serversocket serverskt = NULL;
Socket clientskt = NULL;
Bufferedreader in = NULL;
Printstream out = NULL;
// Constructor
Public simpleserver (INT port ){
System. Out. println ("the server proxy is listening, Port:" + port );
Try {
Serverskt = new serversocket (port );
} Catch (ioexception e ){
System. Out. println ("listening port" + port + "failed! ");
}
Try {
Clientskt = serverskt. Accept ();

} Catch (ioexception e ){
System. Out. println ("connection failed ");
}
Try {
In = new bufferedreader (New inputstreamreader (clientskt. getinputstream ()));
Out = new printstream (clientskt. getoutputstream ());

} Catch (ioexception e ){

}
}
// Receives the client request
Public String getrequest (){
String frmclt = NULL;
Try {
Frmclt = in. Readline ();
System. Out. println ("server received request:" + frmclt );
} Catch (exception e ){
System. Out. println ("unable to read port .......");
System. Exit (0 );
}
Return frmclt;
}
// Send a response to the client
Public void sendresponse (string response ){
Try {
Out. println (response );
System. Out. println ("Server Response Request:" + response );
} Catch (exception ex ){
System. Out. println ("failed to write port !... ");
System. Exit (0 );
}
}
Public static void main (string ARGs []) throws ioexception {
Simpleserver SA = new simpleserver (8888 );
While (true ){
SA. sendresponse (SA. getrequest ());
}
}
}

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.