[JAVA100 example]043, a pair of simple servers and clients

Source: Internet
Author: User

Import java.net.*;


import java.io.*;


/**


* <p>title: Simple Server service side </p>


* <p>description: This is a simple server-side program </p>


* <p>copyright:copyright (c) 2003</p>


* <p>Filename:SampleServer.java</p>


* @version 1.0


*/


public class sampleserver{


public static void Main (string[] arges) {


try{


int port = 8888;


//Use 8888 port to create a serversocket


ServerSocket mysocket = new ServerSocket (port);


//Waiting to listen for a client connection


Socket SK = mysocket.accept ();


//Input Cache


BufferedReader in = new BufferedReader (


New InputStreamReader (Sk.getinputstream ()));


//Output cache


PrintWriter out = new PrintWriter (


New BufferedWriter (


New OutputStreamWriter (


Sk.getoutputstream ()), true);


//Print incoming information sent by the client


System.out.println ("Client Information:" +in.readline ());


//Back to client information


out.println ("Hello, I am the server.") I use the port number: "+port";


}catch (Exception e) {


System.out.println (e);


  }


 }


}


import java.io.*;


import java.net.*;


/**


* <p>title: Simple Server client </p>


* <p>description: This program is a simple client, used to connect with the server </p>


* <p>copyright:copyright (c) 2003</p>


* <p>Filename:SampleClient.java</p>


* @version 1.0


*/


public class sampleclient{


public static void Main (string[] arges) {


try{


//Get an IP. Null indicates native


inetaddress addr = Inetaddress.getbyname (null);


//Open port 8888, establish a connection with the server


Socket SK = new socket (addr, 8888);


//Cache input


BufferedReader in = new BufferedReader (


New InputStreamReader (Sk.getinputstream ()));


//Cache output


PrintWriter out = new PrintWriter (


New BufferedWriter (


New OutputStreamWriter (


Sk.getoutputstream ()), true);


//Send information to the server


out.println ("Hello!)" ");


//Receive server information


System.out.println (In.readline ());


}catch (Exception e) {


System.out.println (e);


  }


 }


}

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.