Package com. java. suting;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java.net. Socket;
Import java. util. collections;
Public class sockettest {
Public static void main (string [] ARGs ){
Try {
// Create a socket
Socket S = new socket ("java.sun.com", 80 );
Try {
Inputstream instream = S. getinputstream ();
Vertex in = new vertex (instream );
While (in. hasnextline ()){
String line = in. nextline ();
System. Out. println (line );
}
}
Finally
{
// Close a socket
S. Close ();
}
}
Catch (ioexception e ){
E. printstacktrace ();
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~
Package com. java. suting;
Import java. Io .*;
Import java.net .*;
Import java. util .*;
/*
* This program implements a simple server to port 8189 and echoes back
* All client input.
*/
Public class echoserver {
Public static void main (string [] ARGs ){
Try {
// Establish server socket
Serversocket S = new serversocket (8189 );
// Wait for client connection
Socket incoming = S. Accept ();
Try {
// Instream receives the input from the client, and outstream outputs the data to the client
Inputstream instream = incoming. getinputstream ();
Outputstream outstream = incoming. getoutputstream ();
Vertex in = new vertex (instream );
Printwriter out = new printwriter (outstream, true/* autoflush */);
Out. println ("heloo enter bye to exit .");
// Echo client input
Boolean done = false;
While (! Done & in. hasnextline ())
{
String line = in. nextline ();
Out. println ("Echo:" + line );
If (line. Trim (). Equals ("bye "))
Done = true;
}
}
Finally
{
Incoming. Close ();
}
}
Catch (ioexception e ){
E. printstacktrace ();
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ·
Package com. java. suting;
Import java. Io .*;
Import java.net .*;
Import java. util .*;
/*
* This program implements a Multithread server that listens to Port
* 8189 and echoes back all client input
*/
Public class threadechoserver {
Public static void main (string [] ARGs ){
Try {
Int I = 1;
Serversocket S = new serversocket (8189 );
While (true ){
Socket incoming = S. Accept ();
System. Out. println ("spawing" + I );
// Create a thread
Runnable r = new threadedechohandler (incoming, I );
Thread t = new thread (R );
T. Start ();
I ++;
}
}
Catch (ioexception e ){
E. printstacktrace ();
}
}
}
/*
* This class handles the client input for one server socket connection
*
*/
Class threadedechohandler implements runnable {
/*
* Constructor
*/
Public threadedechohandler (socket I, int c ){
Incoming = I;
Counter = C;
}
@ Override
Public void run (){
// Todo auto-generated method stub
Try {
Try {
// Instream receives the input from the client, and outstream outputs the data to the client
Inputstream instream = incoming. getinputstream ();
Outputstream outstream = incoming. getoutputstream ();
Vertex in = new vertex (instream );
Printwriter out = new printwriter (outstream, true/* autoflush */);
Out. println ("heloo enter bye to exit .");
// Echo client input
Boolean done = false;
While (! Done & in. hasnextline ())
{
String line = in. nextline ();
Out. println ("Echo:" + line );
If (line. Trim (). Equals ("bye "))
Done = true;
}
}
Finally
{
Incoming. Close ();
}
}
Catch (ioexception e ){
E. printstacktrace ();
}
}
Private socket incoming;
Private int counter;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ ·