Java Network Programming-server-side and client-to-peer messaging

Source: Internet
Author: User

Introduction

??

In order to learn Java network programming, with a qq ( Dick Silk version ) as an example practiced hand, recorded the development process of the Dick Silk version of QQ , here we think we have mastered the basic part of the network, that is, the HTTP protocol,TCP/IP protocol, etc., on this basis we start our Java Network part of the learning, we want to know how the server and the client is how to communicate, first we want to understand the Java One of the most important things in network knowledge-Socket

??

A probe into sockets

??

ServerSocket and sockets

??

First, the server side needs to usejava.netUnder the bagServerSocketClass, an instance of the class that listens for connections waiting on the client, and the client needs to use theNetUnder the bagSocketClass, which is used to send connection requests to the server side, and how the client connects to the server, first the client'sSocketinstance to make the server-sideIPAddress and port, how the server-side connects to the client, the server-sideServerSocketClass ofAccepthas been listening for connection requests from the client, and if not, it has been blockedAcceptThis code here(Can be inSocket s=ss.accept ();This line of code is printed after a sentence, you can find that the program does not execute to this line of code), the following code1Description Server-sideServerSocketThe usage, code2Description ClientSocketUsage of

??

Code 1

??

Create a class named Server, create instances such as ServerSocket in the constructor of the class

??

public class MyServer1 {

Public MyServer1 ()

{

// on 9999 port monitoring

ServerSocket ss=new ServerSocket (9999);

// wait for a client to connect, the function returns a scoket connection

Socket s=ss.accept ();

}

??

}

??

Code 2

??

Create a class named Client

??

public class MyClient1 {

Public MyClient1 ()

{

//socket, is to connect a server side,127.0.0.1 represents the server's Ip 9999 represents the port number

Socket s=new socket ("127.0.0.1", 9999);

}

}

??

Server and Client Interoperability information

??

Server side and the client is how to send information, use or java.io package of things, the server to read the message from the client, use to inputstreamreader,BufferedReader , where inputstreamreader input is the input stream returned by the Socket.getinputstream () method, to send information to the client using printwriter , where the input of printwriter is the output stream returned by the Socket.getoutputstream () method, with the following specific usage

??

Client sends information

// If the s connection is successful, you can send data to the server

// We write to s via pw true indicates refresh stream

PrintWriter pw=new PrintWriter (S.getoutputstream (), true);

pw.println ("Hello Server,I ' m Client");

??

Server-Side Accept information

// to read the data passed in s

InputStreamReader isr=new InputStreamReader (S.getinputstream ());

BufferedReader br=new BufferedReader (ISR);

// line read

String Info=br.readline ();

System.out.println (" the server received the information is," +info);

??

The server then sends the message to the client

PrintWriter pw=new PrintWriter (S.getoutputstream (), true);

pw.println ("Hello Client,I ' m Server");

??

The client then receives server-side information

InputStreamReader isr=new InputStreamReader (S.getinputstream ());

BufferedReader br=new BufferedReader (ISR);

// line read

String Response=br.readline ();

System.out.println (" the message received by the client is," +response);

??

Server and client Interop information ( input from console )

??

Before the server and the client mutually sends the information to be written to die in pw.println , we slightly modifies, from the console input

??

The connection was established in the same way, and the way to read and send the information is similar, just one more need to enter information from the console

??

Server-side code

??

For example, the server-side accepts the message from the client is still the same, the input stream returned from the Socket.getinputstream () method as the input of the InputStreamReader

??

But the server also needs a InputStreamReader accept input from system.in to receive information from the console that you want to send to the client

??

// Accept the information sent by the client

InputStreamReader isr=new InputStreamReader (S.getinputstream ());

BufferedReader br=new BufferedReader (ISR);

// Print the information sent by the client

String Infofromclient=br.readline ();

System.out.println (" client sends " +infofromclient);

??

// receive information entered from the console

InputStreamReader isr2=new InputStreamReader (system.in);

BufferedReader br2=new BufferedReader (ISR2);

// print information entered from the console

System.out.println (" Enter information to be sent to the client ");

String Response=br2.readline ();

??

The client's code

??

And the client code is similar

??

InputStreamReader isr=new InputStreamReader (system.in);

BufferedReader br=new BufferedReader (ISR);

??

InputStreamReader isr2=new InputStreamReader (S.getinputstream ());

BufferedReader br2=new BufferedReader (ISR2);

??

Server and client Interoperability information ( input from Chat window )

??

With the first foundation, this part just turns from console input to reading text from the jtextarea domain, and here we think that there is already a Java interface base for JFrame to understand

??

This part we want the message from the client to appear in the Chat window, not the console, then

System.out.println (" client sends " +infofromclient); Substitute for jta.append (" client sends " +info+ "\ r \ n"); Can

??

In addition, we want the client to send the server message also from the Chat window input, then we need to inherit ActionListener, implementation when the Click Send button, read the text of the jtextfield field, the specific implementation of the following

??

String Info=jtf.gettext ();

PW.PRINTLN (info);

// Empty the contents of the Send box

Jtf.settext ("");

??

??

Java Network Programming-server-side and client-to-peer messaging

Related Article

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.