what could be best way to implement observer pattern when I use 2 programming languagues C and Java? It’s not necessarily use observer patter I just need to know how to implement method or function like notifyall() to notify both clients which are connected to session after action of one client. Its enough to just send them string message.
I have server application in C and clients in Java swing.
There are n games on server, to each game can be connected two clients.
server and client use sockets with Inet Address and server and client have 2 metods – send, recieve.
I have realized:
server – C :
– send – send message to client (client is specified by socket file descriptor)
– recieve – recieve message from client (client is specified by socket file descriptor)
client – in Java
– send – send message to server (server is specified by socket file descriptor)
– recieve – recieve message from server (server is specified by socket file descripor)
Server runs on localhost 127.0.0.1 and port for example 10000
Clients too. I connect clients to server adress 127.0.0.1, port 10000.
Message is simple string like “Hello world\n”
1. run server, server wait for messages
2. run client – client send message to server
3. server – recieve message and send message back to client
server proccess the each client in new process using fork
server and client write sent and recieved messages
4.client end after sent message
this is what work.
I have class in java swing which represents client GUI, there are n buttons(JButton), for example 16, each of them have image icon, each button have added ActionListener
if I click on the button something should happen – change or not to change the ImageIcon.
What I’m trying to implement is method something like observer
I need somehow have something like sessions on server:(which represents games)
game1: client1, client2
game2: client3, client4
game3: client5, client6
etc..
1.I press button number 15 in client1, client send to server message like “client1 pressed button no. 15”
2. Server recieve message and send message like “client1 pressed button 15” to all clients conencted in game1 (client1, client2). In server message will be also if is necessarilly something like “change ImageIcon of button 15”.
point number 2 should be function notifyAll
What is good and simple way to implement it?
Thanks for advices.
You simply need a “list” of clients to “notify”
The “list” could be as simple as an array of hostnames or IP addresses.
Your “event” handler would just iterate through the list, sending a message to each host in turn.