The third assignment of Java programming: Network programming

Source: Internet
Author: User
Tags sin

target of the job

Target 1:socket simple application. Input the textbook p238-230 sample 11-5 code into our Code editor, Debug and run (80 points).

P238 client program

Import Java.io.*;import java.net.*;p ublic class ChatClient {public static void main (String args[]) {Try{socket socket=new Socket ("127.0.0.1", 4001); 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 ("Client:" +readline); System.out.println ("Server:" +is.readline ()); Readline=sin.readline ();} Os.close (); Is.close (); Socket.close ();} catch (Exception e) {System.out.println ("Error" +e);}}



p238-230 Server-side programs

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 (4001); 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.print (line); Os.flush (); 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);}}


The third assignment of Java programming: Network 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.