java - Sending a message from server to all clients -
i'm trying code instant messaging system... initially, i'm doing way, , once work i'll add gui.
once client sends message server, server supposed display other clients. how can that? i've been trying few things keeps displaying client sent message...
thanks in advance!
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, 1 per client } public void run() { while (true) { try { string receivedmessage = datain.readutf(); //print receivedmessage clients } catch (exception e) { break; } } } } public server(int port) throws exception { this.main = new serversocket(port); } // start 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 connection // keep reading using infintite loop system.out.println("handling connection client " + index + "..."); (new mailserver(index, datain, dataout)).start(); index += 1; // add 1 every time new client 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 message 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 mesage server (ending in \n) string msg = datain.readutf(); while (msg != null) { system.out.println(msg); msg = datain.readutf(); } } catch(exception e) { break; } } } } //client require host name , 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 connection system.out.println("handling connection 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(); } }
update: created list of mailserver objects , iterated through them send message clients, jp moresmau suggested... first client send receives outputs. why this? how can fix it... ? thank you, , sorry if questions seem obvious or dumb, i'm still java noob:(
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<mailserver> mailservers = collections.<mailserver>synchronizedlist(new arraylist<mailserver>()); 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, 1 per client } public void run() { while (true) { try { string receivedmessage = datain.readutf(); string outputmessage = "client " + index + " said: " + receivedmessage; //print receivedmessage clients (mailserver mailserver : mailservers) { dataout.writeutf(outputmessage); } } catch (exception e) { break; } } } } public server(int port) throws exception { this.main = new serversocket(port); } // start 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 connection // keep reading using infintite loop system.out.println("handling connection client " + index + "..."); mailserver mailserver = new mailserver(index, datain, dataout); mailserver.start(); mailservers.add(mailserver); dataout.writeutf("client " + index); index += 1; // add 1 every time new client added } } public static void main(string[] args) throws exception { server s = new server(port); system.out.println("serving...."); s.serve(); } }
have static list of mailserver objects create
static list<mailserver> servers=collections.<mailserver>synchronizedlist(new linkedlist<mailserver>); ... mailserver s=new mailserver(index, datain, dataout); servers.add(s); s.start();
then loop through them when 1 of them receives message, , expect receiver, write message output.
the big problem in code dataout , datain unique! need move them mailserver class. each mailserver talks 1 specific client , hence needs have own streams.
static class mailserver extends thread { dataoutputstream dataout; datainputstream datain;
and notification loop becomes:
for(mailserver mailserver:mailservers){ if (mailserver!=this){ mailserver.dataout.writeutf(...); } }
i don't understand how expect client.name in server, since client runs on machine... rid of now.
Comments
Post a Comment