mmingfeilam
Programmer
hi all,
i am writing a simple client to test out a concept:
try
{
/*Reader from_server;
PrintWriter to_server;
BufferedReader from_user;
PrintWriter to_user;
WorkerThread t;*/
// Connect to the specified host and port
Socket s = new Socket(host, port);
s.setKeepAlive(true);
synchronized(s)
{
//while(true)
for (int i=0; i<500; i++)
{
//Socket s = new Socket(host, port);
// Set up streams for reading from and writing to the
server.
// The from_server stream is final for use in the
inner class below
Reader from_server=new
InputStreamReader(s.getInputStream());
if(from_server == null)
{
throw new Exception("from_server null");
}
PrintWriter to_server = new
PrintWriter(s.getOutputStream());
if(to_server == null)
{
throw new Exception("to_server null");
}
// Set up streams for reading from and writing to the
console
// The to_user stream is final for use in the
anonymous class below
BufferedReader from_user =
new BufferedReader(new
InputStreamReader(System.in));
if(from_user == null)
{
throw new Exception("from_user null");
}
// Pass true for auto-flush on println()
PrintWriter to_user = new PrintWriter(System.out,
true);
if(to_user == null)
{
throw new Exception("to_user null");
}
// Tell the user that we've connected
/*to_user.println("Connected to " +
s.getInetAddress() +
":" + s.getPort());*/
// Create a thread that gets output from the server
and displays
// it to the user. We use a separate thread for this
so that we
// can receive asynchronous output
WorkerThread t = new WorkerThread(from_server,
to_user, s);
// We set the priority of the server-to-user thread
above to be
// one level higher than the main thread. We
shouldn't have to do
// this, but on some operating systems, output sent
to the console
// doesn't appear when a thread at the same priority
level is
// blocked waiting for input from the console.
t.setPriority(Thread.currentThread().getPriority() +
1);
// Now start the server-to-user thread
t.start();
// In parallel, read the user's input and pass it on
to the server.
/*String line;
while((line = from_user.readLine()) != null) {
to_server.print(line + "\n");
to_server.flush();
}*/
// If the user types a Ctrl-D (Unix) or Ctrl-Z
(Windows) to end
// their input, we'll get an EOF, and the loop above
will exit.
// When this happens, we stop the server-to-user
thread and close
// the socket.
//s.close();
}
}
//to_user.println("Connection closed by client.");
//System.exit(0);
}
// If anything goes wrong, print an error message
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java GenericClient <hostname>
<port>");
}
I am trying to reuse the same socket (including address and port) for
all the threads, but so far only one of the threads gets to print out
an output from the server. Is this reuseage even possible, if so,
can someone show me how?
Thanks.
i am writing a simple client to test out a concept:
try
{
/*Reader from_server;
PrintWriter to_server;
BufferedReader from_user;
PrintWriter to_user;
WorkerThread t;*/
// Connect to the specified host and port
Socket s = new Socket(host, port);
s.setKeepAlive(true);
synchronized(s)
{
//while(true)
for (int i=0; i<500; i++)
{
//Socket s = new Socket(host, port);
// Set up streams for reading from and writing to the
server.
// The from_server stream is final for use in the
inner class below
Reader from_server=new
InputStreamReader(s.getInputStream());
if(from_server == null)
{
throw new Exception("from_server null");
}
PrintWriter to_server = new
PrintWriter(s.getOutputStream());
if(to_server == null)
{
throw new Exception("to_server null");
}
// Set up streams for reading from and writing to the
console
// The to_user stream is final for use in the
anonymous class below
BufferedReader from_user =
new BufferedReader(new
InputStreamReader(System.in));
if(from_user == null)
{
throw new Exception("from_user null");
}
// Pass true for auto-flush on println()
PrintWriter to_user = new PrintWriter(System.out,
true);
if(to_user == null)
{
throw new Exception("to_user null");
}
// Tell the user that we've connected
/*to_user.println("Connected to " +
s.getInetAddress() +
":" + s.getPort());*/
// Create a thread that gets output from the server
and displays
// it to the user. We use a separate thread for this
so that we
// can receive asynchronous output
WorkerThread t = new WorkerThread(from_server,
to_user, s);
// We set the priority of the server-to-user thread
above to be
// one level higher than the main thread. We
shouldn't have to do
// this, but on some operating systems, output sent
to the console
// doesn't appear when a thread at the same priority
level is
// blocked waiting for input from the console.
t.setPriority(Thread.currentThread().getPriority() +
1);
// Now start the server-to-user thread
t.start();
// In parallel, read the user's input and pass it on
to the server.
/*String line;
while((line = from_user.readLine()) != null) {
to_server.print(line + "\n");
to_server.flush();
}*/
// If the user types a Ctrl-D (Unix) or Ctrl-Z
(Windows) to end
// their input, we'll get an EOF, and the loop above
will exit.
// When this happens, we stop the server-to-user
thread and close
// the socket.
//s.close();
}
}
//to_user.println("Connection closed by client.");
//System.exit(0);
}
// If anything goes wrong, print an error message
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java GenericClient <hostname>
<port>");
}
I am trying to reuse the same socket (including address and port) for
all the threads, but so far only one of the threads gets to print out
an output from the server. Is this reuseage even possible, if so,
can someone show me how?
Thanks.