套接字java - 初学者

时间:2015-02-19 21:40:09

标签: java sockets

我从java开始并且套接字有问题,我希望我的服务器满足并问候一个值,然后想把它变成String,然后包含在条件{{1 }}。但是,尽管服务器批准了文本没有问题,但我无法传递if的值。

String

Cliente代码,将消息发送到套接字127.0.0.1",5000

/*
* 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();
        }
    }
}

谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

每次调用nextLine()方法,服务器都会等待接收一个新的字符串,但在你的客户端的实现中只会发送一个字符串。

所以试试:

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

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()) {

                        String texto = entrada.nextLine();

                        System.out.println(texto);
                        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();
        }
    }
}