package cn.itcast.tcp;import java.io.fileoutputstream;import java.io.ioexception;import java.io.inputstream;import java.net.inetaddress;import java.net.socket;import java.net.unknownhostexception;//download picture of client public class imageclient { Public static void main (String[] args) throws Exception{ //establishing a TCP service socket socket = new socket (Inetaddress.getlocalhost (), 9090); // Gets the input stream object of the socket InputStream inputStream = Socket.getinputstream (); //gets the output stream object of the file fileoutputstream fileoutputstream = new fileoutputstream ("F:\\3. JPG "); //Read and write byte[] buf = new byte[1024]; int length = 0 ; while ((length = Inputstream.read (BUF))!=-1) { Fileoutputstream.write (buf,0,length); } //Close Resource fileoutputstream.close (); socket.close (); }}
package cn.itcast.tcp;import java.io.fileinputstream;import java.io.filenotfoundexception; Import java.io.ioexception;import java.io.outputstream;import java.net.serversocket;import java.net.Socket;import java.util.HashSet;/*1. writing a server can send pictures to multiple clients. (multithreading) */public class imageserver extends thread { socket The collection used by socket ; //is used to store IP addresses. static HashSet<String> ips = new HashSet<String> ( ); public imageserver (Socket socket) { this.socket = socket; } @Override public void run () { try { //gets the socket output stream object outputstream outputstream = socket.getoutputstream () //gets the input stream object of the picture FileInputStream fileInputStream = new FileInputStream ("f:\\ Belle \\3.jpg"); // Read the image data and write the data out byte[] buf = new byte[1024]; int Length = 0 ; while ((Length = fileinputstream.read (BUF))!=-1) { Outputstream.write (buf,0,length); } string ip = Socket.getinetaddress (). gethostaddress (); // socket.getinetaddress () Get the IP address of each other if (Ips.add (IP)) { system.out.println ("Congratulations" +ip+ " Students successfully downloaded, the current number of downloads is: "+ ips.size ()); } //Close Resources fileinputstream.close (); socket.close (); }catch (ioexception e) { } } &Nbsp;public static void main (String[] args) throws IOException { //establish TCP service and listen to a port serversocket serversocket = new serversocket (9090); while (true) { // Accept the user's link. Socket socket = Serversocket.accept (); new Imageserver (socket). Start (); } }}
package cn.itcast.tcp;import java.io.bufferedreader;import java.io.ioexception;import java.io.inputstreamreader;import java.io.outputstreamwriter;import java.net.inetaddress;import java.net.socket;/*2. implements login and registration function. Client connection with the server, it is necessary to prompt the client please select the function. Client registration, the user name and password are sent to the server , the server needs to save the data to the server side of the file. Login: Login When the client input user name and password sent to the server, the server needs to verify, return the results to the client. */public class loginclinet { public static void main ( String[] args) throws ioexception { socket socket = new socket (Inetaddress.getlocalhost (), 9090); //gets the output stream object of the socket OutputStreamWriter Socketout = new outputstreamwriter (Socket.getoutputstream ()); //gets the input stream object to the socket &NBSP;&NBSP;&NBSP;&NBSP;&NBSp; bufferedreader socketreader = new bufferedreader (new InputStreamReader (Socket.getinputstream ())) //gets the input stream object to the keyboard BufferedReader keyReader = new BufferedReader (New inputstreamreader (system.in)); while ( true) { system.out.println ("Please select the function: A (login) b (registration) "); string Option = keyreader.readline (); if ("a". EqualsIgnoreCase (option)) { getinfo ( socketout, keyreader, option); //read the information of server feedback string line = socketreader.readline (); system.out.println (line); }else if ("B". Equalsignorecase (option)) { getinfo (socketout, keyreader, option); //read the information of server feedback String line = Socketreader.readline (); system.out.println(line); } } } public static void getinfo ( Outputstreamwriter socketout,bufferedreader keyreader, string option) throws ioexception { system.out.println ("Please enter user name:"); string username = keyreader.readline (); system.out.println ("Please enter password:"); string password = keyreader.readline (); string info = option + " " +username+ " " +password+ "\ r \ n"; socketout.write (info); socketout.flush ( ); &NBSP;&NBSP;&NBSP;&NBSP;}}
package cn.itcast.tcp;import java.io.bufferedreader;import java.io.file;import java.io.filereader;import java.io.filewriter;import java.io.ioexception;import Java.io.inputstreamreader;import java.io.outputstreamwriter;import java.net.serversocket;import java.net.Socket;import java.util.Properties;public class LoginServer extends Thread { socket socket; static file file = new file ("F:\\users.properties"); public loginserver (Socket socket) { this.socket = socket; } static { try { if (! File.exists ()) { &Nbsp; file.createnewfile (); } } catch ( Ioexception e) { E.printstacktrace (); } } @Override public void run () { while (True) { try { // Gets the input stream object of the socket BufferedReader Bufferedreader = new bufferedreader ( new inputstreamreader (SOcket.getinputstream ())); // get the output stream object of the socket outputstreamwriter socketout = new outputstreamwriter ( Socket.getoutputstream ()); // Read the client input information string info = bufferedreader.readline (); string [] datas = info.split (" "); // get to User selection features string option = datas[0]; // Registration &NBSP;&NBSP;&NBSP; string username = datas[1]; String password = datas[2]; if ("a". Equalsignorecase (option)) { // Login properties properties = new properties (); // Load configuration Files properties.load (New filereader (file)); if (Properties.containskey ( UserName)) &NBSP;{&NBSP;&NBSP;&NBsp; string temppass = properties.getproperty (UserName); if (Password.equals (temppass)) { socketout.write ("Welcome" + userName + "landing success \ r \ n"); } else { socketout.write ("password error \ r \ n"); } } else { socketout.write ("User name does not exist, please reenter ... \ r \ n"); } &nbsP; socketout.flush (); } else if ("B". Equalsignorecase (option)) { // Create a configuration file class properties properties = new properties (); //loading the original configuration file properties.load (New filereader (file)); if (!properties.containskey (userName)) { // does not exist for this user name properties.setproperty (Username, password); // generate a configuration file properties.store (new filewriter (file), "users"); socketout.write ("Registered successfully: \ r \ n "); } else { // There is a user name socketout.write ("User name has been registered, please re-enter \ r \ n"); } Socketout.flush (); } } catch (exception e) { e.printstacktraCE (); } } } public static void main ( String[] args) throws IOException { Serversocket serversocket = new serversocket (9090); while (True) { Socket socket = serversocket.accept (); new loginserver (socket). Start (); } }}
This article from "Small Fish Blog" blog, declined reprint!
Java Network Programming 2