Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – ServerSocket and Socket communication example

Java Examples - ServerSocket and Socket Communication

Java Examples Java Examples

The following example demonstrates how a client sends a message to the server; the server receives the message, reads the output, writes it back to the client, and the client receives the output.

1. Set up the server side

Server-side code:

Server.java file

import

java

.

io

.

BufferedReader

;

import

java

.

io

.

BufferedWriter

;

import

java

.

io

.

IOException

;

import

java

.

io

.

InputStreamReader

;

import

java

.

io

.

OutputStreamWriter

;

import

java

.

net

.

ServerSocket

;

import

java

.

net

.

Socket

;

public

class

Server

{

public

static

void

main

(

String

[

]

args

)

{

try

{

ServerSocket

ss

=

new

ServerSocket

(

8888

)

;

System

.

out

.

println

(

"

Starting server....

"

)

;

Socket

s

=

ss

.

accept

(

)

;

System

.

out

.

println

(

"

Client:

"

s

.

getInetAddress

(

)

.

getLocalHost

(

)

"

Connected to the server

"

)

;

BufferedReader

br

=

new

BufferedReader

(

new

InputStreamReader

(

s

.

getInputStream

(

)

)

)

;

//

Read the message sent by the client

String

mess

=

br

.

readLine

(

)

;

System

.

out

.

println

(

"

Client:

"

mess

)

;

BufferedWriter

bw

=

new

BufferedWriter

(

new

OutputStreamWriter

(

s

.

getOutputStream

(

)

)

)

;

bw

.

write

(

mess

"

\n

"

)

;

bw

.

flush

(

)

;

}

catch

(

IOException

e

)

{

e

.

printStackTrace

(

)

;

}

}

}

The output of the above code is:


启动服务器....

2. Set up the client side

Client code:

Client.java file

import

java

.

io

.

BufferedReader

;

import

java

.

io

.

BufferedWriter

;

import

java

.

io

.

IOException

;

import

java

.

io

.

InputStream

;

import

java

.

io

.

InputStreamReader

;

import

java

.

io

.

OutputStream

;

import

java

.

io

.

OutputStreamWriter

;

import

java

.

net

.

Socket

;

import

java

.

net

.

UnknownHostException

;

public

class

Client

{

public

static

void

main

(

String

[

]

args

)

{

try

{

Socket

s

=

new

Socket

(

"

127.0.0.1

"

,

8888

)

;

//

Build the IO

InputStream

is

=

s

.

getInputStream

(

)

;

OutputStream

os

=

s

.

getOutputStream

(

)

;

BufferedWriter

bw

=

new

BufferedWriter

(

new

OutputStreamWriter

(

os

)

)

;

//

Send a message to the server

bw

.

write

(

"

Test the client-server communication: the server receives the message and returns it to the client

\n

"

)

;

bw

.

flush

(

)

;

//

Read the message returned by the server

BufferedReader

br

=

new

BufferedReader

(

new

InputStreamReader

(

is

)

)

;

String

mess

=

br

.

readLine

(

)

;

System

.

out

.

println

(

"

Server:

"

mess

)

;

}

catch

(

UnknownHostException

e

)

{

e

.

printStackTrace

(

)

;

}

catch

(

IOException

e

)

{

e

.

printStackTrace

(

)

;

}

}

}

The output of the above code is:


服务器:测试客户端和服务器通信,服务器接收到消息返回到客户端

Java Examples Java Examples