Design suggestions, is this how I would allow a client to listen on a multi

Hello everyone. Currently my program will create a new thread everytime a person connects to my server. Once they connect they can send me events, and once they send me the events I parse the events and store them in a Map.

Now if someone wants to recieve all those parsed events, but don't want to me anything thats where my question is at.

I'll try to draw what I mean...

client1-send event-->myserver(parsing events and storing in a database)]

client2 only wants to recieve the already parsed events that client1 sent to the server.

client 1-send event->[my server parses it] <client2 wants to get those stored parsed events.

Right now this is the code that will spawn a new thread to allow how ever many clients to connect to my server and send me events.

package server;

//This is the parser Client that will parse messages and send to the

//z/Os output Server

import java.io.*;

import java.net.*;

publicclass MainTest{

publicstaticvoid main(String args[])throws Exception{

//get console input

BufferedReader stdin =new BufferedReader(new InputStreamReader(

System.in));

System.out.println("Enter port to run server on: ");

String input = stdin.readLine();

int servPort = Integer.parseInt(input);

//setting up sockets

ServerSocket ssock =null;

Socket sock =null;

try{

//setting up server to run on servPort

ssock =new ServerSocket(servPort);

System.out.println("Listening on : " + ssock);

System.out.println("Waiting for client connection...");

while (true){

//waiting for client to connect to server socket

sock = ssock.accept();

System.out.println(sock +" connected.");

//get host information

InetAddress clientIa = sock.getInetAddress();

String clientName = clientIa.getHostName();

String clientIp = clientIa.getHostAddress();

int localPort = sock.getLocalPort();

System.out.println("hostname: " + clientName +'\n'

+"Ip address: " + clientIp +":" + localPort +"\n\n");

System.out.println("Waiting for data...");

new Thread(new MultiThreadServer(sock)).start();

}

}catch (SocketException e){

System.out.println("Socket error: " + e);

}

catch (UnknownHostException e){

System.out.println("Unknown host: " + e);

}catch (IOException e){

System.out.println("IOException: " + e);

}

}

now for me to open another server socket each time for whoever wants to receive these events would I just add the following?

...

...

Socket sockSend =null;

ServerSocket ssockSend =null;

try{

//setting up server to run on servPort

ssock =new ServerSocket(servPort);

ssockSend =new ServerSocket(1234);

System.out.println("Listening on : " + ssock);

System.out.println("Waiting for client connection...");

while (true){

//waiting for client to connect to server socket

sock = ssock.accept();

sockSend = ssockSend.accept();

System.out.println(sock +" connected.");

//get host information

InetAddress clientIa = sock.getInetAddress();

String clientName = clientIa.getHostName();

String clientIp = clientIa.getHostAddress();

int localPort = sock.getLocalPort();

System.out.println("hostname: " + clientName +'\n'

+"Ip address: " + clientIp +":" + localPort +"\n\n");

System.out.println("Waiting for data...");

new Thread(new MultiThreadServer(sock,sockSend)).start();

}

...

The problem I see here is that, if no client wants to listen for the events, but clients want to send events, the program will stop here:

while (true){

//waiting for client to connect to server socket

sock = ssock.accept();

sockSend = ssockSend.accept();

because sockSend = ssockSend.accept();will just sit there waiting for a client to listen to receive the messages

But I would like the server to Accepting connections for clients who want to send me events, and make it optional if another client wants to connect to me to just recieve the already parsed events.

So basically I'll have 2 ServerPorts listening,

say Port: 5656, these are for clients who want to send me events.

Port 2222: this port is for clients who want to recieve the events.

Sorry if that was confusing, let me know if I can clarify anything.

:D

[7082 byte] By [lokiea] at [2007-11-27 10:17:02]
# 1

You need two accepting threads, or more generally an accepting thread per ServerSocket.

ejpa at 2007-7-28 15:49:07 > top of Java-index,Core,Core APIs...
# 2

Thanks actually there was a change of plan, For every client connection its going to automatically be routed to another server, rather than a client trying to connect to my server so I think it will be easier.

lokiea at 2007-7-28 15:49:07 > top of Java-index,Core,Core APIs...