我是靠谱客的博主 激动小蘑菇,最近开发中收集的这篇文章主要介绍Java ServerSocket & Socket 实现 单组【客户端⇄服务端】双工通信(双向通信),觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
Server.java /**服务器端开启服务**/
package com.example.socket.service;
import lombok.SneakyThrows;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server{
private static List<Socket> sockets = new ArrayList<>();
private static SST sst;
private static SRT srt;
@SneakyThrows
public static void main(String[] args){
ServerSocket ss = new ServerSocket(8080);
System.out.println("**********聊天室服务器开启成功,等待用户连接************");
while (true) {
Socket socket = ss.accept();
sockets.add(socket);
System.out.println("*****t用户t" + socket.getPort() + "t连接成功t******");
sst = new SST("服务器:", socket);
sst.start();
srt = new SRT("服务器:", socket);
srt.start();
}
}
}
Client.java /**客户端开启服务**/
package com.example.socket.service;
import java.io.*;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 8080);
CST cst = new CST("客户t"+socket.getLocalPort()+"t发消息线程:",socket);
CRT crt = new CRT("客户t"+socket.getLocalPort()+"t收消息线程:",socket);
crt.start();
cst.start();
}
}
SST.java /**服务器端发送消息实现**/
package com.example.socket.service;
import lombok.SneakyThrows;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue;
public class SST extends Thread{
private String threadName;
private Thread thread;
private Socket socket;
private static List<Socket> sockets = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
private OutputStream outputStream = null;
private OutputStreamWriter outputStreamWriter = null;
private BufferedWriter bufferedWriter = null;
private Queue<String> queue = new LinkedBlockingQueue();
SST(String tn , Socket st){
this.threadName = tn;
this.socket = st;
this.sockets.add(socket);
}
@SneakyThrows
@Override
public void run(){
// 当只有一个socket时,无需判断与谁进行聊天
outputStream = socket.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream);
bufferedWriter = new BufferedWriter(outputStreamWriter);
while(true){
if(scanner.hasNext()){
queue.add(scanner.next());
}
for (String s : queue ) {
bufferedWriter.write("t" + s +"n");
bufferedWriter.flush();
}
queue.clear();
}
}
@Override
public void start(){
if(null==thread){
thread = new Thread(this,this.threadName);
thread.start();
}
}
}
SRT.java /**服务器端接收消息实现**/
package com.example.socket.service;
import lombok.SneakyThrows;
import java.io.*;
import java.net.Socket;
public class SRT extends Thread{
private String threadName;
private Thread thread;
private Socket socket;
SRT(String tn , Socket st){
this.threadName = tn;
this.socket = st;
}
@SneakyThrows
@Override
public void run(){
if(null!=socket){
InputStream inputStream = socket.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
while(true){
// 循环读
System.out.println("客户t" + socket.getPort() + "t说:" + bufferedReader.readLine());
}
}
}
@Override
public void start(){
if(null==thread){
thread = new Thread(this,this.threadName);
thread.start();
}
}
}
CST.java /**客户端发送消息实现**/
package com.example.socket.service;
import java.io.*;
import java.net.Socket;
import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue;
/**
* @description 客户端发送线程
*/
public class CST extends Thread{
private String threadName;
private Socket socket;
private Thread thread;
CST(String tn, Socket st){
this.threadName = tn ;
this.socket = st ;
}
@Override
public void run(){
try {
OutputStream outputStream = socket.getOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
Queue<String> queue = new LinkedBlockingQueue();
// 循环发消息
while (true) {
Scanner scanner = new Scanner(System.in);
if(scanner.hasNext()){
queue.add(scanner.next());
}
for (String s : queue ) {
bufferedWriter.write("t" + s +"n");
bufferedWriter.flush();
}
queue.clear();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void start(){
if(null==thread){
thread = new Thread(this,this.threadName);
thread.start();
}
}
}
CRT.java /**客户端接收消息实现**/
package com.example.socket.service;
import java.io.*;
import java.net.Socket;
public class CRT extends Thread{
private String threadName;
private Socket socket;
private Thread thread;
CRT(String tn, Socket st){
this.threadName = tn ;
this.socket = st ;
}
@Override
public void run(){
try {
InputStream inputStream = socket.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
// 循环读消息
while (true) {
System.out.println("服务器说:" + bufferedReader.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void start(){
if(null==thread){
thread = new Thread(this,this.threadName);
thread.start();
}
}
}
注意:
1) 先启动Server.java 之后再启动Client.java
2) 由于是测试demo,所有没有对资源进行关流处理,大家可自行斟酌
最后
以上就是激动小蘑菇为你收集整理的Java ServerSocket & Socket 实现 单组【客户端⇄服务端】双工通信(双向通信)的全部内容,希望文章能够帮你解决Java ServerSocket & Socket 实现 单组【客户端⇄服务端】双工通信(双向通信)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复