Getting connections to work

EDIT: see "http://forum.captiveimagination.com/index.php/topic,799.0.html"



here is our situation. we have tried to modify the chat test from JGN to work over the internet. We have only gotten it to partially work.



Basically, we want the server and the client to be runnable .jar files.

So, we simple changed the connected IP address for the client.



If we run client.java and server.java (modified from the chat test) inside of eclipse then it works. I can connect to my own server, my friends can connect from their houses running the client.java inside of eclipse



so we decided we wanted to take it the next step further, so that it isnt required for the JGN or Eclipse to be installed on ones computer.



so here is what we did next. we took the client.java and server.java and exported them (from inside eclipse Export -> runnable .jar) to client.jar and server.jar (remember, these are the same .java files that worked inside eclipse!)



so now we have these .jar files. On my computer, if I run the server and then the client, it connects, but this is the end of our success. With this server.jar running, none of my friends can connect with client.jar, and only I can connect. Basically all that happens is (from watching inside of Task Manager) that the client tries to connect but doesnt (it gets to like 8000k memory and stops, while a healthy client goes to 8000, and then jumps up to 20,000 after it connects)



So we are at a loss… we have tried changing some small stuff code wise in the server



namely:


      InetSocketAddress reliableAddress = new InetSocketAddress((InetAddress)null, 9100);
      InetSocketAddress fastAddress = new InetSocketAddress((InetAddress)null, 9200);


and

      InetSocketAddress reliableAddress = new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(), 9100);
      InetSocketAddress fastAddress = new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(), 9200);


which connects to my internal IP (but it worked in eclipse!)

so, here are the two .java files that the .jar files are exported from (basically just the chat test from JGN, except modified to "supposedly" connect to my server)

Server

/**
 * Copyright (c) 2005-2006 JavaGameNetworking
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'JavaGameNetworking' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Created: Jul 31, 2006
 */
package com.captiveimagination.jgn.test.chat;

import java.net.*;

import com.captiveimagination.jgn.*;
import com.captiveimagination.jgn.clientserver.*;
import com.captiveimagination.jgn.event.*;

/**
 * @author Matthew D. Hicks
 */
public class ChatServer extends DynamicMessageAdapter {
   public ChatServer() throws Exception {
      JGN.register(NamedChatMessage.class);
      
      InetSocketAddress reliableAddress = new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(), 9100);
      InetSocketAddress fastAddress = new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(), 9200);
      String ii = InetAddress.getLocalHost().getHostAddress();
       System.out.println(ii);
      JGNServer server = new JGNServer(reliableAddress, fastAddress);
      server.addMessageListener(this);
      JGN.createThread(server).start();
   }


   public void messageReceived(NamedChatMessage message) {
      System.out.println("Message received: " + message.getPlayerName() + ", " + message.getText() + ", " + message.getDestinationPlayerId());
   }

   public static void main(String[] args) throws Exception {
      new ChatServer();
   }
}


Client

/**
 * Copyright (c) 2005-2006 JavaGameNetworking
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'JavaGameNetworking' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Created: Jul 31, 2006
 */
package com.captiveimagination.jgn.test.chat;

import com.captiveimagination.jgn.JGN;
import com.captiveimagination.jgn.clientserver.JGNClient;
import com.captiveimagination.jgn.event.DebugListener;
import com.captiveimagination.jgn.event.DynamicMessageAdapter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.InetSocketAddress;

/**
 * @author Matthew D. Hicks
 */
public class ChatClient extends DynamicMessageAdapter implements ActionListener {
   private JGNClient client;
   private String nickname;
   private JTextPane textPane;
    private JTextField textField;

   public ChatClient() throws Exception {
      JGN.register(NamedChatMessage.class);

      client = new JGNClient();
      client.addMessageListener(this);
      client.addMessageListener(new DebugListener("ChatClient>"));
      JGN.createThread(client).start();

      InetSocketAddress reliableServerAddress = new InetSocketAddress(InetAddress.getByName("24.234.70.244"), 9100);
      InetSocketAddress fastServerAddress = new InetSocketAddress(InetAddress.getByName("24.234.70.244"), 9200);

      client.connectAndWait(reliableServerAddress, fastServerAddress, 5000);
      nickname = JOptionPane.showInputDialog("Connection established to servernnPlease enter the name you wish to use?");
      initGUI();
   }

   private void initGUI() {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   // TODO fix
      frame.setTitle("Chat Client - " + nickname);
      frame.setSize(300, 300);
      Container c = frame.getContentPane();
        c.setLayout(new BorderLayout());
        textPane = new JTextPane();
        textPane.setText("");
        textPane.setEditable(false);
        c.add(BorderLayout.CENTER, textPane);
        textField = new JTextField();
        textField.addActionListener(this);
        c.add(BorderLayout.SOUTH, textField);
        frame.setVisible(true);
   }

   public void actionPerformed(ActionEvent evt) {
      String txt = textField.getText().trim();
      if (txt.length() > 0) {
         NamedChatMessage message = new NamedChatMessage();
         message.setPlayerName(nickname);
         message.setText(txt);

         if (txt.startsWith("srv")) client.sendToServer(message);
         else if (txt.startsWith("0")) client.sendToPlayer(message, (short) 0);
         else client.broadcast(message);

         textField.setText("");
         writeMessage(client.getPlayerId(), nickname, message.getText());
      }
   }

   public void messageReceived(NamedChatMessage message) {
      writeMessage(message.getPlayerId(), message.getPlayerName(), message.getText());
   }


   private void writeMessage(short playerId, String playerName, String text) {
      String message = "[" + playerName + ":" + playerId + "]: " + text;
      if (textPane.getText().length() == 0) {
            textPane.setText(message);
        } else {
            textPane.setText(textPane.getText() + "rn" + message);
        }
   }

   public static void main(String[] args) throws Exception {
      new ChatClient();
   }
}



So why this discrepancy between inside of eclipse, and in jar file???

Any help would be awesome, we have been plugging at this for a couple days now. Thanks!

It doesn't look like you do, but the first thing I would do is change your System.out to print to a file.(Or I think executing from console would work, that way the exception will be printed to the console(Start->Run->cmd - java -jar filename.jar). But we would be able to help a lot more(Or maybe the error message would make it more obvious to you even) if you had the exception that was being thrown so you could look over the stack-trace and see where it is failing.(Not to mention I bet the networking code gives a detailed message of exactly what happened).



If it's failing right after the 8000k mark and not sitting there for awhile it could mean a few things, all I can tell from that is it's not timing out, Of course I could be wrong(or rather probably am wrong) but we could help you a lot more with a stack-trace and an exception message.(I assume it's throwing an exception message, maybe not).

hello.



sorry i didnt post the fix for this.



Yes we fixed this problem, and the fix for another problem as well.



This problem was simple, the .jar exporter was not exporting the right file. it was exporting the client.java and server.java out of JGN instead of my client.java and server.java. All I had to do was rename and export it again.



However…



If you are trying to export JME projects to runnable .jar files, and they wont run, you need to use FatJar and put the library .dll files in the same folder as your .jar file.



:slight_smile: