GithubHelp home page GithubHelp logo

javax.server's People

Contributors

mordechaim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

javax.server's Issues

How to obtain the messages in the "messages" queues in the server and client

Hey! I managed to build your project and have written a main method in another class I made to test the connection:

package javax.server;

public class JavaxServerMain {
public static void main(String[] args){
String serverAddress = "";
int port = 5454;
int messageHandlingThreadCount = 0;

    // Initialize and start the Server
    Server server = new Server(port, messageHandlingThreadCount);
    server.start();

    // Initialize and start the Client
    Client client = new Client(serverAddress, port);
    client.start();

    // Prepare the messages
    String clientMsg = "Incoming from the client";
    String serverMsg = "Incoming from the server";
    String clientAuth = Integer.toString(client.getClientId());
    String serverAuth = "The Server...";
    QuadMsg toServer = new QuadMsg(clientMsg, clientAuth);
    QuadMsg toClient = new QuadMsg(serverMsg, serverAuth);

    while(true){
        // Have the client send a message to the server, and the server to the client.
        client.send(toServer);
        server.sendToAll(toClient);
    }


    // Now here, obtain the messages and print them.




    // When you are done, shut down the client and the server
    //client.shutDown();
    //server.shutDown();
}

}

I managed to step through the program, and see that the client object is found within the server's clients map, however, it seems that the messages I have been sending between the server and client rarely make it to each other's "messages" queues. While nothing ever arrives on the client's side, the server's side occasionally gets a message, which then disappears the very next step.

Do you know what I am missing, and what I can do to retrieve the messages from the queues?

Communicating with other languages

Mordechaim,

Thanks again for your help earlier! I managed to modify your script so that JSON objects can be passed between clients instead of Serializable objects. My reason for doing this is so that I can send information from a Java client to a Python Client. I am trying to better understand how the Acception class works in the server.java file.

I try to have my Python client connect to the Server while the main function is waiting for a user input. It appears that my Python client manages to connect to the socket, but it doesn't seem to trigger in the server.java's acception.run() or acception.authenticate() methods in the debugger, and after sending my message and checking the server's list of clients, it only lists the two Java clients that I initialize in the beginning, but not the Python one.

Clearly, the server is either rejecting or not noticing the Python client trying to connect to the designated socket. However, the Python client always seems to receive some garbage characters from the server upon connecting to it.

What does it take for a Java client to connect to the server? I am hoping to mimic the behavior in Python so that I can trick the server into at least accepting the client and assigning it an id (not necessarily a Client object).

My main file:

package javax.server;

import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import org.json.*;

public class JavaxServerMain {
public static void main(String[] args){
String serverAddress = "";
int port = 50012;
int messageHandlingThreadCount = 0;

    // toBRECCIAQ is for messages received from the server directed towards BRECCIA
    // toQUADQ is for messages received from the server directed towards the Quadcopter
    ArrayBlockingQueue toBRECCIAQ = new ArrayBlockingQueue(1024);
    ArrayBlockingQueue toQUADQ = new ArrayBlockingQueue(1024);

    // Initialize and start the Server
    Server server = new Server(port, messageHandlingThreadCount);
    server.start();

    // Initialize and start the Clients
    Client brecciaClient = new Client(serverAddress, port);
    Client quadClient = new Client(serverAddress, port);
    brecciaClient.start();
    quadClient.start();

    String response = "start text";

    while(!response.equals("exit"))
    {
        // Ask for user input
        Scanner input = new Scanner(System.in);
        response = input.nextLine();

        JSONObject obj = new JSONObject();

        try {
            obj.put("message", response);
            obj.put("sender", brecciaClient.getClientId());
            obj.put("receiver", quadClient.getClientId());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        // Have the client send a message to the server, and the server to the client.
        boolean brecciaMsgSent = brecciaClient.send(obj);
        System.out.println(brecciaMsgSent);
        //boolean quadMsgSent = quadClient.send(toBRECCIA);
    }

    /*
    String userInput = "Beginning";
    String termInput = "exit";
    while(!userInput.equals(termInput)) {

    }*/

    // Now here, obtain the messages and print them.
    try {
        brecciaClient.sync();
        quadClient.sync();
    } catch (InterruptedException e) {
        System.out.println("Sync didn't work...");
        e.printStackTrace();
    }

    // When you are done, shut down the client and the server
    brecciaClient.shutDown();
    quadClient.shutDown();
    server.shutDown();
}

}

I overloaded the send functions in the client, server and connectionToClient classes so that they can accept Serializable or JSONObjects. Other than that, the code is the other files is the same.

Any tips?

Thank you for your help!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.