Java Network Programming Learning Java Chat program code sharing _java

Source: Internet
Author: User
Tags flush

Copy Code code as follows:

Package com.neusoft.edu.socket;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.PrintWriter;
Import Java.net.ServerSocket;
Import Java.net.Socket;
/**
* Server-side code
* Get the information sent by the client, display and return the corresponding reply
* 1, create ServerSocket objects
* 2, call the Accept method to obtain the client connection
* 3, use the input stream to read the data sent by the client
* 4, using output to write data to the client
* 5, close the corresponding object
* @author L
*
*/
public class Chatserver {
/**
* @param args
*/
public static void Main (string[] args) {
try {
1, create the ServerSocket object, 8875 for the custom port number
ServerSocket Server = new ServerSocket (8857);

Simple Tips
System.out.println ("Waiting for client to connect ...");

2. Get Client Connection
Socket client = Server.accept ();

Get information about the client
System.out.println (Client.getinetaddress (). Gethostaddress () + "connected ...");

3.1. Define input and output stream objects
BufferedReader in = new BufferedReader (
New InputStreamReader (
Client.getinputstream ()));

Used to get data entered from the console and send that data to the client
BufferedReader inbyserver = new BufferedReader (
New InputStreamReader (system.in));

PrintWriter out = new PrintWriter (Client.getoutputstream (), true);

Data read from
String data = null;
String answer = NULL;

Looping and communicating with the client
Todo
{
3.2, read the data sent by the client
data = In.readline ();

To display read data on the server side
SYSTEM.OUT.PRINTLN ("Client sends message:" + data);

Get the information that the server side wants to send to the client
System.out.print ("server-side Reply client:");
Answer = Inbyserver.readline ();

3.3, write data to the client
Out.println (answer);
Out.flush ();
}while (!) Bye ". Equals (data));

4, close the relevant resources
Out.flush ();
In.close ();
Inbyserver.close ();
Out.close ();

Close the Socket object
Client.close ();
Server.close ();

SYSTEM.OUT.PRINTLN ("server-side shutdown ...");
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

Copy Code code as follows:

Package com.neusoft.edu.socket;

Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.PrintWriter;
Import Java.net.Socket;
Import java.net.UnknownHostException;

/**
* Chat Client
* 1, create socket object
* 2, write data
* 3, read data sent from the server side
* 4, close
* @author L
*
*/
public class ChatClient {

/**
* @param args
*/
public static void Main (string[] args) {
try {
1, create the Socket object, ("192.168.1.107", 8857) are host IP and port number, two class port number to be consistent
Socket client = new Socket ("192.168.1.107", 8857);

2.1. Create input and output stream objects
BufferedReader in = new BufferedReader (
New InputStreamReader (
Client.getinputstream ()));

PrintWriter out = new PrintWriter (Client.getoutputstream (), true);

BufferedReader inbyclient = new BufferedReader (
New InputStreamReader (system.in));

Server-side data sent
String data = null;
Data that the client writes to the server side
String answer = NULL;

Todo
{
2.2, the client's read and write operation
System.out.print ("You say:");
Get the data to send to the server side
Answer = Inbyclient.readline ();
Writing data to the server side
Out.println (answer);
Out.flush ();

Getting data sent from the server side
data = In.readline ();
Output data obtained from the server side
SYSTEM.OUT.PRINTLN ("Server-side return information is:" + data);
}while (!) Bye ". Equals (data));

3. Close
In.close ();
Out.close ();
Inbyclient.close ();
Client.close ();

SYSTEM.OUT.PRINTLN ("Client shutdown ...");
catch (Unknownhostexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

}

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.