[864] in java-interest

home help back first fref pref prev next nref lref last post

Re: SIMPLE NETWORK EXAMPLE

daemon@ATHENA.MIT.EDU (Jay Gallivan)
Sat Aug 12 00:51:01 1995

Reply-To: jag@mvision.com
Date: Fri, 11 Aug 95 17:48:26 CDT
From: jag@mvision.com (Jay Gallivan)
To: daemon@java.sun.com
Cc: java-interest@java.sun.com
In-Reply-To: <Pine.SUN.3.91.950811110700.10842C-100000@ATM-ws40> (kurr@ctron.com)


>
>I'm looking for a simple client-server network example using the
>net.NetworkServer net.NetworkClient object.  Please e-mail me any examples 
>you might have or point me to a web page.
>
>Thanks,
>
>Ron

I sent a similar request to java-interest several days ago. I found
some stuff by Francois Boussard (francois@coplanet.fr) and worked it
to fit my (limited) understanding. Right below you will find my stuff.
A little further down you will find stuff that Francois mailed me
earlier this week (which I've not had time to work with). I hope this
helps.

##
## Jay Gallivan's stuff
##

//
// MyServer.java
//

import net.*;

class MyServer
extends NetworkServer
implements Runnable
{
  //
  // Override super.serviceRequest()
  //
  public void serviceRequest() {
    byte buf[] = new byte[300];
    int n;
    int c;
    System.out.println("In serviceRequest()");
    while ((n = clientInput.read(buf, 0, buf.length)) >= 0) {
      clientOutput.println("Hello from MyServer!");
      clientOutput.flush();
      System.out.write(buf, 0, n);
      // need to do switch to handle protocol.
    }
    System.out.println("Ex serviceRequest()");	
  }
  
  //
  // This is the class constructor
  //
  public MyServer(){
    super();
  }

  public static void main (String args[]) {
    System.out.println("In main()!");
    MyServer ms;

    ms=new MyServer();
    ms.startServer(4040);

  }
}

//
// MyClient.java
//
import net.*;

class MyClient {
  public static void main (String args[]) {

    byte buf[] = new byte[300];
    int n;
    int c;

    NetworkClient mc;

    mc=new NetworkClient("mvloop7.chi.mvision.com",4040);

    mc.serverOutput.println("Hello from MyClient!");
    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.write(buf, 0, n);
      mc.serverOutput.println("BYE");
    }

//    while(System.in.read()!=32) ;
    mc.closeServer();
  
  }
}

##
## Francois Boussard's stuff
##

In fine, i ask you if you know HOW getting the DNS name/IP adress without
asking it to the final user.

Cause if a client/socket wants to connect outside the user site, you need
to know the IP adress of the DNS for this site.

So, how can we get this adress internally inside the aplet without asking
it to user ?

import net.*;

//
// MyServeur.java
//

//************************************************************************
//***************************  CLASSE
//---------------------------  Doc de la classe
//
//
class MyServeur
extends NetworkServer
implements Runnable
{
  //-------------------------- Static
  //-------------------------- Vars PRIVEES
  //-------------------------- Vars REDEFINIES
  //-------------------------- Vars NOUVELLES
  //-------------------------- Methodes PRIVEES
  //-------------------------  Methodes REDEFINIES

  public void serviceRequest() {
    byte buf[] = new byte[300];
    int n;
    int c;
    System.out.println("Another try !\n");
    while ((n = clientInput.read(buf, 0, buf.length)) >= 0) {
      System.out.write(buf, 0, n);
    }

  }

  //-------------------------  Methodes IMPLEMENTEES
  //-------------------------  Methodes NOUVELLES
  //-------------------------  Constructeurs

  public MyServeur(){
    super();
  }

}
//***************************  FIN CLASSE
//************************************************************************

class serveur {

  public static void main (String args[]) {
    MyServeur ms;

System.out.println("-1\n");
    ms=new MyServeur();
System.out.println("0\n");
    ms.startServer(4040);
    
System.out.println("1\n");
//    ms.run();
System.out.println("2\n");

  }
}


//
// client.java
//

import net.*;



class client {

  public static void main (String args[]) {
    NetworkClient mc;

System.out.println("-1\n");
    mc=new NetworkClient("peche",4040);
System.out.println("0\n");
//    mc.openServer("prune",11000);
    mc.serverOutput.println("coucou ?");
    mc.serverOutput.flush();
System.out.println(mc.serverIsOpen()+"\n");
    while(System.in.read()!=32) ;
    mc.closeServer();

  }
}
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post