Java Examples - ServerSocket and Socket Communication
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
- The server creates a ServerSocket for communication
- The server creates a Socket to accept client connections
- Create an IO input stream to read the data sent by the client
- Create an IO output stream to send data messages to the client
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
- Create Socket communication and set the IP and Port of the server
- Create an IO output stream to send data messages to the server
- Create an IO input stream to read the data messages sent by the server
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