Java Third job: Java and socket implementations

Source: Internet
Author: User

Client program 1:chatclient.java<pre name= "code" class= "java" >import java.io.*;import Java.net.*;import Java.applet.applet;public class Chatserver {public static void main (String[]args) {Try{serversocket Server = null;try{ Server = new ServerSocket (4000); System.out.println ("Ready. Exit input Bye ");} catch (Exception e) {System.out.println ("Can not listen to" + E);} Socket socket = Null;try{socket = Server.accept ();} catch (Exception e) {System.out.println ("Error." + e);} String Line; BufferedReader is = new BufferedReader (New InputStreamReader (Socket.getinputstream ())); PrintWriter os = new PrintWriter (Socket.getoutputstream ()); BufferedReader sin = new BufferedReader (new InputStreamReader (system.in)); System.out.println ("Client:" + is.readline ()), line = Sin.readline (), while (! Line.equals ("Bye")) {os.println (line); Os.flush (); System.out.println ("Server:" + line);  System.out.println ("Client:" + is.readline ()); line = Sin.readline (); }os.close (); Is.close (); Socket.close (); Server.close ();} catch (Exception e) {System.ouT.println ("Error:" + E);}}} 
Client program 2:chatserver.java
Import Java.io.*;import java.net.*;p ublic class Cgatclient {public static void main (String[]args) {Try{socket Socket = new Socket ("127.0.0.1", 4000); System.out.println ("Enter what you want to say, if you want to exit input bye"); BufferedReader sin = new BufferedReader (new InputStreamReader (system.in)); PrintWriter os = new PrintWriter (Socket.getoutputstream ()); BufferedReader is = new BufferedReader (New InputStreamReader (Socket.getinputstream ())); String readline;readline = Sin.readline (); while (! readline.equals ("Bye")) {os.println (readline); Os.flush (); System.out.println ("catclient:" + readline); System.out.println ("Server:" + is.readline ()); readLine = Sin.readline ();} Os.close (); Is.close (); Socket.close ();} catch (Exception e) {System.out.println ("Error" + e);}}}
The result of the implementation is that whatever dialog box is compiled in that program, you enter what you want to say in it, and it will be displayed in another dialog box. These two small programs achieve simple dialogue and communication.
At the end of the dialog box, enter bye, which is the end of the session.

Java Third job: Java and socket implementations

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.