Transmit - Multi-Threaded Server Socket Library

Discussion in 'Resources' started by kumpelblase2, Jan 21, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    soliddanii

    lol thanks a lot, it works perfectly. Thanks a lot, all is ok now :D you are amazing :D
     
  2. Offline

    wouter0100

    This won't work in your main post:
    server.sendToClient(clientID, "Hey client!");


    server.sendToClient(clientID, new Packet("command", "", new String[] {"Hey Client!"}));
     
  3. I know. Therefor I created a little notice:
     
  4. Offline

    soliddanii

    hi again! :) I've a new doubt now. I made the client to send some information, the server recieves it succesfully and it responds sending some informatin to the client. How can i make the client to recieve that information? can you give me an examle like the one above? thanks :rolleyes: [diamond]
     
  5. Well, it's most likely using sockets. there are a lot of those around the internet. But it would be an idea to make a client lib :) However, I might only work on it when I have time to...
     
  6. Offline

    wouter0100

    with a BufferedReader, a simple while loop.

    I have created a Java Chat, all connections are sockets etc, and i'm using something like this:

    Code:
    in = new BufferedReader(
                        new InputStreamReader(socket.getInputStream()));
    String message;
    while ((message = in.readLine()) != null)
    {
          System.out.println(message);       
    }    
    <Edit by Moderator: Redacted mediafire url>
     
    Last edited by a moderator: Nov 11, 2016
  7. Offline

    soliddanii

    sorry it doesn't work for me :'(

    I've the code like this (command , respone, and argfinal are variables):

    .......
    Code:
    String toSend = "{\"packets\":[{\"command\":\""+command+"\",\"responseCommand\":\""+response+"\",\"args\":"+argfinal+"}]}";
            socket.getOutputStream().write(toSend.getBytes("UTF-8"));
                socket.getOutputStream().flush(); 
    ...... Then the server recieves the information succesfully and responds like this(id is the id captured, comprobarUsuario is a String, and main.mysqlpass is a string):

    Code:
    main.server.sendToClient(ID, new Packet(comprobarUsuario, "", new String[] {main.mysqlpass}));
    ......Then the client code continues like how? How do I recieve the information? I tryed your way, but it doesn´t work. Thanks :oops:
     
  8. I quickly wrote a client for this, but I need to test it and make an example. Hopefully it's done today.
     
  9. Offline

    Lolmewn

    Is this 'fixed' if you use JSON?
     
  10. nope, json is just the format how the message inside the packet is structured. However, I don't see anything that is 'broken' that should get fixed.

    btw:
    Source of the client is already up! (You would need to compile it yourself, or wait for the binaries)
    link: Dropbox
     
  11. Offline

    soliddanii

    you are really saving my work ;D Thank you so much, i downloaded the source, but i will wait for the binaries ;D
     
  12. hm... Looks like receiving doesn't work for some reason... or the server doesn't send anything :confused:
    however, it'll take some more time, sry.

    Problems solved. Please re-download the server, to prevent any problems caused. I still don't know why I didn't noticed it earlier.... However, the problem was when you use .sendToClient(id, packet) and there's only one client connected, it was getting ignored. It works again now.

    Therefor the client does work as well. Here's a little example on how to use it:
    Code:
    TransmitClient client = new TransmitClient("127.0.0.1", 1400); // Creating an instance
    client.setListener(new ClientListener() // Creating a custom listener. There is currently only an event if the server send you something.
    {
        public void onServerMessageRecieve(Packets packets)
        {
            System.out.println("We got some data!");
        }
    });
    client.connect(); // Connects to the server with the ip and port provided in the constructor (see above)
    Packet p = new Packet();
    p.command = "";
    p.args = new String[0];
    p.responseCommand = "";
    client.send(p); // sends the 'packet' to the server
    Source: Dropbox
    Binary: Dropbox

    Have fun!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  13. Offline

    Lolmewn

    Well, I've got a multi-threaded client-server pair as well, but the problem is that it's difficult to separate the data from each other. And for some reason, data sometimes gets sent in pieces, making me unable of using a continuously running while loop (since other data will end up in there as well.)

    I'm wondering how you do this, going to check source soon :)
     
  14. i'm not doing this at all. But it would give you an error in the process of parsing the json string when it's not complete.
     
  15. Offline

    soliddanii

    Thanks, the client and the server both work fine. Great job. The last quiestion, how do i get the parts (command, rresponse, args) from the recieved packet ? (the packet recieved from the server). I suposed it would be (from the listener) packets.getCommand(); like in the server, but aparently its not like this :p
     
  16. I made the client rather quick, so things might not be thought out. It'll give you a packets object which has a variable containing all commands. each of these commands has three variables like you know from the server: command, responsecommand and args. all of them are strings but args is a string-array.

    Code:
    for(Packet pa : event.packets)
    {
        System.out.println("Command: " + pa.command + ", ResponseCommand: " + pa.responseCommand + ", Argument count: " + pa.args.length);
    }
    
    something like that. Sorry for any mistakes, but writing on a smartphone is not always the best idea.
     
  17. Offline

    soliddanii

    I wrote that inside of "onServerMessageRecieve". event is underlined with red. Sorry but im very noob on this xD

    I noticed that the packet is shown in the console (in string format)
     
  18. you need to rename event to the name of the variable defined in the function. I think it's 'packets' in the default case.
     
  19. Offline

    soliddanii

    just like you said, thanks :D
     
  20. Offline

    JoNiProductions

    Hi kumpelblase2
    "manchmal" wenn ich etwas von meinem Client (Nicht Java) zu meinem Server schicke (deine Library)
    kommt dieser fehler:

    2012-08-11 15:43:13 [SEVERE] Exception in thread "Thread-13"
    2012-08-11 15:43:13 [SEVERE] java.lang.ArrayIndexOutOfBoundsException: -1
    2012-08-11 15:43:13 [SEVERE] at java.util.ArrayList.get(Unknown Source)
    2012-08-11 15:43:13 [SEVERE] at de.kumpelblase2.transmit.TransmitServer.handle(TransmitServer.java:190)
    2012-08-11 15:43:13 [SEVERE] at de.kumpelblase2.transmit.ClientConnectionThread.run(ClientConnectionThread.java:127)
    2012-08-11 15:43:13 [SEVERE] at java.lang.Thread.run(Unknown Source)

    Hoffe du kannst mir weiter helfen. Echt coole Library sonst funktioniert alles einwandfrei.
     
  21. Offline

    Milkywayz

  22. Offline

    spaceweather

    Thx works really great!
    @wouter0100 your posted php code is wrong!
    you have:
    PHP:
    socket_send($sock$jsonObjectstrlen($jsonObject), MSG_DONTROUTE);
    but it has to be:
    PHP:
    socket_send($sock$jsonStringstrlen($jsonString), MSG_DONTROUTE);
    PS: would be greate if you add an optional argument "password" to the JSON notation! Now everybody how knows the port is able to send sockets there. So I can check if i should excecute this command or not...
    And a ip-address in ClientRecieveEvent whould be nice for more safe :D
     
  23. Does anyone have a copy of this library for me? The download links aren't working for me.
     
  24. Offline

    Summerfeeling

    Alternative download link or a copy were nice! :s
     
Thread Status:
Not open for further replies.

Share This Page