Question
Java program help... Let's say a client sends a JSON which is converted to a string to send to a server. My problem here is
Java program help...
Let's say a client sends a JSON which is converted to a string to send to a server. My problem here is how can I take in the String that the client sends and convert it to JSON on the server-side(I'm not using the GSON library so it's not that simple I presume).
please help me out with the server-side code
Please take a look at my server-side code and provide valueable feedback instead of just posting a code.
Thank you very much.
snippet of the Client-side that creates the JSON to send to serverside:
JSONObject c = new JSONObject(); String i = "instruction " + count; if (wordArr[0].equals("Add") || wordArr[0].equals("add")) { try { c.put(i,statement); System.out.println(c.toString()); outStream.writeBytes(c.toString()+" "); String inputStream = inStream.readLine(); System.out.println(inputStream);
Server-side Code:
import java.io.*; import java.net.*; import java.util.ArrayList; import java.util.Scanner; import org.json.*;
public class HussainServer {
static ArrayList inputList = new ArrayList<>(); public static void main(String[] args) { // TODO Auto-generated method stub try { Scanner input = new Scanner(System.in); System.out.println("Started..."); ServerSocket serv = new ServerSocket(7777); Socket reqClient = serv.accept(); System.out.println("Recieved"); BufferedReader br = new BufferedReader(new InputStreamReader(reqClient.getInputStream())); PrintStream ps = new PrintStream(reqClient.getOutputStream()); String inputData = ""; while (!(inputData = br.readLine()).equals("exit")) { //JSONObject obj = new JSONObject(); stuck // obj. System.out.println("received a message from client: " + inputData); //print the incoming data from the client
ps.println("Here is an acknowledgement from the server"); //respond back to the client } serv.close(); br.close(); ps.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started