Connection problem

Post here your questions about the Java client / Android API for SFS2X

Moderators: Lapo, Bax

Post Reply
esin
Posts: 6
Joined: 25 Feb 2011, 08:40

Connection problem

Post by esin »

Hello

Recently i wrote in "Java client API" topic about my problem. I'd like to thank everybody who answered me there, but none of offered in that topic decisions have helped. So i decided to create separate topic to describe my problem in details.

It's my first experience with SmartFoxServer. I downloaded it (i work under Ubuntu 10.04), unzipped, and ran (exactly as in documentation). I can connect to admin tool, i can run flash examples from localhost:8080 page (SimpleChat, SFSTris, etc.). I can connect to admin tool and run examples from remote computer. Everything seems to be ok. Even Unity3D FPS Demo works excellent.

Then i tried to write my Java client. I looked up in Java SimpleChat example how to create SmartFoxClient object, how to add event listeners, etc.
That's my application source code:

Code: Select all

package main;

public class ClientSFSMain {

	public static void main(String[] args)
	{
		
		Controller controller = new Controller();		
		controller.initialize();
	}

}

Code: Select all

package main;

import it.gotoandplay.smartfoxclient.ISFSEventListener;
import it.gotoandplay.smartfoxclient.SFSEvent;
import it.gotoandplay.smartfoxclient.SmartFoxClient;
import java.util.logging.Level;

public class Controller implements ISFSEventListener {
	
	private SmartFoxClient sfs;
	
	public void initialize()
	{
		sfs = new SmartFoxClient(true);		
		sfs.addEventListener(SFSEvent.onConfigLoadSuccess, this);
        sfs.addEventListener(SFSEvent.onConfigLoadFailure, this);
        sfs.addEventListener(SFSEvent.onConnection, this);
        sfs.addEventListener(SFSEvent.onConnectionLost, this);
        sfs.addEventListener(SFSEvent.onDebugMessage, this);
        
        sfs.getLogger().setLevel(Level.ALL);
		
		log("controller created");
		
		sfs.loadConfig("config.xml", false);
	}

	@Override
	public void handleEvent(SFSEvent event) 
	{
		if(event.getName().equals(SFSEvent.onDebugMessage))
		{
			log(event.getParams().toString());
		}
		else if(event.getName().equals(SFSEvent.onConfigLoadSuccess))
        {
			log("config loaded");
			
			sfs.defaultZone = "SimpleChat";
			
            sfs.connect(sfs.ipAddress, sfs.port);
        }
        else if(event.getName().equals(SFSEvent.onConfigLoadFailure))
        {
            log("config load error");
        }
        else if(event.getName().equals(SFSEvent.onConnection))
        {
        	
        	log("onConnection");
            if(event.getParams().getBool("success"))
            {
                log("connected");
            }
            else
            {
                log("not connected");
                System.exit(-2);
            }
        }
        else if(event.getName().equals(SFSEvent.onConnectionLost))
        {
        	log("connection lost");
        	System.exit(-1);
        }
	}
	
	private void log(String str)
	{
		System.out.println(str);
	}

}
And config.xml file:

Code: Select all

<SmartJChatConfig>
  <ip>127.0.0.1</ip>
  <udpIp>127.0.0.1</udpIp>
  <port>9933</port>
  <udpPort>9933</udpPort>
  <zone>SimpleChat</zone>
   
  <debug>true</debug>

  <httpPort>8080</httpPort>

  <useBlueBox>true</useBlueBox>
  <blueBoxPollingRate></blueBoxPollingRate> 

</SmartJChatConfig>
If i try to run this application, it throws an exception

java.io.FileNotFoundException: /xml/dtds/cross-domain-policy.dtd (No such file or directory)

If i change crossdomain.xml file (replace string after SYSTEM to "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd"), application won't throw exception. But it cannot connect to server.
In console i see, that controller created, config loaded. After this there are many messages

Mar 1, 2011 1:20:30 PM it.gotoandplay.smartfoxclient.SmartFoxClient debugMessage
INFO: [ RECEIVED ]: , (len: 0)
{ "message" : "[ RECEIVED ]: , (len: 0)" }

and then "connection lost" string appears.

Can you help?
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post by ThomasLund »

Hi,

In the other thread I posted a complete junit testcase for you to try.

Did that work for you?

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
esin
Posts: 6
Joined: 25 Feb 2011, 08:40

Post by esin »

Yes, i saw it. Thank you very much!
But I didn't understand, how to run it. I Created new Java-project, copy-pasted your code there, added Java client API .jar files to classpath, but eclipse underlined all classes and packages declarations with red line. All packages you imported it can't resolve. Package for assert classes is not important. Main packages and classes are: SmartFox, SFSEvent (there are such classes in server and client APIs), BaseEvent, and IEventListener interface. Could you describe, how to import these classes? I'd be very grateful to you

At least, I used your config.xml file for my application :)

P.S. as I understood from your code, it does the same as my code, but in some other way. You use autoconnect option, I tried too, but result didn't change.
Last edited by esin on 01 Mar 2011, 09:36, edited 1 time in total.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post by ThomasLund »

OK - if you look at your imports it looks like you are including totally wrong classes. I dont know if this is a SFS1 example you used as template or maybe included server side jar files?!?!

But the code looks all wrong from the start. Try to look at my test code. You should only be missing the JUnit jar potentially - which you can grab here:

https://github.com/KentBeck/junit/downloads

Where did you get the example from that you've copied?!?!

/Thomas

-----
Edit:

It truly looks like a SFS1 version code. So to clarify this, I need to know if you are running a SFS1 or SFS2X server. The Java API posted is a 2X API and is not backward compatible
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
esin
Posts: 6
Joined: 25 Feb 2011, 08:40

Post by esin »

I get this example from SFS2X Java Client API archive

http://www.smartfoxserver.com/2X/quickU ... API-b1.zip

Lapo posted it in this thread:

http://forums.smartfoxserver.com/viewto ... sc&start=0

In this archive there is a /examples/Java2SE/SmartFoxJChat/ folder with Java application example

P.S. I use SmartFoxServer 2X, sorry that I did't mention it
esin
Posts: 6
Joined: 25 Feb 2011, 08:40

Post by esin »

oh! my great apologize! I downloaded wrong archive. Thank you very much, Thomas! For your patience and help
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post by ThomasLund »

Hmmm - there is no examples folder in that zip when I open it. There is a bin/ and there is a SFS2X-ClientTests.zip

The client tests has a src folder with a BasicClientExample.java file in there which uses the correct code.

My best guess here is, that you are trying to run a old SFS1 client code example against the 2X server.

The client code you pasted here is definitely wrong, and you should try to look at the BasicClientExample in the zip

Hope that helps you along

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post by ThomasLund »

Ahh - you managed to post while writing.

Happy you got it running!!! Good luck, and ask away! Here to help

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
Post Reply