首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sockets java -初学者

Sockets java -初学者
EN

Stack Overflow用户
提问于 2015-02-20 05:40:09
回答 1查看 226关注 0票数 2

我从java开始,遇到了套接字的问题,我想让我的服务器遇到并问候一个值,然后想把它转换成一个String,然后包含在一个条件if中。然而,尽管服务器没有问题地批准了文本,但我不能传递String的值。

代码语言:javascript
复制
/*
* 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();
        }
    }
}

客户端代码,将消息发送到套接字127.0.0.1",5000

代码语言:javascript
复制
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.
    }
}

谢谢你的帮助

EN

回答 1

Stack Overflow用户

发布于 2015-02-20 06:01:52

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

所以试试吧:

代码语言:javascript
复制
/*
* 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();
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28617466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档