DarkStar - server/Client Issue

Hello, mm , as am working on my project, i figured out that i cant do anything before i start learning some java Networking (for MMORPG), so i'v chosen DarkStar for that.



In short, i am pretty new to it (just started today), so i need help with the following thing:


  • I am writing a basic Server/client Networking app , i am fine till now i guess, but i dont know how to keep the server-coded Application running on my pc? or should i do it in some other way.since its kind of missing in the Tutorial (no main body), so i tried to run the server side and added a kind of senseless loop in main, didnt work.



    My after all Goal is to try the very basic application, so that the server would listens to the client's loggin and kick him out after.: 

    CODE:



    Client-Side:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//ClientSide...
[code]
package my.DarkStar;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;
import java.net.PasswordAuthentication;
import java.nio.ByteBuffer;
import java.util.Properties;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.sun.sgs.client.ClientChannel;
import com.sun.sgs.client.ClientChannelListener;
import com.sun.sgs.client.simple.SimpleClient;
import com.sun.sgs.client.simple.SimpleClientListener;
/**
* A simple GUI client that interacts with an SGS server-side app.
* It presents a basic chat interface with an output area and input
* field.
* <p>
* The client understands the following properties:
* <ul>
* <li><code>{@value #HOST_PROPERTY}</code> <br>
* <i>Default:</i> {@value #DEFAULT_HOST} <br>
* The hostname of the server.<p>
*
* <li><code>{@value #PORT_PROPERTY}</code> <br>
* <i>Default:</i> {@value #DEFAULT_PORT} <br>
* The port that the server is listening on.<p>
*
* </ul>
*/
public class DarkStar1 extends JFrame
implements SimpleClientListener, ActionListener
{
/** The version of the serialized form of this class. */
private static final long serialVersionUID = 1L;
/** The name of the host property. */
public static final String HOST_PROPERTY = "tutorial.host";
/** The default hostname. */
public static final String DEFAULT_HOST = "localhost";

public static final String PORT_PROPERTY = "tutorial.port";
/** The default port. */
public static final String DEFAULT_PORT = "1139";
/** The message encoding. */
public static final String MESSAGE_CHARSET = "UTF-8";
/** The output area for chat messages. */
protected final JTextArea outputArea;
/** The input field for the user to enter a chat message. */
protected final JTextField inputField;
/** The panel that wraps the input field and any other UI. */
protected final JPanel inputPanel;
/** The status indicator. */
protected final JLabel statusLabel;
/** The {@link SimpleClient} instance for this client. */
protected final SimpleClient simpleClient;
/** The random number generator for login names. */
private final Random random = new Random();
// Main
/**
* Runs an instance of this client.
*
* @param args the command-line arguments (unused)
*/
public static void main(String[] args) {
new DarkStar1().login();
}
// HelloUserClient methods
/**
* Creates a new client UI.
*/
public DarkStar1() {
this(DarkStar1.class.getSimpleName());
}
/**
* Creates a new client UI with the given window title.
*
* @param title the title for the client's window
*/
protected DarkStar1(String title) {
super(title);
Container c = getContentPane();
JPanel appPanel = new JPanel();
appPanel.setFocusable(false);
c.setLayout(new BorderLayout());
appPanel.setLayout(new BorderLayout());
outputArea = new JTextArea();
outputArea.setEditable(false);
outputArea.setFocusable(false);
appPanel.add(new JScrollPane(outputArea), BorderLayout.CENTER);
inputField = new JTextField();
inputField.addActionListener(this);
inputPanel = new JPanel();
inputPanel.setLayout(new BorderLayout());
populateInputPanel(inputPanel);
inputPanel.setEnabled(false);
appPanel.add(inputPanel, BorderLayout.SOUTH);
c.add(appPanel, BorderLayout.CENTER);
statusLabel = new JLabel();
statusLabel.setFocusable(false);
setStatus("Not Started");
c.add(statusLabel, BorderLayout.SOUTH);
setSize(640, 480);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
simpleClient = new SimpleClient(this);
}
/**
* Allows subclasses to populate the input panel with
* additional UI elements. The base implementation
* simply adds the input text field to the center of the panel.
*
* @param panel the panel to populate
*/
protected void populateInputPanel(JPanel panel) {
panel.add(inputField, BorderLayout.CENTER);
}
/**
* Appends the given message to the output text pane.
*
* @param x the message to append to the output text pane
*/
protected void appendOutput(String x) {
outputArea.append(x + "n");
}
/**
* Initiates asynchronous login to the SGS server specified by
* the host and port properties.
*/
protected void login() {

try {
Properties connectProps = new Properties();
connectProps.put("host", "Server x");
connectProps.put("port", "port x");
simpleClient.login(connectProps);
} catch (Exception e) {
e.printStackTrace();
disconnected(false, e.getMessage());
}
}
/**
* Displays the given string in this client's status bar.
*
* @param status the status message to set
*/
protected void setStatus(String status) {
appendOutput("Status Set: " + status);
statusLabel.setText("Status: " + status);
}
/**
* Encodes a {@code String} into a {@link ByteBuffer}.
*
* @param s the string to encode
* @return the {@code ByteBuffer} which encodes the given string
*/
protected static ByteBuffer encodeString(String s) {
try {
return ByteBuffer.wrap(s.getBytes(MESSAGE_CHARSET));
} catch (UnsupportedEncodingException e) {
throw new Error("Required character set " + MESSAGE_CHARSET +
" not found", e);
}
}
/**
* Decodes a {@link ByteBuffer} into a {@code String}.
*
* @param buf the {@code ByteBuffer} to decode
* @return the decoded string
*/
protected static String decodeString(ByteBuffer buf) {
try {
byte[] bytes = new byte[buf.remaining()];
buf.get(bytes);
return new String(bytes, MESSAGE_CHARSET);
} catch (UnsupportedEncodingException e) {
throw new Error("Required character set " + MESSAGE_CHARSET +
" not found", e);
}
}
/**
* Returns the user-supplied text from the input field, and clears
* the field to prepare for more input.
*
* @return the user-supplied text from the input field
*/
protected String getInputText() {
try {
return inputField.getText();
} finally {
inputField.setText("");
}
}
// Implement SimpleClientListener
/**
* {@inheritDoc}
* <p>
* Returns dummy credentials where user is "guest-&lt;random&gt;"
* and the password is "guest." Real-world clients are likely
* to pop up a login dialog to get these fields from the player.
*/
public PasswordAuthentication getPasswordAuthentication() {
String User = "x";
setStatus("Logging in as " + User);
String password = "y";
return new PasswordAuthentication(User, password.toCharArray());

}
/**
* {@inheritDoc}
* <p>
* Enables input and updates the status message on successful login.
*/
public void loggedIn() {
inputPanel.setEnabled(true);
setStatus("Logged in");
}
/**
* {@inheritDoc}
* <p>
* Updates the status message on failed login.
*/
public void loginFailed(String reason) {
setStatus("Login failed: " + reason);
}
/**
* {@inheritDoc}
* <p>
* Disables input and updates the status message on disconnect.
*/
public void disconnected(boolean graceful, String reason) {
inputPanel.setEnabled(false);
setStatus("Disconnected: " + reason);
}
/**
* {@inheritDoc}
*/
public ClientChannelListener joinedChannel(ClientChannel channel) {
return new NullClientChannelListener();
}
/**
* {@inheritDoc}
* <p>
* Decodes the message data and adds it to the display.
*/
public void receivedMessage(ByteBuffer message) {
appendOutput("Server sent: " + decodeString(message));
}
/**
* {@inheritDoc}
* <p>
* Updates the status message on successful reconnect.
*/
public void reconnected() {
setStatus("reconnected");
}
/**
* {@inheritDoc}
* <p>
* Updates the status message when reconnection is attempted.
*/
public void reconnecting() {
setStatus("reconnecting");
}

// Implement ActionListener
/**
* {@inheritDoc}
* <p>
* Encodes the string entered by the user and sends it to the server.
*/
public void actionPerformed(ActionEvent event) {
if (! simpleClient.isConnected())
return;
String text = getInputText();
send(text);
}
/**
* Encodes the given text and sends it to the server.
*
* @param text the text to send.
*/
protected void send(String text) {
try {
ByteBuffer message = encodeString(text);
simpleClient.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* A ClientChannelListener that does nothing at all (this basic
* client does not support channels).
*/
private static class NullClientChannelListener
implements ClientChannelListener
{
/** {@inheritDoc} */
public void leftChannel(ClientChannel channel) {
System.out.println("Unexepected call to leftChannel");
}
/** {@inheritDoc} */
public void receivedMessage(ClientChannel channel, ByteBuffer message) {
System.out.println("Unexepected call to receivedMessage");
}
}
}
[/code]




Server-Side:(here i need help i guess)...
[code]
//Server-Sided ( LEFT WITHOUT A MAIN).
package my.DarkStar;

import java.io.Serializable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sun.sgs.app.AppListener;
import com.sun.sgs.app.ClientSession;
import com.sun.sgs.app.ClientSessionListener;
/**
* Simple example of listening for user {@linkplain AppListener#loggedIn login}
* in the Project Darkstar Server.
* <p>
12 In fact, ClientSession describes the new connection session, the user being one of those parameters. This distinction is
important, in that you cannot save a ClientSession object and expect it to be valid after the session has ended, which is
when the user disconnects.
32 04/24/08 Project Darkstar Server Application Tutorial
* Logs each time a user logs in, then kicks them off immediately.
*/
public class DarkStar2
implements AppListener, // to get called during startup and login.
Serializable // since all AppListeners are ManagedObjects.
{
    public static void main(String[] args){
        while(true){
           
        }
    }
           
/** The version of the serialized form of this class. */
private static final long serialVersionUID = 1L;

private static final Logger logger =
Logger.getLogger(DarkStar2.class.getName());
public void initialize(Properties props) {

}

public ClientSessionListener loggedIn(ClientSession session) {
// User has logged in
logger.log(Level.INFO, "User {0} almost logged in", session.getName());
// Kick the user out immediately by returning a null listener
return null;
}
}
[/code]

Thanks incase of Help.

some one please reply :slight_smile: specially DarkFrog :)and id like to take his private email if possible yes darkfrog? there is privae things to talk about, thanks

Can’t help you with the code, but check out project snowman. It is a jMe - Darkstar example game

Nader said:

(no main body)

You're not supposed to run PDS server applications in their own main method. Instead you start the PDS server application and pass it the class name of your server's PDS entry point in one of several ways.

Nader said:

(just started today)

Take some more time to read the extensive PDS server and client tutorials, it's all explained in there.

Other than that, you'll have much more success at getting answers if you ask PDS specific questions over at the project darkstar forum!