java服务器如何群发消息,java TCP编程简单实现一个消息群发功能

client:

package com.lzs.net;

import java.io.*;

import java.net.Socket;

/**

* Created by zaish on 2016-3-20.

*/

public class MyTCPClient {

public static void main(String[] args) throws IOException {

//1 创建服务器指定服务器和端口

Socket socket=new Socket("localhost",8888);

new Thread(new Send(socket)).start();

new Thread(new Recieve(socket)).start();

}

}

class Send implements Runnable {

private BufferedReader br;

private DataOutputStream dos;

private boolean isRunning=true;

public Send(){

br=new BufferedReader(new InputStreamReader(System.in));

}

public Send(Socket socket) {

this();

try {

dos=new DataOutputStream(socket.getOutputStream());

} catch (IOException e) {

isRunning=false;

CloseUtil.close(dos,br);

}

}

public void send(){

String msg=getMsgFromConsole();

if(null!=msg&&!msg.equals("")){

try {

dos.writeUTF(msg);

} catch (IOException e) {

isRunning=false;

CloseUtil.close(dos,br);

}

}

}

private String getMsgFromConsole(){

try {

return br.readLine();

} catch (IOException e) {

return "";

}

}

@Override

public void run() {

while (isRunning){

send();

}

}

}

class Recieve implements Runnable {

private BufferedWriter bw;

private DataInputStream dis;

private boolean isRunning=true;

public Recieve(){

bw=new BufferedWriter(new OutputStreamWriter(System.out));

}

public Recieve(Socket socket){

this();

try {

dis=new DataInputStream(socket.getInputStream());

} catch (IOException e) {

isRunning=false;

CloseUtil.close(dis,bw);

}

}

public void recieve(){

String msg=null;

try {

msg=dis.readUTF();

} catch (IOException e) {

isRunning=false;

CloseUtil.close(dis,bw);

}

try {

bw.write(msg);

bw.newLine();

bw.flush();

} catch (IOException e) {

e.printStackTrace();

}

}

@Override

public void run() {

while (isRunning){

recieve();

}

}

}

server:

package com.lzs.net;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.util.ArrayList;

import java.util.List;

/**

* Created by zaish on 2016-3-20.

*/

public class MyTCPServer {

private List list=new ArrayList();

public static void main(String[] args) throws IOException {

new MyTCPServer().start();

}

private void start()throws IOException {

//1 创建服务器指定端口

ServerSocket ss=new ServerSocket(8888);

while(true){

//2 接受客户端连接,阻塞式

Socket socket=ss.accept();

System.out.println(socket.getChannel()+"建立连接");

MyChannel myChannel=new MyChannel(socket);

new Thread(myChannel).start();

list.add(myChannel);

}

}

private class MyChannel implements Runnable{

private DataInputStream dis;

private DataOutputStream dos;

private boolean isRunning=true;

public MyChannel(Socket socket){

try {

dis=new DataInputStream(socket.getInputStream());

dos=new DataOutputStream(socket.getOutputStream());

} catch (IOException e) {

isRunning=false;

list.remove(this);

CloseUtil.close(dis,dos);

}

}

private String receive(){

String msg="";

try {

msg=dis.readUTF();

} catch (IOException e) {

isRunning=false;

list.remove(this);

CloseUtil.close(dis,dos);

}

return msg;

}

private void send(String msg){

if("".equals(msg)){

return;

}

try {

dos.writeUTF(msg);

dos.flush();

} catch (IOException e) {

isRunning=false;

list.remove(this);

CloseUtil.close(dis,dos);

}

}

private void sendOthers(){

String msg=receive();

for(MyChannel m:list){

if(m==this){

continue;

}else {

m.send(msg);

}

}

}

@Override

public void run() {

while (isRunning){

sendOthers();

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值