A simple example of serversocket and socket communication in Java

Source: Internet
Author: User

Server code:

Import java. Io .*;
Import java.net .*;

Class serveonejabber extends thread {
Private Socket socket;
Private bufferedreader in;
Private printwriter out;
Public serveonejabber (socket S)
Throws ioexception {
Socket = s;
In =
New bufferedreader (
New inputstreamreader (
Socket. getinputstream ()));
// Enable auto-flush:
Out =
New printwriter (
New bufferedwriter (
New outputstreamwriter (
Socket. getoutputstream (), true );
// If any of the above callthrow
// Exception, the caller is responsible
// Closing the socket. Otherwise the thread
// Will close it.
Start (); // callrun ()
}
Public void run (){
Try {
While (true ){
String STR = in. Readline (); // receives client data
If (Str. Equals ("end") break;
System. Out. println ("echoing:" + Str );
Out. println (STR); // returns a message to the client.
}
System. Out. println ("closing ...");
} Catch (ioexception e ){
} Finally {
Try {
Socket. Close ();
} Catch (ioexception e ){}
}
}
}

Public class multijabberserver {
Static final int Port = 8080;
Public static void main (string [] ARGs)
Throws ioexception {
Serversocket S = new serversocket (port );
System. Out. println ("server started ");
Try {
While (true ){
// Blocks until a connection occurs:
Socket socket = S. Accept ();
Try {
New serveonejabber (socket );
} Catch (ioexception e ){
// If it fails, close the socket,
// Otherwise the thread will close it:
Socket. Close ();
}
}
} Finally {
S. Close ();
}
}
}

Client code:

Import java.net .*;
Import java. Io .*;

Class jabberclientthread extends thread {
Private Socket socket;
Private bufferedreader in;
Private printwriter out;
Private Static int counter = 0;
Private int id = counter ++;
Private Static int threadcount = 0;
Public static int threadcount (){
Return threadcount;
}
Public jabberclientthread (inetaddress ADDR ){
System. Out. println ("Making client" + id );
Threadcount ++;
Try {
Socket =
New socket (ADDR, multijabberserver. Port );
} Catch (ioexception e ){
// If the creation of the socket fails,
// Nothing needs to be cleaned up.
}
Try {
In =
New bufferedreader (
New inputstreamreader (
Socket. getinputstream ()));
// Enable auto-flush:
Out =
New printwriter (
New bufferedwriter (
New outputstreamwriter (
Socket. getoutputstream (), true );
Start ();
} Catch (ioexception e ){
// The socket shocould be closed on any
// Failures other than the socket
// Constructor:
Try {
Socket. Close ();
} Catch (ioexception E2 ){}
}
// Otherwise the socket will be closed
// The run () method of the thread.
}
Public void run (){
Try {
For (INT I = 0; I <25; I ++ ){
Out. println ("client" + ID + ":" + I); // send data to the server (or send a request to the server)
String STR = in. Readline (); // read the data sent from the server.
System. Out. println (STR );
}
Out. println ("end ");
} Catch (ioexception e ){
} Finally {
// Always close it:
Try {
Socket. Close ();
} Catch (ioexception e ){}
Threadcount --; // ending this thread
}
}
}

Public class multijabberclient {
Static final int max_threads = 4;
Public static void main (string [] ARGs)
Throws ioexception, interruptedexception {
Inetaddress ADDR =
Inetaddress. getbyname (null );
While (true ){
If (jabberclientthread. threadcount () <max_threads)
New jabberclientthread (ADDR );
Else
Break;
Thread. currentthread (). Sleep (1000 );
}
}
}

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.