Multi-client sign-on (TCP-based streaming socket socket programming)

Source: Internet
Author: User

1. Serialization of objects

Package Com.ljb.app.socket;import java.io.serializable;/** * User class (for serialization) * @author LJB * @version March 12, 2015 */public class  User implements serializable{private string name; private string password; public string GetName () {return name;} public void SetName (String name) {this.name = name;} public string Getpasswor D () {return password;} public void SetPassword (String password) {this.password = password;}}

2, the Client

 package com.ljb.app.socket;import java.io.BufferedReader;import java.io.IOException; import java.io.inputstream;import java.io.inputstreamreader;import java.io.objectoutputstream; import java.io.outputstream;import java.io.printwriter;import java.net.socket;import  java.net.unknownhostexception;/** *  Client (simulates customer login and receives service-side response)  *  @author  ljb * @ version 2015 March 11  */public class LoginClient { /**  *  @param  args  */ public static void main (String[] args)  {  try  {   //  Establish a client Socket connection, specify the location of the server, and Port    Socket socket =  New socket ("localhost", 8800);      //  get Socket read/write stream     Outputstream os = socket.getoutputstream ();      //  Serializing an Object    objectoutPutstream oos = new objectoutputstream (OS);      //  Get input stream    inputstream is = socket.getinputstream ();    Bufferedreader br = new bufferedreader (New inputstreamreader (IS));       //  write Operations    user user = new user ();    User.setname ("Tom2");    user.setpassword ("1234567");    oos.writeobject (user);    socket.shutdownoutput ();      //  Receive server response     String reply = null;   while  (!) ( (Reply = br.readline ())  == null))  {    system.out.println ("I am the client, The server response is: " + reply);   }      //  Close stream     br.close ();    is.close ();    oos.close ();   os.close ();    socket.close ();  } catch  ( unknownhostexception e)  {   e.printstacktrace ();  } catch  ( ioexception e)  {   e.printstacktrace ();   } }}

3, server-side

 package com.ljb.app.socket;import java.io.BufferedReader;import java.io.IOException; import java.io.inputstream;import java.io.inputstreamreader;import java.io.objectinputstream; Import java.io.outputstream;import java.io.printwriter;import java.net.serversocket;import  java.net.Socket;/** *  Server (receiving client login information and responding)  *  @author  LJB *  @version  2015 March 11  */public class LoginServer { /**  *  @param  args   */ public static void main (String[] args)  {   try  {   //  set up a server binding specified port socket (serversocket) and start listening    ServerSocket  Serversocket = new serversocket (8800);      //  use Accept () Method blocking waits for monitoring, obtaining a new connection    socket socket = null;      //   Number of customers    int num = 0;      //  Continuous monitoring    while  (true)  {     socket = serversocket.accept ();         serverthread serverthread = new serverthread (socket);     Serverthread.start ();        num++;     System.out.println ("Number of Customers:"  + num);    //  IP information for customers      Inetaddress is = socket.getinetaddress ();         //   Customer's IP address     string ip = is.gethostaddress ();     System.out.println ("Customer's IP Address:"  + IP);        //  the customer's host name     string hostname = is.gethostname ();     System.out.println ("Customer name is:"  + hostname);   }  } catch  (ioexception e)  {   e.printstacktrace ();   }  }}

4, service thread

package com.ljb.app.socket;import java.io.ioexception;import java.io.inputstream;import  java.io.objectinputstream;import java.io.outputstream;import java.io.printwriter;import  java.net.serversocket;import java.net.socket;/** *  Create a service thread to continuously respond to customer requests  *  @author   ljb *  @version  2015 March 12  */public class serverthread extends thread  { Socket socket = null;  public ServerThread  (Socket socket )  {  this.socket = socket; }  //  respond to customer requests  public void  run  ()  {  try {   //  get input stream    inputstream  is = socket.getinputstream ();      //  to get the stream, you can deserialize the object     objectinputstream ois = new objectinputstream (IS);       //  Get output stream  &nbsP; outputstream os = socket.getoutputstream ();    printwriter pw =  new printwriter (OS);      //  read user input information    user  user =  (user) ois.readobject ();    system.out.println ("Users info:"  +  User.getname ()  +  " "  + user.getpassword ());       //   Response Information    String reply =  "Welcome";    pw.write (Reply);    pw.flush ();      //  close stream    pw.close ();    os.close ();    ois.close ();    is.close ();    Socket.close ();  } catch  (ioexception e)  {   e.printstacktrace ();   } catch  (classnotfoundexception e)  {   e.printstacktrace ();   }  }}

The client passes two different objects

Operation Result:

Client:

I am the client, the server response is: Welcome

Service side:

Number of customers: 1
User information: Tom 123456
Number of customers: 2
User information: Tom2 1234567

Multi-client sign-on (TCP-based streaming socket socket programming)

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.