Log In
New Account
  
 
Home My Page Project Tree Code Snippets Project Openings NUTSS
 
 
Summary Tracker Lists CVS Files
 

CVS | Administration


File:  [GForge:cvsroot/nutss] / libnutss / examples / java / Test.java
Revision 1.7: download - view: text, annotated - select for diffs
Fri Jul 13 18:09:46 2007 UTC (10 years, 2 months ago) by ari
Branches: MAIN
CVS tags: HEAD
Added anohter line of explanation.

import net.nutss.*;

import java.io.*;

/*
Can be launched in two modes, as either client or server.
Client will connect to server, and then the two exchange messages back and forth, in C string format.
Java version of NUTSS_app.c test.
*/
public class Test {

	public static void main(String[] args) {
		System.out.println("starting test");

		if(args.length > 0)
			testClient();
		else
			testServer();
	}

	protected static void testServer() {
		NUTSS.init("ari1", "nutss.net", "sip.nutss.net", 5060, "ari1", "password");
	//initialize NUTSS; use ari1@nutss.net as the local app name, with login credentials ari1/password, and server sip.nuts.net:5060

		try{
			NUTSSSocketAddress endpt = new NUTSSSocketAddress("ari1", "nutss.net", "nutsstestapp");  
	//endpt represents the NUTSS address ari1@nutss.net, with app name nutsstestapp
			NUTSSServerSocket ns = new NUTSSServerSocket(); //create a server socket
			ns.bind(endpt);  //bind it to the endpoint
			System.err.flush();  
			System.out.println("bound to port "+ ns.getLocalPort()+ "!");
			NUTSSSocket soc = ns.accept();  //wait for connection
			System.out.println("accept succeeded!"); 
			System.out.println("other party is: " + soc.getRemoteSocketAddress()); 
				//print out address of connected host

			//soc is an instance of NUTSSSocket, and therefore of Socket.  So read it as normal.
			byte[] buf = new byte[255];
			InputStream is = soc.getInputStream();  
			OutputStream os = soc.getOutputStream();
			String s;
			for(int i=0; i< 1000; ++i)
			{
				s = "sending message #"+i+ " from java";
				os.write(s.getBytes());
				os.write(0);
				is.read(buf);
				System.out.println("got message: "+  parseCStr(buf));


			}

			soc.close();  //close socket
		}
		catch(java.io.IOException e)
		{
			e.printStackTrace();
		}

	}
		//convert C string to java String
	private static String parseCStr(byte[] buf) {
		int len ;
		for(len = 0; len < buf.length; ++len)
			if(buf[len] == 0)
			{
				return new String(buf, 0, len);
			}

		return new String(buf);
	}

	protected static void testClient() {
		NUTSS.init("ari2", "nutss.net", "sip.nutss.net", 5060, "ari2", "password");
		try{
			NUTSSSocketAddress endpt = new NUTSSSocketAddress("ari1", "nutss.net", "nutsstestapp");
				//create an endpoint, specifically ari@nutss.net with app name nutsstestapp
			NUTSSSocket soc = new NUTSSSocket(); //create a socket,
			soc.connect(endpt);		// and connect to endpoint
			System.out.println("connect returned!");

			System.out.println("other party is: " + soc.getRemoteSocketAddress());

				//it's just a socket, so we can connect as normal
			byte[] buf = new byte[255];
			InputStream is = soc.getInputStream();
			OutputStream os = soc.getOutputStream();
			String s;
			for(int i=0; i< 1000; ++i)
			{
				int rd = is.read(buf);
				if(rd == -1)
				{
					System.out.println("EOF");
					break;
				}
				System.out.println("got message: "+  parseCStr(buf));
				s = "sending message #"+i+ " from java";
				os.write(s.getBytes());

				try{
					Thread.sleep(100);
					}
					catch(InterruptedException e)
					{}
			}

			soc.close();  //and close it
		}
		catch(java.io.IOException e)
		{
			e.printStackTrace();
		}

	}

}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>
 

GForge.cis.cornell.edu is brought to you by

Cornell Computing and Information Science


Powered By GForge Collaborative Development Environment