1, Client Myclient.java
1 ImportJava.io.*;2 Importjava.net.*;3 4 Public classmyclient5 {6 Public Static voidMain (string[] args)throwsException7 {8Socket s =NewSocket ("192.168.1.1", 30000);9 //client-initiated Clientthread thread continuously reads data from the serverTen NewThread (NewClientthread (s)). Start (); One //gets the output stream corresponding to the socket APrintStream PS =NewPrintStream (S.getoutputstream ()); -String line =NULL; - //constant reading of keyboard input theBufferedReader br =NewBufferedReader ( - NewInputStreamReader (system.in)); - while(line = Br.readline ())! =NULL) - { + //writes the user's keyboard input to the socket corresponding output stream - ps.println (line); + } A } at}
2. Join multi-threading, Clientthread.java
1 ImportJava.io.*;2 Importjava.net.*;3 4 Public classClientthreadImplementsRunnable5 {6 //the thread is responsible for processing the socket7 PrivateSocket S;8 //The input stream corresponding to the socket that the thread is processing9BufferedReader br =NULL;Ten PublicClientthread (Socket s) One throwsIOException A { - This. S =s; -BR =NewBufferedReader ( the NewInputStreamReader (S.getinputstream ())); - } - Public voidRun () - { + Try - { +String content =NULL; A //continuously reads the contents of the socket input stream and prints out the contents at while(content = Br.readline ())! =NULL) - { - System.out.println (content); - } - } - Catch(Exception e) in { - e.printstacktrace (); to } + } -}
3, server-side Myserver.java
1 Importjava.net.*;2 ImportJava.io.*;3 ImportJava.util.*;4 5 Public classMyServer6 {7 //define the ArrayList that holds all sockets8 Public StaticArraylist<socket>socketlist9=NewArraylist<socket>();Ten Public Static voidMain (string[] args) One throwsIOException A { -System.out.println ("222222 ..."); -ServerSocket SS =NewServerSocket the -(30000); - while(true) - { +SYSTEM.OUT.PRINTLN ("Server Waiting for link ..."); - //This line of code will block and will wait for someone else's connection +Socket s =ss.accept (); A Socketlist.add (s); at //each time a client connects, a Serverthread thread is started to service the client - NewThread (NewServerthread - - (s)). Start (); - } - } in}
4. Add multithreading for server-side communication
1 ImportJava.io.*;2 Importjava.net.*;3 ImportJava.util.*;4 5 //thread class responsible for handling each thread's communication6 Public classServerthreadImplementsRunnable {7 //defines the socket that the current thread handles8Socket s =NULL;9 //The input stream corresponding to the socket that the thread is processingTenBufferedReader br =NULL; One A PublicServerthread (Socket s)throwsIOException { -System.out.println ("4444444 ..."); - This. S =s; the //initialize the input stream corresponding to the socket -String ip=s.getinetaddress (). gethostaddress (); -SYSTEM.OUT.PRINTLN ("IP address is" +ip+ "... connected!!!"); -BR =NewBufferedReader (NewInputStreamReader (S.getinputstream (), +"Utf-8"));// - } + A Public voidrun () { atSystem.out.println ("555555 ..."); - Try { -System.out.println ("4444444 ..."); -String content =NULL; - //using loops to continuously read the data sent by the client from the socket . - while(content = Readfromclient ())! =NULL) { in /*System.out.println ("---" - + arrays.tostring (content.getbytes ("Utf-8" ));*/ toSYSTEM.OUT.PRINTLN ("---" + "the data sent by the client is" +content); +System.out.println ("==============="); - //traverse each socket in the socketlist, the //send read content to each socket once * for(Iterator<socket> it =MyServer.socketList.iterator (); it $ . Hasnext ();) {Panax NotoginsengSocket s =It.next (); - Try { theSystem.out.println ("666666"); +OutputStream OS =S.getoutputstream (); AOs.write (content+ "Server feedback to client OK" + "\ n"). GetBytes ("Utf-8")); theSystem.out.println ("777777"); +}Catch(SocketException e) { - e.printstacktrace (); $ //Delete the socket. $ It.remove (); - System.out.println (myserver.socketlist); - } the } - }Wuyi}Catch(IOException e) { the e.printstacktrace (); - } Wu } - About //define methods for reading client data $ PrivateString readfromclient () { - Try { - returnbr.readline (); - } A //If an exception is caught, the client that corresponds to the socket is closed + Catch(IOException e) { the e.printstacktrace (); -SYSTEM.OUT.PRINTLN ("The client has been turned off!!! "); $ //Delete the socket. the MyServer.socketList.remove (s); the } the return NULL; the } -}
TCP socket Communication for Java Network programming