Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

peer-topeer Networks phần 4 ppt
MIỄN PHÍ
Số trang
30
Kích thước
566.1 KB
Định dạng
PDF
Lượt xem
1802

peer-topeer Networks phần 4 ppt

Nội dung xem thử

Mô tả chi tiết

P1: OTE/SPH P2: OTE

SVNY285-Loo October 18, 2006 7:8

70 7. Java Network Programming

need more flexibility in communication, we need to use more complex methods.

Socket communication is one of them. It is similar to a phone conversation. The

communication process with phones consists of the following steps:

Dial the number.

Wait for the other side to pick up the phone.

Talk to each other once the connection is established.

Hang up the phone.

It has the following characteristics:

The connection must be established before the communication.

Connection is maintained even if the line is idle. For example, your friend asks a

question in the phone conservation. You need to think for several seconds before

you can answer so there is an idle period.

The connection is closed only after the communication process is completed.

7.3.1 Client Side Program—Socket

The following client program sends a request to establish connection with the

server. It despatches a simple message ‘How are you?’ to the server and waits for

the answer. It then displays the answer on the screen. The program is presented in

Fig. 7.3.

First try block. —

The connection is established with the following lines. The first line sends a request

for connection to the server. The second and third lines initialise the PrintStream

object (for output) and BufferReader object (for input).

powerSocket = new Socket(IP, 3333);

out = new PrintStream(powerSocket.getOutputStream());

in = new BufferedReader(new InputStreamReader(

powerSocket.getInputStream()));

The syntax for defining the socket is:

Socket nameOfSocket; :

nameOfSocket = new Socket(IP, 3333);

The syntax for defining the output object is:

PrintStream nameOfOutputObject =null ;

nameOfOutputObject = new PrintStream(nameOfSocket.getOutputStream());

The syntax for defining the input object:

BufferedReader nameOfInputObject =null;

nameOfInputObject = new BufferedReader(new InputStreamReader(

nameOfSocket.getInputStream()));

P1: OTE/SPH P2: OTE

SVNY285-Loo October 18, 2006 7:8

import java.net.*;

import java.io.*;

public class socketClient {

public static void main(String[] args) throws IOException {

String IP;

PrintStream out=null ;

BufferedReader in =null;

Socket powerSocket;

// ************ get parameter from command argument *******

if (args.length >= 1)

{

IP =args[0];

System.out.println(IP address: + IP);

}

else

{

IP = localhost;

}

// ************* connect to server ****************

try

{

System.out.println(connecting site: +IP);

powerSocket = new Socket(IP, 3333);

out = new PrintStream(powerSocket.getOutputStream());

in = new BufferedReader(new InputStreamReader(

powerSocket.getInputStream()));

}

catch (UnknownHostException e)

{

System.err.println(unknown host: +IP);

System.exit(1);

}

catch (IOException e)

{

System.err.println(connection to: +IP);

System.exit(1);

}

System.out.println(connection ok);

// *********** start to communicate *****************

out.println(How are you?); // transmit to power server

String answer;

if ((answer = in.readLine())!= null)

System.out.println(server’s answer: +answer);

} // end of main method

}

Figure 7.3. socketClient.java

71

P1: OTE/SPH P2: OTE

SVNY285-Loo October 18, 2006 7:8

72 7. Java Network Programming

Sending and receiving messages.—The following line sends a message to the

server:

out.println(How are you?); // transmit to power server

The following lines receive messages from the server:

String answer;

if ((answer = in.readLine())!= null)

System.out.println(server’s answer: +answer);

7.3.2 Server Side Program

This program is presented in Fig. 7.4.

First try block.—This program creates a socket with the following line:

serverSocket = new ServerSocket(3333);

Second try block.—The following statement accepts the connection request from

client.

powerSocket=serverSocket.accept();

Third try block.—The following lines initialise the input and output objects:

out = new PrintWriter(powerSocket.getOutputStream(), true);

in = new BufferedReader(new InputStreamReader(

powerSocket.getInputStream()));

Fourth try block.—The server receives a message from the client with the following

lines:

fromClient = in.readLine();

System.out.println (Client’s Message: +fromClient);

The server sends a message to the client with the following lines:

toClient = Thank you! I am fine.;

out.println(toClient);

7.3.3 Sequences of Operations

The sequences of operations in client and server are summarized in Table 7.1.

7.3.4 Testing 1

Programs can be tested with the following steps as in Fig. 7.6:

1. Type the following command in your server machine:

java socketServer

Tải ngay đi em, còn do dự, trời tối mất!