Serveonesocket. Java Server Source Code

Source: Internet
Author: User

// Serveonesocket. Java Server Source Code

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

Public class serveonesocket extends thread {

Private Socket socket;
Private bufferedreader in;
Private printwriter out;
Private string content;

/**
* Constructor
*/
Public serveonesocket (socket S, string c) throws ioexception {
Socket = s;
Content = C;
In = new bufferedreader (
New inputstreamreader (
Socket. getinputstream ()));
// Enable auto-flush
Out = new printwriter (
New bufferedwriter (
New outputstreamwriter (
Socket. getoutputstream (), true );
Start (); // callrun ()
}

Public void run (){
Try {
While (true ){
String STR = in. Readline ();
If (Str. Equals ("end") break;
System. Out. println ("echoing:" + Str );
Out. println (STR );
Out. println (content );
}
System. Out. println ("closing ...");
} Catch (ioexception e ){
} Finally {
Try {
Socket. Close ();
} Catch (ioexception e ){}
}
}
}

// Socketclientthread. Java client source code

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

Class socketclientthread 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;
Final int Port = 8110;

Public static int threadcount (){
Return threadcount;
}

Public socketclientthread (inetaddress ADDR ){
System. Out. println ("Making client:" + id );
Threadcount ++;
Try {
Socket = new socket (ADDR, Port );
} Catch (ioexception e ){
}
Try {
In = new bufferedreader (
New inputstreamreader (
Socket. getinputstream ()));
Out = new printwriter (
New bufferedwriter (
New outputstreamwriter (
Socket. getoutputstream (), true );
Start ();
} Catch (ioexception e ){
Try {
Socket. Close ();
} Catch (ioexception E2 ){}
}
}

Public void run (){
Try {
For (INT I = 0; I <25; I ++ ){
Out. println ("client:" + ID + ":" + I );
String STR = in. Readline ();
System. Out. println (STR );
}
Out. println ("end ");
} Catch (ioexception e ){
} Finally {
Try {
Socket. Close ();
} Catch (ioexception e ){}
Threadcount --;
}
}
}

Public class multisocketclient {
Static final int max_threads = 10;
/**
* Main
* @ Param ARGs
*/
Public static void main (string [] ARGs) throws ioexception, interruptedexception {
Inetaddress ADDR = inetaddress. getbyname (null );
While (true ){
If (socketclientthread. threadcount () <max_threads)
New socketclientthread (ADDR );
Thread. currentthread (). Sleep (100 );
}
}
}

Related Article

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.