#Json and sockets

1 messages ยท Page 1 of 1 (latest)

valid rivet
#

can someone give me a link to find information how to use json with sockets?

ebon stormBOT
#

<@&987246487241105418> please have a look, thanks.

ebon stormBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

sinful garnet
#

but not sure if thats what you want

valid rivet
#

Im not sure

#

someone was helping me but I had to go and I dont think he wants to help me anymore

sinful garnet
#

not sure what you mean

#

ok thats probably not helpful

#

its not java xD

ebon stormBOT
#

An error has occurred while trying to communicate with ChatGPT. Please try again later

sinful garnet
valid rivet
#

Im trying to create server/client for my GUI design

sinful garnet
#

ok, did you already manage to create some kind of server/client with sockets?
because your question directly goes to sending json between socket connection

valid rivet
#

yeah we were working on it together

#

the server is finished but I dont know how to do the client

sinful garnet
sinful garnet
#

and what you want to do next

valid rivet
ebon stormBOT
valid rivet
#

this is the client

#

import dev.mccue.json.Json;
import dev.mccue.json.JsonReader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Server {
    private MenuList menuList;

    public Server(MenuList menuList){
        this.menuList = menuList;
    }

    public void handleNewConnection(){
        try (ServerSocket server = new ServerSocket(25566)) {
            System.out.println("server started, waiting for connection...");
            Socket socket = server.accept();
            BufferedReader in = new BufferedReader(new
                    InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(),
                    true);
            System.out.println("connection established, listening...");
            JsonReader reader = Json.reader(in);
            for (Json json: reader){
                Json json1 = handleQuery(Query.fromJson(json));
                Json.write(json1, out);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        System.out.println("server terminating...");
    }

    private Json handleQuery(Query query){
        System.out.println("handing query");

        if (query instanceof RequestMenuList)
        {
            return handleMenuList();
        } else if (query instanceof RequestMenu requestMenu) {
            return handleMenu(requestMenu);
        } else if (query instanceof AddMenu addMenu){
            return handleAddMenu(addMenu);
        }

        throw new RuntimeException();
    }
    //S: "ol%<MENU 1 name>%<MENU 2 name>"
    private Json handleMenuList(){
        return menuList.toJson();
    }

    private Json handleMenu(RequestMenu requestMenu){
        //Subject to error
        int index = requestMenu.getMenuIndex();
        Menu menu = menuList.getMenu(index);
        return menu.toJson();
    }

    private Json handleAddMenu(AddMenu addMenu){
        menuList.addMenu(addMenu.getMenu());
        return menuList.toJson();
    }
}```
#

I have no experience with sockets

sinful garnet
#

also you are using @emccue's JSON library

#

and I have no idea how to use it

valid rivet
#

yeah he was helping me but I had to go

sinful garnet
#

but it shouldnt really matter which json library you use

#

also you know how to generally get data in the client from the server right?

wet zenith
#

whats the problem you're having with the client? is it not connecting? is it connecting but data isnt sending?

valid rivet
#

its okay Im just going to go back to regular coding without using json

#

its too confusing for me

sinful garnet
#

but you want to improve right?

valid rivet
#

no I dont have time for this

#

I have only 2 days left

sinful garnet
#

why not taking that step and creating your socket connection using json

sinful garnet
#

are there any requirements for this application or can you just implement your own ideas using the time you have

frosty sleet
#

lol man im still here

#

@sinful garnet For context, their partner did the server code for them

#

but they were sending data in this wackadoodle thing

#

lo%thing#custom<>

valid rivet
#

can you help me more emc?

#

I mean its almost done

frosty sleet
#

sure. give me a few minutes

valid rivet
#

Im just a slow learner

frosty sleet
#

yes

valid rivet
#

very slow ๐Ÿ™‚

frosty sleet
#

@sinful garnet hop in vc with us so you see where they are getting lost too

sinful garnet
#

ok but cant talk its almost 2am

frosty sleet
#

oh

#

eh yeah still come

#

@valid rivet in VC

sinful garnet
#

ah ok

#

CodeWithMe literally such a nice feature, used it almost a year ago for helping with javafx and makes many things easier

sinful garnet
#

a bug in your json library?

#

@frosty sleet

#

oof

#

yeah learning/working with libraries without really knowing the language itself is quite hard

sinful garnet
#

bruh my internet just completely died :/

#

so much stuff I have never worked with lol

#

but how are you going to test if it works now though?

#

@frosty sleet

#

gg

glass sorrel
#

So - what is the pushback buffer thing? Or is it gone now so no point?

#

I was doing socket stuff not too long ago and just did the read into a char until < 0.