[1846] in java-interest
Getting one server to talk to 2 clients
daemon@ATHENA.MIT.EDU (Robert Uomini)
Mon Sep 18 19:41:35 1995
Date: Mon, 18 Sep 1995 12:27:15 -0700
From: uomini@fractals.fractals.com (Robert Uomini)
To: java-interest@java.Eng.Sun.COM
Cc: uomini@fractals.fractals.com
I've got a Java server application and two clients. Client1 sends some ASCII
text to the server. who makes it available to Client2 when Client2 is first
started and as the data from Client1 becomes available. Here's what the code
looks like:
-------------------------------Server----------------------------------------
import sun.net.*;
class StatsServer
extends NetworkServer
implements Runnable
{
//
// Override super.serviceRequest()
//
static StatsServer ms1;
static StatsServer2 ms2;
byte buf[] = new byte[300];
public synchronized void serviceRequest()
{
int n=0;
int c;
System.out.println ("In serviceRequest");
while ((n = clientInput.read(buf, 0, buf.length)) >= 0)
{
wait(1000);
System.out.println ("Msg rcvd from Jump:");
System.out.write(buf, 0, n);
ms2.clientOutput.write(buf, 0, n);
ms2.clientOutput.flush();
}
System.out.println ("Exit serviceRequest");
}
public StatsServer()
{
super();
}
public static void main (String args[])
{
ms1 = new StatsServer();
ms1.startServer(7001);
ms2 = new StatsServer2(ms1);
ms2.startServer(7000);
}
}
class StatsServer2
extends NetworkServer
implements Runnable
{
//
// Override super.serviceRequest()
//
static StatsServer ms1;
byte buf[] = new byte[300];
public synchronized void serviceRequest()
{
int n=0;
int c;
System.out.println ("In serviceRequest2");
while ((n = clientInput.read(buf, 0, buf.length)) >= 0)
{
wait(1000);
System.out.println ("Msg rcvd from StatsClient:");
System.out.write(buf, 0, n);
System.out.println ("Contents of ms1.buf:");
System.out.write(ms1.buf, 0, ms1.buf.length);
clientOutput.write(ms1.buf, 0, ms1.buf.length);
clientOutput.flush();
}
System.out.println ("Exit serviceRequest2");
}
public StatsServer2(StatsServer server)
{
super();
ms1 = server;
}
}
-------------------------------Client1----------------------------------------
import sun.net.*;
class Jump {
public static void main (String args[]) {
NetworkClient mc;
mc = new NetworkClient("www.fractals.com", 7001);
mc.serverOutput.println(args[0]);
mc.serverOutput.flush();
mc.closeServer();
}
}
-------------------------------Client2----------------------------------------
import sun.net.*;
class StatsClient {
public static void main (String args[]) {
byte buf[] = new byte[300];
int n;
int c;
NetworkClient mc;
mc = new NetworkClient("www.fractals.com", 7000);
mc.serverOutput.println("Hello from StatsClient!");
mc.serverOutput.flush();
System.out.println(mc.serverIsOpen()+"\n"); // <-- this says me "true" !!
while ((n = mc.serverInput.read(buf, 0, buf.length)) >= 0) {
System.out.println ("In StatsClient while loop");
System.out.println ("Contents of buf:");
System.out.write(buf, 0, n);
}
System.out.println ("Exit StatsClient");
mc.closeServer();
}
}
Everything works fine except the call
ms2.clientOutput.write(buf, 0, n);
doesn't wake up Client2, i.e., the call to mc.serverInput.read() should
execute, but it just sits there. Is this a bug in the Java interpreter?
Bob
---
Robert Uomini
The Fractal Images Company (http://www.fractals.com)
Voice: 510-528-0258/800-548-0258
Fax: 510-528-0243
*******************************************************************************
* Buying or selling a home? Come visit the most comprehensive set of *
* residential real estate listings and related services on the Internet: *
* The FractalNet Real Estate Server, http://www.fractals.com/realestate.html *
*******************************************************************************
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com