1、建立基于TCP的网络通讯程序----示例演示
-
示例一:完成客户端想服务器发送数据,服务器接收到客户端发送来的数据。
-
示例二:完成客户端持续想服务器发送数据,服务器持续接收客户端发送来的数据。
-
示例三:完成客户端持续向服务器发送数据,还能接收服务器返回的信息,服务器持续接收到客户端发送来的数据,还可以向客户端返回信息。
-
示例四:多客户端,配置一台服务器,独立运行。
//示例一:完成客户端想服务器发送数据,服务器接收到客户端发送来的数据。
//客户端:
public class SocketTest {
public static void main(String[] args) throws Exception {
//接收控制台数据的输入流对象
BufferedReader buff = null;
//定义保存服务器地址的对象
InetAddress serverIp = null;
//定义连接服务器的端口
int serverPort = 3000;
//定义创建客户端对象Socket
Socket client = null;
//定义发送信息的输出流对象
OutputStream os = null;
//定义被保存的数据
String info = "";
//下面为实例化
serverIp = InetAddress.getLocalHost();
client = new Socket(serverIp,serverPort);
os = client.getOutputStream();
System.out.println("请输入需要发送的数据:");
buff = new BufferedReader(new InputStreamReader(System.in));
info = buff.readLine();
os.write(info.getBytes());
buff.close();
os.close();
client.close();
}
}
//服务器端:
public class ServerSocketTest {
public static void main(String[] args) throws IOException {
//定义服务器的端口号
int serverPort = 3000;
//定义服务器的对象
ServerSocket server = null;
//定义保存连接到服务器的客户端对象
Socket client = null;
//定义服务器接收客户端的输入流对象
InputStream is = null;
server = new ServerSocket(serverPort);
System.out.println("服务器已启动,等待客户端连接---");
client = server.accept();
is = client.getInputStream();
byte[] bytes = new byte[1024];
int len = is.read(bytes);
//将读取来的保存字节数组中的数据转换成字符串
String string = new String(bytes, 0, len);
System.out.println("服务器接收到客户端的数据:" + string);
server.close();
client.close();
is.close();
}
}
客户端发送数据 Hello,World!:
服务器端接收到的数据 Hello,World!:
---手动分割线---
//示例二:完成客户端持续想服务器发送数据,服务器持续接收客户端发送来的数据。
//客户端
public class SocketTest {
public static void main(String[] args) throws Exception {
//接收控制台数据的输入流对象
BufferedReader buff = null;
//定义保存服务器地址的对象
InetAddress serverIp = null;
//定义连接服务器的端口
int serverPort = 3000;
//定义创建客户端对象Socket
Socket client = null;
//定义发送信息的输出流对象
OutputStream os = null;
//定义被保存的数据
String info = "";
boolean flag = true;
serverIp = InetAddress.getLocalHost();
client = new Socket(serverIp, serverPort);
os = client.getOutputStream();
buff = new BufferedReader(new InputStreamReader(System.in));
//下面为实例化
while(flag) {
System.out.println("请输入需要发送的数据:");
info = buff.readLine();
os.write(info.getBytes());
if(info.equals("exit")){
flag = false;
}
}
buff.close();
os.close();
client.close();
}
}
//服务器端
public class ServerSocketTest {
public static void main(String[] args) throws IOException {
//定义服务器的端口号
int serverPort = 3000;
//定义服务器的对象
ServerSocket server = null;
//定义保存连接到服务器的客户端对象
Socket client = null;
//定义服务器接收客户端的输入流对象
InputStream is = null;
//定义持续读取数据的变量
boolean flag = true;
server = new ServerSocket(serverPort);
System.out.println("=======服务器已启动,等待客户端连接=======");
client = server.accept();
is = client.getInputStream();
byte[] bytes = new byte[1024];
while (flag) {
int len = is.read(bytes);
//将读取来的保存字节数组中的数据转换成字符串
String string = new String(bytes, 0, len);
System.out.println("服务器接收到客户端的数据:" + string);
if (string.equals("exit")) {
flag = false;
}
}
server.close();
client.close();
is.close();
}
}
客户端发送数据:
服务器端接收到的数据:
---手动分割线---
//示例三:完成客户端持续向服务器发送数据,还能接收服务器返回的信息,服务器持续接收到客户端发送来的数据,还可以向客户端返回信息。
//客户端
public class SocketTest {
public static void main(String[] args) throws Exception {
//接收控制台数据的输入流对象
BufferedReader buff = null;
//定义保存服务器地址的对象
InetAddress serverIp = null;
//定义连接服务器的端口
int serverPort = 3000;
//定义创建客户端对象Socket
Socket client = null;
//定义发送信息的输出流对象
OutputStream os = null;
//定义接收数据的输入流对象
InputStream is = null;
//定义被保存的数据
String info = "";
//定义保存数据的字节数组
byte[] bytes = new byte[1024];
boolean flag = true;
serverIp = InetAddress.getLocalHost();
client = new Socket(serverIp, serverPort);
os = client.getOutputStream();
buff = new BufferedReader(new InputStreamReader(System.in));
is = client.getInputStream();
//下面为实例化
while(flag) {
//发送
System.out.println("请输入需要发送的数据:");
info = buff.readLine();
os.write(info.getBytes());
//接收
int len = is.read(bytes);
String string = new String(bytes, 0, len);
System.out.println("服务器返回的结果是:" + string);
if(info.equals("exit")){
flag = false;
}
}
buff.close();
os.close();
client.close();
is.close();
}
}
//服务器端
public class ServerSocketTest {
public static void main(String[] args) throws IOException {
//定义服务器的端口号
int serverPort = 3000;
//定义服务器的对象
ServerSocket server = null;
//定义保存连接到服务器的客户端对象
Socket client = null;
//定义服务器接收客户端的输入流对象
InputStream is = null;
//定义写出数据的输出流对象
OutputStream os = null;
//定义持续读取数据的变量
boolean flag = true;
server = new ServerSocket(serverPort);
System.out.println("=======服务器已启动,等待客户端连接=======");
client = server.accept();
is = client.getInputStream();
byte[] bytes = new byte[1024];
os = client.getOutputStream();
while (flag) {
int len = is.read(bytes);
//将读取来的保存字节数组中的数据转换成字符串
String string = new String(bytes, 0, len);
System.out.println("服务器接收到客户端的数据:" + string);
if (string.equals("exit")) {
flag = false;
}else {
//写出
string = "server==" + string;
}
os.write(string.getBytes());
}
server.close();
client.close();
is.close();
os.close();
}
}
客户端发送数据:
服务器端接收到的数据:
---手动分割线---
//示例四:多客户端,配置一台服务器,独立运行。
//客户端:
public class SocketTest {
public static void main(String[] args) throws Exception {
//接收控制台数据的输入流对象
BufferedReader buff = null;
//定义保存服务器地址的对象
InetAddress serverIp = null;
//定义连接服务器的端口
int serverPort = 3000;
//定义创建客户端对象Socket
Socket client = null;
//定义发送信息的输出流对象
OutputStream os = null;
//定义接收数据的输入流对象
InputStream is = null;
//定义被保存的数据
String info = "";
//定义保存数据的字节数组
byte[] bytes = new byte[1024];
boolean flag = true;
serverIp = InetAddress.getLocalHost();
client = new Socket(serverIp, serverPort);
os = client.getOutputStream();
buff = new BufferedReader(new InputStreamReader(System.in));
is = client.getInputStream();
//下面为实例化
while(flag) {
//发送
System.out.println("请输入需要发送的数据:");
info = buff.readLine();
os.write(info.getBytes());
//接收
int len = is.read(bytes);
String string = new String(bytes, 0, len);
System.out.println("服务器返回的结果是:" + string);
if(info.equals("exit")){
flag = false;
}
}
buff.close();
os.close();
client.close();
is.close();
}
}
//服务器端:
public class ServerSocketTest {
public static void main(String[] args) throws IOException {
//定义服务器的端口号
int serverPort = 3000;
//定义服务器的对象
ServerSocket server = null;
//定义保存连接到服务器的客户端对象
Socket client = null;
//定义持续接收客户端的控制变量
boolean getFlag = true;
server = new ServerSocket(serverPort);
System.out.println("=======服务器已启动,等待客户端连接=======");
while (getFlag) {
client = server.accept();
ServerThread serverThread = new ServerThread(client);
Thread thread = new Thread(serverThread);
thread.start();
}
server.close();
}
}
//使用多线程
public class ServerThread implements Runnable {
//构造方法传值
private Socket client = null;
//定义服务器接收客户端的输入流对象
InputStream is = null;
//定义写出数据的输出流对象
OutputStream os = null;
//定义持续读取数据的变量
boolean flag = true;
//保存数据的字节数组
byte[] bytes = new byte[1024];
public ServerThread(Socket client) {
this.client = client;
}
@Override
public void run() {
try {
is = client.getInputStream();
os = client.getOutputStream();
while (flag) {
int len = is.read(bytes);
//将读取来的保存字节数组中的数据转换成字符串
String string = new String(bytes, 0, len);
System.out.println("服务器接收到客户端的数据:" + string);
if (string.equals("exit")) {
flag = false;
} else {
//写出
string = "server==" + string;
}
os.write(string.getBytes());
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端一发送:
客户端二发送:
客户端三发送:
服务端接收: