Jump to content

Hello, I've developed a simple client/server app for my homework, the client makes an XML file out of data you input, sends it to the server, the server takes the XML, transform it in an object array and sends back some data, thing is, even if I pw.println() and pw.flush, the client doesn't recieve anything, and even stuff that worked before, doesn't work anymore if I put the br.readLine() in the client.

CLIENT

package clientclasse;


import java.util.Scanner;
import java.net.*;
import java.io.*;
import javax.xml.bind.*;

public class ClientClasse {

    /**
     * @param args the command line arguments
     */
     static Socket S;
     static   InputStream is;
     static   InputStreamReader isr;
     static  BufferedReader br;
     static   OutputStream os;
     static PrintWriter pw;

    public static void main(String[] args) throws IOException, JAXBException {

        Scanner Scan = new Scanner(System.in);



        S = new Socket("localhost", 4000);
        is = S.getInputStream();
        isr = new InputStreamReader(is);
        br = new BufferedReader(isr);

        os = S.getOutputStream();
        pw  = new PrintWriter(os);

        Classe C = new Classe();

        Studente[] Stud = new Studente[3];
        for (int i = 0; i < 3; i++) {
        	System.out.println("Inserire i dati ");
            String cognome = Scan.next();
            String nome = Scan.next();
            int eta= Scan.nextInt();
            Stud[i] = new Studente(cognome,nome,eta);


        }
        C.setStudenti(Stud);
        converti(C);
		//String etaMax = br.readLine();
		//System.out.println("l'eta massima e': " + etaMax);

      // System.out.println("l'eta massima e':" + br.readLine());
      System.out.println(br.read());
          S.close();
       is.close();
       isr.close();
       br.close();
       os.close();
       pw.close();



    }
     static void converti(Classe C) throws JAXBException
    {
        JAXBContext context = JAXBContext.newInstance(Classe.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        m.marshal(C, pw );
    }

}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package serverclasse;

/**
 *
 * @author Anis
 */
import java.io.*;
import java.net.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.*;
@XmlRootElement(namespace = "hertz.javaxmlparser.jaxb")
public class ServerClasse {

    /**
     * @param args the command line arguments
     */
  static  ServerSocket SS;
  static     Socket S;
  static     InputStream is;
  static     InputStreamReader isr;
  static     BufferedReader br;
  static     OutputStream os;
  static     PrintWriter pw;
    public static void main(String[] args) throws IOException, JAXBException {


       SS = new ServerSocket(4000);
       while(true){
       S = SS.accept();
       is = S.getInputStream();
       isr = new InputStreamReader(is);
       br = new BufferedReader(isr);
       os = S.getOutputStream();
       pw = new PrintWriter(os);

        Classe C = riconverti();

       System.out.println(C.getStudenti()[0].getCognome());


       int etaMax = 0;

       for(int i = 0; i<3; i++)
       {
       	if(C.getStudenti()[i].getEta()>etaMax)
       	{
       		etaMax = C.getStudenti()[i].getEta();
       	}
       }

       
       System.out.println(etaMax);
       pw.println(""+etaMax);
       pw.flush();

       S.close();
       is.close();
       isr.close();
       br.close();
       os.close();
       pw.close();


               }
    }

    static Classe riconverti() throws JAXBException
    {
        JAXBContext context = JAXBContext.newInstance(Classe.class);
        Unmarshaller unm = context.createUnmarshaller();
        Classe C = (Classe) unm.unmarshal(is);
        return C;
    }

}

SERVER

 

Thanks

 

Spoiler

|| Asrock Z68 Extreme 3 Gen 3 || i5 3570 @3.5GHz || Zalman CNPS10X Optima || 8GB RAM HyperX Fury Blue @ 1600MHz || Thermaltake Berlin 630W || Zalman Z11 || Gainward Phantom GTX 970 || 120GB Kingston V300  (Gift) + 1TB  WD Green

 

Phone: Xiaomi Redmi Note 8

Tablet: iPad Mini 2

Link to comment
https://linustechtips.com/topic/1149473-client-doesnt-recieve-java/
Share on other sites

Link to post
Share on other sites

Try using an application layer protocol like http perhaps? Few people program raw sockets these days. It is much easier to use an application protocol like http, implement a rest api, and then debug using postman or a web browser.

Sudo make me a sandwich 

Link to post
Share on other sites

Thanks, but it's for my homework, so I have to use this technique

 

Spoiler

|| Asrock Z68 Extreme 3 Gen 3 || i5 3570 @3.5GHz || Zalman CNPS10X Optima || 8GB RAM HyperX Fury Blue @ 1600MHz || Thermaltake Berlin 630W || Zalman Z11 || Gainward Phantom GTX 970 || 120GB Kingston V300  (Gift) + 1TB  WD Green

 

Phone: Xiaomi Redmi Note 8

Tablet: iPad Mini 2

Link to post
Share on other sites

I would debug this step by step and function by function.

 

First check that your xml parser works on both the server and client. Make sure your client creates the xml file properly. 

 

2nd check that the server receive the data. Use filestream to recreate that xml file from the server application to test if correct data is receive. 

 

3rd check, check that the server builds the array properly. To send back the array, you may want to send it in a json. Use a library like the json builder or just a long string with comma as the delimiter. 

 

 

Sudo make me a sandwich 

Link to post
Share on other sites

Already did all that, the problem is the sending/recieving at the end, even if I send a messagge saying ("aaa") it would still not work.

 

Spoiler

|| Asrock Z68 Extreme 3 Gen 3 || i5 3570 @3.5GHz || Zalman CNPS10X Optima || 8GB RAM HyperX Fury Blue @ 1600MHz || Thermaltake Berlin 630W || Zalman Z11 || Gainward Phantom GTX 970 || 120GB Kingston V300  (Gift) + 1TB  WD Green

 

Phone: Xiaomi Redmi Note 8

Tablet: iPad Mini 2

Link to post
Share on other sites

package clientclasse;
import javax.xml.bind.annotation.*;
@XmlRootElement(namespace = "hertz.javaxmlparser.jaxb")

public class Classe {
	@XmlElementWrapper(name = "classe")
@XmlElement(name = "studente")

    Studente[] Studenti;

    public Classe()
    {


    }


    public Classe(Studente[] Studenti) {
        this.Studenti = Studenti;
    }

    public Studente[] getStudenti() {
        return Studenti;
    }

    public void setStudenti(Studente[] Studenti) {
        this.Studenti = Studenti;
    }


}

CLASSE

package clientclasse;

import javax.xml.bind.annotation.*;

@XmlRootElement(name = "studente")
@XmlType(propOrder =
        {
            "nome",
            "cognome",
            "eta"
        })

public class Studente {
String nome;
String cognome;
int eta;

public Studente()
{

}

    public Studente(String nome, String cognome, int eta) {
        this.nome = nome;
        this.cognome = cognome;
        this.eta = eta;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getCognome() {
        return cognome;
    }

    public void setCognome(String cognome) {
        this.cognome = cognome;
    }

    public int getEta() {
        return eta;
    }

    public void setEta (int eta) {
        this.eta = eta;
    }


}

STUDENTE

 

Spoiler

|| Asrock Z68 Extreme 3 Gen 3 || i5 3570 @3.5GHz || Zalman CNPS10X Optima || 8GB RAM HyperX Fury Blue @ 1600MHz || Thermaltake Berlin 630W || Zalman Z11 || Gainward Phantom GTX 970 || 120GB Kingston V300  (Gift) + 1TB  WD Green

 

Phone: Xiaomi Redmi Note 8

Tablet: iPad Mini 2

Link to post
Share on other sites

I removed all this User-Input-Via Console just to debug easier, and i added toString Methods to Classe and Studente just to check if everything transmitted as wished

this worked for me

 

**ClientClasse**

Spoiler

public class ClientClasse {
    public static void main(String[] args) throws Exception {
        final int PORT = 4000;

        Classe classe = new Classe(new Studente[]{
                new Studente("test1", "test11", 0),
                new Studente("test2", "test22", 1),
                new Studente("test3", "test33", 1)
        });

        Socket socket = new Socket("localhost", PORT);

        converti(classe, socket);

        socket.close();
    }

    static void converti(Classe classe, Socket socket) throws JAXBException, XMLStreamException, ParserConfigurationException, IOException {
        JAXBContext context = JAXBContext.newInstance(Classe.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);


        OutputStream outputStream = socket.getOutputStream();
        PrintWriter printWriter = new PrintWriter(outputStream);

        m.marshal(classe, printWriter);

        printWriter.close();
        outputStream.close();
    }
}

 

 

**ServerClasse**

Spoiler

@XmlRootElement(namespace = "hertz.javaxmlparser.jaxb")
public class ServerClasse {
    public static void main(String[] args) throws IOException, JAXBException {
        final int PORT = 4000;

        ServerSocket serverSocket = new ServerSocket(4000);

        while(true){
            Socket socket = serverSocket.accept();
            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());

            Classe classe = riconverti(socket.getInputStream());
            System.out.println(classe.toString());
        }
    }

    static Classe riconverti(InputStream inputStream) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(Classe.class);
        Unmarshaller unm = context.createUnmarshaller();
        Classe classe = (Classe) unm.unmarshal(inputStream);
        return classe;
    }

}

 

 

 

You should add Exception Handling Currently all Exceptions are just thrown all the way up till the Programm Crashes

 

And another hint would be to maybe name variables with a usefull name, instead of os use outputStream for example

most ide's autogenerated that stuff => live template

image.png.1b370d0ab98e4043bf71d05bdb25aca4.png

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×