Network Programming for Java Learning

Source: Internet
Author: User

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;
}

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ ·

 

 

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.