Java socket programming example

Source: Internet
Author: User

I. Client

Package net. sockettest; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. io. outputstreamwriter; import Java. io. printwriter; import Java. io. reader; import Java. io. writer; import java.net. socket; import java.net. unknownhostexception; import Java. util. random; import Java. util. concurrent. executorservice; import Java. util. concurrent. executors; public class multithreadclient_jiaohu {public static void main (string [] ARGs) throws interruptedexception {int numtasks = 2120; executorservice exec = executors. newcachedthreadpool (); For (INT I = 0; I <numtasks; I ++) {thread. sleep (5l); exec.exe cute (createtask (I) ;}// define a simple task Private Static runnable createtask (final int taskid) {return New runnable () {private Socket socket = NULL; private int Port = 8821; Public void run () {socket clinet = NULL; int task_id = taskid; try {clinet = new socket ("192.168.252.155 ", port);} catch (unknownhostexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} reader = NULL; try {reader = new inputstreamreader (clinet. getinputstream ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} bufferedreader clinetin = new bufferedreader (Reader); writer = NULL; try {writer = new outputstreamwriter (clinet. getoutputstream ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} printwriter clientout = new printwriter (writer); int I = 0; while (true) {I ++; try {int ran = new random (). nextint (20*60) + 1; thread. sleep (Ran * 1000l);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace ();} string STR = "Thread No.:" + task_id; clientout. println (STR); clientout. flush (); string newstr = NULL; try {newstr = clinetin. readline ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} system. out. println ("--:" + newstr); if (I> = 100000) break;} Try {clinetin. close ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} clientout. close (); system. out. println ("=== sending end = ");}};}}

Ii. Server

 

Package net. sockettest; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. outputstream; import Java. io. printwriter; import java.net. *; import Java. util. concurrent. *; public class socketserver {private int Port = 8821; private serversocket; private executorservice; // thread pool private final int pool_siz E = 2200; // the size of a single CPU thread pool is also the maximum number of connections; otherwise, the number of connections exceeding this limit cannot be connected to private int COUNT = 0; Public socketserver () throws ioexception {serversocket = new serversocket (port); // The availableprocessor () method of runtime returns the number of CPUs of the current system. executorservice = executors. newfixedthreadpool (runtime. getruntime (). availableprocessors () * pool_size); system. out. println ("START socket server at Port:" + port);} public void start () {While (true) {Socket socket = nu Ll; try {// receive the customer connection. As long as the customer connects, accept () is triggered, and the connection socket = serversocket is established. accept (); count ++; system. out. println ("" + count); executorservice.exe cute (new handler (socket);} catch (exception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) throws ioexception {New socketserver (). start () ;}} class handler implements runnable {private Socket socket; Public handler (Socket socket) {This. Socket = socket;} private printwriter getwriter (Socket socket) throws ioexception {outputstream socketout = socket. getoutputstream (); return New printwriter (socketout, true);} private bufferedreader getreader (Socket socket) throws ioexception {inputstream socketin = socket. getinputstream (); return New bufferedreader (New inputstreamreader (socketin);} public void run () {try {system. out. println ("New Co Nnection accepted "+ socket. getinetaddress () + ":" + socket. getport (); bufferedreader BR = getreader (socket); printwriter PW = getwriter (socket); string MSG = NULL; while (MSG = BR. readline ())! = NULL) {// system. out. println (MSG); PW. println (MSG) ;}} catch (ioexception e) {e. printstacktrace ();} finally {try {If (socket! = NULL) socket. Close () ;}catch (ioexception e) {e. printstacktrace ();}}}}

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.