java服务器向客户端发送消息_从服务器向所有客户端发送消息

我正在尝试编写即时消息系统...最初,我这样做,一旦我开始工作,我将添加GUI .

一旦客户端向服务器发送消息,服务器就应该将其显示给所有其他客户端 . 我怎样才能做到这一点?我一直在尝试一些东西,但它一直只显示给发送消息的客户端...

提前致谢!

SERVER

import java.io.*;

import java.net.*;

class Server {

//one per server

static int port = 3000;

private int backlog = 100;

ServerSocket main;

static DataOutputStream dataOut;

static DataInputStream dataIn;

static String scannerMessage;

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

static class MailServer extends Thread {

//one per client

static int index;

String name = Client.name;

public MailServer(int index, DataInputStream in, DataOutputStream out) {

Server.dataIn = in;

Server.dataOut = out;

this.index = index; // thread index, one per client

}

public void run() {

while (true) {

try {

String receivedMessage = dataIn.readUTF();

//print receivedMessage to all clients

} catch (Exception e) {

break;

}

}

}

}

public Server(int port) throws Exception {

this.main = new ServerSocket(port);

}

// start a serve

public void serve() throws Exception {

int index = 1;

while (true) {

Socket socket = this.main.accept();

InputStream in = socket.getInputStream();

OutputStream out = socket.getOutputStream();

DataInputStream dataIn = new DataInputStream(in);

DataOutputStream dataOut = new DataOutputStream(out);

// handle the connection

// keep reading using an infintite loop

System.out.println("Handling connection to Client " + index + "...");

(new MailServer(index, dataIn, dataOut)).start();

index += 1; // add one every time a new client is added

}

}

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

Server s = new Server(port);

System.out.println("Serving....");

s.serve();

}

}

CLIENT

import java.io.*;

import java.net.*;

class Client {

static String hostname = "127.0.0.1";

static int port = Server.port;

static Socket socket;

static String name;

static class Sender extends Thread {

DataOutputStream dataOut;

public Sender(DataOutputStream dataOut) {

this.dataOut = dataOut;

}

public void run() {

while(true) {

//get a message from the user

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

try {

String message = br.readLine();

dataOut.writeUTF(message);

dataOut.flush();

} catch(Exception e) {

break;

}

}

}

}

static class Receiver extends Thread {

DataInputStream dataIn;

public Receiver(DataInputStream dataIn) {

this.dataIn = dataIn;

}

public void run() {

while(true) {

try {

//RECEIVE A MESAGE FROM THE SERVER (ending in \n)

String msg = dataIn.readUTF();

while (msg != null) {

System.out.println(msg);

msg = dataIn.readUTF();

}

} catch(Exception e) {

break;

}

}

}

}

//client will require host name and the port

public Client(String hostname, int port) throws Exception {

socket = new Socket(hostname, port);

}

public void connect() throws Exception {

InputStream in = socket.getInputStream();

OutputStream out = socket.getOutputStream();

DataInputStream dataIn = new DataInputStream(in);

DataOutputStream dataOut = new DataOutputStream(out);

//handle the connection

System.out.println("Handling connection to server...");

Thread sender = new Sender(dataOut);

Thread receiver = new Receiver(dataIn);

sender.start();

receiver.start();

sender.join();

receiver.join();

System.out.println("Client " + Server.MailServer.index);

System.out.println("----------------------");

}

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

Client c = new Client(hostname, port);

c.connect();

}

}

更新:我创建了所有MailServer对象的列表,然后通过它们迭代将消息发送给所有客户端,正如JP Moresmau建议的那样......但现在第一个发送内容的客户端接收所有输出 . 为什么是这样?我该怎么解决它?谢谢,对不起,如果我的问题看起来太明显或愚蠢,我还是一个Java菜鸟:(

SERVER - UPDATED

package csci2020_assignment51;

import java.io.*;

import java.net.*;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

class Server {

//one per server

static int port = 3000;

private int backlog = 100;

ServerSocket main;

static DataOutputStream dataOut;

static DataInputStream dataIn;

static String scannerMessage;

static List mailServers = Collections.synchronizedList(new ArrayList());

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

static class MailServer extends Thread {

//one per client

static int index;

String name = Client.name;

public MailServer(int index, DataInputStream in, DataOutputStream out) {

Server.dataIn = in;

Server.dataOut = out;

this.index = index; // thread index, one per client

}

public void run() {

while (true) {

try {

String receivedMessage = dataIn.readUTF();

String outputMessage = "Client " + index + " said: " + receivedMessage;

//print receivedMessage to all clients

for (MailServer mailserver : mailServers) {

dataOut.writeUTF(outputMessage);

}

} catch (Exception e) {

break;

}

}

}

}

public Server(int port) throws Exception {

this.main = new ServerSocket(port);

}

// start a serve

public void serve() throws Exception {

int index = 1;

while (true) {

Socket socket = this.main.accept();

InputStream in = socket.getInputStream();

OutputStream out = socket.getOutputStream();

DataInputStream dataIn = new DataInputStream(in);

DataOutputStream dataOut = new DataOutputStream(out);

// handle the connection

// keep reading using an infintite loop

System.out.println("Handling connection to Client " + index + "...");

MailServer mailServer = new MailServer(index, dataIn, dataOut);

mailServer.start();

mailServers.add(mailServer);

dataOut.writeUTF("Client " + index);

index += 1; // add one every time a new client is added

}

}

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

Server s = new Server(port);

System.out.println("Serving....");

s.serve();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值