Chapter One Introduction to Java Network programming _echo

Source: Internet
Author: User

Server-side program Echoserver.java

Import java.io.*;
Import java.net.*;
public class Echoserver {
private int port=8000;
Private ServerSocket serversocket;

Public Echoserver () throws IOException {
ServerSocket = new ServerSocket (port);
System.out.println ("server start");
}

public string echo (string Msg) {
return "echo:" + msg;
}

Private PrintWriter getwriter (socket Socket) throws ioexception{
OutputStream socketout = Socket.getoutputstream ();
return new PrintWriter (socketout,true);
}
Private BufferedReader Getreader (socket Socket) throws ioexception{
InputStream Socketin = Socket.getinputstream ();
return new BufferedReader (new InputStreamReader (socketin));
}

public void service () {
While (true) {
Socket socket=null;
try {
Socket = Serversocket.accept (); Waiting for customer to connect
System.out.println ("New Connection accepted"
+socket.getinetaddress () + ":" +socket.getport ());
BufferedReader BR =getreader (socket); Du Qu
PrintWriter pw = Getwriter (socket);

String msg = null;
While (msg = Br.readline ()) = null) {
System.out.println (msg);
Pw.println ("--------------this is the data uploaded from the server--------------");
If (msg.equals ("bye"))//if the customer sends a message as "bye", end the communication
Break
}
}catch (ioexception E) {
E.printstacktrace ();
}finally {
try{
If (socket!=null) socket.close (); Disconnect Connection
}catch (ioexception E) {e.printstacktrace ();}
}
}
}

public static void main (String Args[]) throws IOException {
New Echoserver (). Service ();
}
}
Client program Echoclient.java
Import java.net.*;
Import java.io.*;
Import java.util.*;
public class Echoclient {
Private String host= "localhost";
private int port=8000;
Private Socket socket;

Public Echoclient () throws ioexception{
Socket=new Socket (host,port);
}
public static void main (String Args[]) throws ioexception{
New Echoclient (). Talk ();
}
Private PrintWriter getwriter (socket Socket) throws ioexception{
OutputStream socketout = Socket.getoutputstream ();
return new PrintWriter (socketout,true);
}
Private BufferedReader Getreader (socket Socket) throws ioexception{
InputStream Socketin = Socket.getinputstream ();
return new BufferedReader (new InputStreamReader (socketin));
}
public void Talk () throws IOException {
try{
BufferedReader Br=getreader (socket);
PrintWriter Pw=getwriter (socket);
BufferedReader localreader=new bufferedreader (new inputstreamreader (system.in));
String msg=null;
System.out.println ("$$$$$$$$$$$$$$$$$$$");
While ((msg=localreader.readline ())!=null) {

Pw.println (msg+ "aaaaaaaaaaaaa"); Data passed to the server
Thread.Sleep (3000);
System.out.println ("br.readline ()" +br.readline ()); Read the Server's data

If (msg.equals ("bye"))
Break
}
}catch (Exception E) {
E.printstacktrace ();
}finally{
Try{socket.close ();} Catch (ioexception e) {e.printstacktrace ();}
}
}
}


/****************************************************
* Sun Weichen *
* Source: <<java Network programming Fine >> *
* Technical Support website: www.javathinker.org *
***************************************************/


Chapter I Introduction to Java Network programming _echo

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.