Java Sockets - Beginner

Asked

Viewed 295 times

3

I’m starting in java and I have a problem with sockets, I wanted my server to receive a value and then wanted to turn it into a String, in such a way as to be subsequently included under if. However, despite the server receiving the text without problem, I cannot pass the value to a String.

Follow the server code:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javasockets;

import java.io.DataInputStream;
import java.io.IOException;
import static java.lang.System.exit;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 *
 * @author Nuno
 */
public class JavaSockets {

    public static String T = "s";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        try {
            ServerSocket sckServer = new ServerSocket(5000);

            System.out.println("Porta 5000 aberta!");
            Socket sck;

            while (true) {

                sck = sckServer.accept();
                try (Scanner entrada = new Scanner(sck.getInputStream())) {

                    while (entrada.hasNextLine()) {

                        System.out.println(entrada.nextLine());
                    }
                    String texto = entrada.nextLine();
                    System.out.println("ola" + texto);
                    String fnames = texto;
                    System.out.println("ola" + fnames);
                    System.out.println(texto);
                    if (texto.equals(T)) {
                        System.out.println("LOOL");
                    }
                }
                sckServer.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Follow the client code:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javasockets;

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nuno
 */
public class cliente {

    public static void main(String[] args) throws IOException {
        while (true) {
            Socket cliente = new Socket("127.0.0.1", 5000);
            System.out.println("O cliente se conectou ao servidor!");

            Scanner teclado = new Scanner(System.in);
            PrintStream saida = new PrintStream(cliente.getOutputStream());

            while (teclado.hasNextLine()) {
                saida.println(teclado.nextLine());
            }
            saida.flush();
            saida.close();
            teclado.close();
        }

    }

    static Object getInetAddress() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
  • I am without computer at the moment (mobile). Could you format the code part? (Indentation, etc)

  • When trying to save as String some error appears?

  • No, simply String text has no value saved.

1 answer

2

It is not clear when you say you receive the text but cannot pass it to a String. The reception is already a String.

I believe your problem may be on your server at this point:

                while (entrada.hasNextLine()) {

                    System.out.println(entrada.nextLine());
                }
                String texto = entrada.nextLine();

You’re consuming all the input on while and only presented it, and of course while will end when there is no more to be consumed (hasNextLine()), then after the while you just pass nothing to String texto.

Try it like this:

                String texto = "";

                while (entrada.hasNextLine()) {
                    texto += entrada.nextLine() + "\n";
                }

                System.out.println(texto);

You save all content before in a variable and then show. Observe the inclusion of the line escapement \n, that makes the ln of println. Then you can do whatever you want with the texto.

  • Thanks for your reply, effectively the problem is no while.. I put the whole code inside the while and solved...

  • Welcome to Stackoverflowpt. Thanks for helping the community! + 1 for the reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.