In Java, the socket creation server is in instant chat with the client

Source: Internet
Author: User
Tags sendmsg

package tool;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.serversocket;import java.net.socket;import  java.net.unknownhostexception;import java.util.scanner;/** *  Server  *  @author   mr.zhang *  the process of establishing a connection between the server and the client  *1. Create a server-side object, and then specify the port  *2 to listen on. The server listens for requests made to the specified port  *3. Client Send Request  * 4. The server receives the request and establishes the connection  *5. server close connection  */public class ServerDemo {/** *  @param   args *  @throws  ioexception  */public static void main (String[]  args)  throws ioexception {serversocket ();//server on, Comment client//client ();//client open, Comment serversocket}/**  *  server-side  *  @throws  ioexception */public static void serversocket ()  THROWS IOEXCEPTION{SYSTEM.OUT.PRINTLN ("Server-side Ready"); SErversocket server=new serversocket (9999);//server listener request while (true) {socket socket=server.accept (); SYSTEM.OUT.PRINTLN ("Server obtains Connection");/** * 1. The service sends a welcome message  *///through the socket to get the stream outputstream os= Socket.getoutputstream (); Printwriter pw=new printwriter (OS);p w.println ("Server-side send: Welcome! ");p W.flush ();/** *  uses BufferedReader to receive client information  */inputstream is=socket.getinputstream (); Bufferedreader br=new bufferedreader (New inputstreamreader (IS)); String msg=new string ();/** *  what is blocking?  *  blocking is because the data in the stream does not meet the acquired conditions, the input stream waits for the data that satisfies the condition, which causes the program to wait, that is, the blocking  *  workaround: 1. io: Let the read data meet the Read condition    2.nio */while ((Msg=br.readline ())!=null) {if (Msg.equalsignorecase ("Quit")) {// Disconnect server-side connection socket.close (); break;} if (Msg.equalsignorecase ("Quitall")) {//Disconnects server-side from Client connection socket.close (); Server.close (); break;} SYSTEM.OUT.PRINTLN ("The data emitted by the client is +msg");/** *  the server sends a confirmation message  */scanner scan=new scanner ( system.in); String sendmsg=sCan.next ();p w.println (sendmsg);p W.flush ();}} /** *  client  *  @throws  IOException  *  @throws  unknownhostexception   */public static void client ()  throws UnknownHostException,  IOEXCEPTION{//uses the socket to make a request to the specified machine and port socket socket=new socket ("192.168.1.101", 9999);/** * 2. The client receives a welcome message from the server and sends the data  *///through the socket to get the input stream Inputstream is=socket.getinputstream (); Bufferedreader br=new bufferedreader (New inputstreamreader (IS));//continue to receive server-side information string content =new string (); while ((Content=br.readline ())!=null) {System.out.println ("data sent by the server" +content); Scanner scan=new scanner (system.in); String sendmsg=scan.next (); Printwriter pw=new printwriter (Socket.getoutputstream ());p w.println (sendmsg);p W.flush ();}}


This article is from the "Mr.zhang" blog, make sure to keep this source http://zhangzhipeng.blog.51cto.com/9115459/1573880

In Java, the socket creation server is in instant chat with the client

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.