Java--Socket通信(客户端服务端双向)
作者:zlQ_ 时间:2021-09-21 20:58:37
新建两个工程,一个客户端,一个服务端,先启动服务端再启动客户端
两个工程的读写操作线程类基本上完全相同
服务端:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static final int PORT = 8000;//监听的端口号
public static void main(String[] args) {
Server server = new Server();
server.init();
}
public void init() {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(PORT);
while (true) {
Socket client = serverSocket.accept();
//一个客户端连接就开户两个线程处理读写
new Thread(new ReadHandlerThread(client)).start();
new Thread(new WriteHandlerThread(client)).start();
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(serverSocket != null){
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
*处理读操作的线程
*/
class ReadHandlerThread implements Runnable{
private Socket client;
public ReadHandlerThread(Socket client) {
this.client = client;
}
@Override
public void run() {
DataInputStream dis = null;
try{
while(true){
//读取客户端数据
dis = new DataInputStream(client.getInputStream());
String reciver = dis.readUTF();
System.out.println("客户端发过来的内容:" + reciver);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(dis != null){
dis.close();
}
if(client != null){
client = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
* 处理写操作的线程
*/
class WriteHandlerThread implements Runnable{
private Socket client;
public WriteHandlerThread(Socket client) {
this.client = client;
}
@Override
public void run() {
DataOutputStream dos = null;
BufferedReader br = null;
try{
while(true){
//向客户端回复信息
dos = new DataOutputStream(client.getOutputStream());
System.out.print("请输入:\t");
// 键盘录入
br = new BufferedReader(new InputStreamReader(System.in));
String send = br.readLine();
//发送数据
dos.writeUTF(send);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(dos != null){
dos.close();
}
if(br != null){
br.close();
}
if(client != null){
client = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class Client {
public static final String IP = "localhost";//服务器地址
public static final int PORT = 8000;//服务器端口号
public static void main(String[] args) {
handler();
}
private static void handler(){
try {
//实例化一个Socket,并指定服务器地址和端口
Socket client = new Socket(IP, PORT);
//开启两个线程,一个负责读,一个负责写
new Thread(new ReadHandlerThread(client)).start();
new Thread(new WriteHandlerThread(client)).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
*处理读操作的线程
*/
class ReadHandlerThread implements Runnable{
private Socket client;
public ReadHandlerThread(Socket client) {
this.client = client;
}
@Override
public void run() {
DataInputStream dis = null;
try {
while(true){
//读取服务器端数据
dis = new DataInputStream(client.getInputStream());
String receive = dis.readUTF();
System.out.println("服务器端返回过来的是: " + receive);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(dis != null){
dis.close();
}
if(client != null){
client = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
* 处理写操作的线程
*/
class WriteHandlerThread implements Runnable{
private Socket client;
public WriteHandlerThread(Socket client) {
this.client = client;
}
@Override
public void run() {
DataOutputStream dos = null;
BufferedReader br = null;
try {
while(true){
//取得输出流
dos = new DataOutputStream(client.getOutputStream());
System.out.print("请输入: \t");
//键盘录入
br = new BufferedReader(new InputStreamReader(System.in));
String send = br.readLine();
//发送数据
dos.writeUTF(send);
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try{
if(dos != null){
dos.close();
}
if(br != null){
br.close();
}
if(client != null){
client = null;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}
来源:http://blog.csdn.net/zlqqhs/article/details/8757250
标签:java,socket,通信
0
投稿
猜你喜欢
自己动手写的mybatis分页插件(极其简单好用)
2023-11-01 18:12:09
Java毕业设计实战项目之在线服装销售商城系统的实现流程
2023-03-27 00:15:31
Java继承方法重写实现原理及解析
2021-12-31 16:35:54
C#中三种Timer计时器的详细用法
2022-12-14 13:52:34
Android实现在一个activity中添加多个listview的方法
2023-10-13 14:41:22
JAVA中的日期时间类用法总结
2023-08-29 08:00:24
Android使用AndroidUtilCode实现多语言
2023-08-28 15:25:50
详解Java信号量Semaphore的原理及使用
2023-09-18 00:13:15
java继承中的构造方法实例解析
2022-07-26 02:56:00
深入理解java动态代理的两种实现方式(JDK/Cglib)
2023-11-26 13:29:52
详解Java如何改变字符串中的字符
2022-06-13 14:29:48
Android逆向入门之常见Davlik字节码解析
2023-09-13 02:53:10
java 文件上传到读取文件内容的实例
2023-11-09 22:00:27
SpringCloud实现灰度发布的方法步骤
2023-03-17 05:18:37
Spring Security配置多个数据源并添加登录验证码的实例代码
2022-11-19 13:49:26
elasticsearch java客户端action的实现简单分析
2022-04-25 05:39:45
解析c#操作excel后关闭excel.exe的方法
2021-08-14 16:55:22
Spring RestTemplate的使用与踩坑
2022-01-12 21:43:14
Java全面解析string类型的xml字符串
2021-06-03 21:15:18
Java里的static import使用小结
2023-08-18 18:36:36