Re: RMI question
Niclas Hedhman <[email protected]> Wed, 29 Oct 2008 10:45:53 +0800
| Newsgroups | gmane.comp.java.sun.rmi |
|---|---|
| Message-ID | <[email protected]> |
Can't see any flush() of the data... On Tue, Oct 28, 2008 at 2:59 PM, FirFlies <[email protected]> wrote: > hello > i have the code: > > import java.net.*; > import java.io.*; > > public class Server{ > public static void main(String []args){ > DatagramSocket socket=null; > int serverPort=7999; > > try{ > //creating a new DatagramSocket > socket=new DatagramSocket(serverPort); > } > catch(Exception e){ > System.out.println("DatagramSocket-error-"+e.getMessage()); > System.exit(0); > } > > //server activation > > boolean listening=true; > DatagramPacket packet=null; > App app=new App(); > Protocol p=null; > > while(listening){ > try{ > > byte[] bin=new byte[4048]; > packet=new DatagramPacket(bin,bin.length); > socket.receive(packet); > > ByteArrayInputStream bais=new ByteArrayInputStream(bin); > ObjectInputStream in=new ObjectInputStream(bais); > p=(Protocol)in.readObject(); > > //resolve the client's query > p.x=app.compute(p.x,p.y); > p.y=0; > > //transform message into a stream > ByteArrayOutputStream baos=new ByteArrayOutputStream(256); > ObjectOutputStream out=new ObjectOutputStream(baos); > out.writeObject(p); > byte[] bout=baos.toByteArray(); > //send the message > InetAddress address=packet.getAddress(); > int port=packet.getPort(); > packet=new DatagramPacket(bout,bout.length,address,port); > socket.send(packet); > } > catch(Exception e){ > System.out.println(e.getMessage()); > } > } > socket.close(); > } > } > > and the client: > > import java.net.*; > import java.io.*; > import java.util.Scanner; > > public class ClientCmmdc{ > public static void main(String []args){ > String hostServer="localhost"; > int portServer=7999; > if(args.length>0) > hostServer=args[0]; > if(args.length>1) > portServer=Integer.parseInt(args[1]); > try{ > DatagramSocket socket=new DatagramSocket(); > > Protocol p=new Protocol(0,0); > Scanner scanner=new Scanner(System.in); > System.out.println("First number: "); > p.x=scanner.nextLong(); > System.out.println("Last number: "); > p.y=scanner.nextLong(); > > //transform the message into a stream > ByteArrayOutputStream baos=new ByteArrayOutputStream(256); > ObjectOutputStream out=new ObjectOutputStream(baos); > out.writeObject(p); > byte[] bout=baos.toByteArray(); > > //send the message > InetAddress address=InetAddress.getByName(hostServer); > DatagramPacket packet=new > DatagramPacket(bout,bout.length,address,portServer); > socket.send(packet); > > //waiting for response > byte[] bin=new byte[4048]; > packet=new DatagramPacket(bin,bin.length); > socket.receive(packet); > > //getting the response > ByteArrayInputStream bais=new ByteArrayInputStream(bin); > ObjectInputStream in=new ObjectInputStream(bais); > p=(Protocol)in.readObject(); > System.out.println("Result is: "+p.x); > > socket.close(); > } > catch(Exception e){ > System.out.println(e.getMessage()); > } > } > } > > Protocol class is: > > import java.io.*; > > public class Protocol implements Serializable{ > long x,y; > Protocol(long x,long y){ > this.x=x; > this.y=y; > } > } > > and finally the App class is the class with one method, to calculate the > divisor of a two numbers: > > public class App{ > public long compute(long m,long n){ > long r,c; > do{ > c=n; > r=m%n; > m=n; > n=r; > } > while(r!=0); > return c; > } > } > > i am not able to run this class with Datagram Channel. if anyone does really > help me, i would appreciate. with DatagramPacket, it's all ok, but using > Datagram Channel i really did not know how to solve the problem. with > DatagramPacket it works,so it's about the still problem, just using Datagram > Channel. > Thanks > -- > View this message in context: http://www.nabble.com/RMI-question-tp20202198p20202198.html > Sent from the Java - RMI mailing list archive at Nabble.com. > > =========================================================================== > To unsubscribe, send email to [email protected] and include in the body > of the message "signoff RMI-USERS". For general help, send email to > [email protected] and include in the body of the message "help". > > For a list of frequently asked RMI questions please refer to: > http://java.sun.com/j2se/1.3/docs/guide/rmi/faq.html > > To view past RMI-USERS postings, please see: > http://archives.java.sun.com/archives/rmi-users.html > =========================================================================== To unsubscribe, send email to [email protected] and include in the body of the message "signoff RMI-USERS". For general help, send email to [email protected] and include in the body of the message "help". For a list of frequently asked RMI questions please refer to: http://java.sun.com/j2se/1.3/docs/guide/rmi/faq.html To view past RMI-USERS postings, please see: http://archives.java.sun.com/archives/rmi-users.html