#Json and sockets
1 messages ยท Page 1 of 1 (latest)
<@&987246487241105418> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
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.
did you try googling yourself?
first thing hat come up when looking it up online:
https://stackoverflow.com/questions/21953958/how-to-send-json-object-through-java-sockets
but not sure if thats what you want
Im not sure
someone was helping me but I had to go and I dont think he wants to help me anymore
An error has occurred while trying to communicate with ChatGPT. Please try again later
what do you really want to do?
Im trying to create server/client for my GUI design
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
yeah we were working on it together
the server is finished but I dont know how to do the client
yeah true
maybe want to send the current codebase related to this
and what you want to do next
I uploaded your attachments as Gist.
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
same actually
also you are using @emccue's JSON library
and I have no idea how to use it
yeah he was helping me but I had to go
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?
whats the problem you're having with the client? is it not connecting? is it connecting but data isnt sending?
its okay Im just going to go back to regular coding without using json
its too confusing for me
but you want to improve right?
why not taking that step and creating your socket connection using json
ah ok
are there any requirements for this application or can you just implement your own ideas using the time you have
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<>
sure. give me a few minutes
Im just a slow learner
yes
very slow ๐
@sinful garnet hop in vc with us so you see where they are getting lost too
ok but cant talk its almost 2am
ah ok
CodeWithMe literally such a nice feature, used it almost a year ago for helping with javafx and makes many things easier

a bug in your json library?
@frosty sleet
oof
yeah learning/working with libraries without really knowing the language itself is quite hard