[5946] in java-interest

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

Help: Java Socket (Server-client interaction)

daemon@ATHENA.MIT.EDU (Eric Pan)
Tue Mar 5 20:23:44 1996

Date: Fri, 1 Mar 1996 13:24:44 -0700 (MST)
From: Eric Pan <c-pye@math.utah.edu>
To: java-interest@java.Eng.Sun.COM


I have created a server and a client program as follows:

/*********Beginning of testServer.java*********/
class testServer {

  
  public static void main ( String arg[] ) {

    ServerSocket s = (ServerSocket) null;

    Socket lsock;
    OutputStream out;
    InputStream in;
    int port = 1112;
    
    try s = new ServerSocket(port, 300);
    catch (IOException e) {
      System.out.println("\nServer time out!");
      System.exit(-1);
    }
 
    while (true) {
      try {
 
        lsock = s.accept();
 
	in  = lsock.getInputStream();

	System.out.println((int)in.read());
        out = lsock.getOutputStream();

	out.write(2);

	lsock.close();
 
     } catch (IOException e) {}
    }
  }
}
/**********End of testServer*********/

/********Beginning of testClient.java******/
import java.net.*;
import java.io.*;

class testClient {

  public static void main (String arg[]) {

    Socket lsock;;
    int lport = 1112;

    try {
      lsock = new Socket (arg[0],lport);

      send(lsock,1);

      while (lsock.getInputStream() != null)
      receive(lsock);

      lsock.close();

    } catch (IOException e) {}

  }

  public static void send (Socket s, int out) throws IOException {
    OutputStream sout;
    sout = s.getOutputStream();
    sout.write(out);
  
  }

  public static void receive (Socket s) throws IOException {
    InputStream in;
    in = s.getInputStream();
    System.out.println((int)in.read());
  }
}
/********** End of testClient*****/

Questions: I like the two programs to interact with one another. For
	instance, I want both the server and client to know if there
	is an incoming signal. If there is an incoming message for
	the server, I want it to print on the screen. I want the server
	and the client to send messages randomly. Hence, if the client
	is sending a message at a particular time, the server need to
	know there is a message and get the message from the socket. 

I hope I am clear enough in my question and please help me on that. Thanks.

-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

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