gpt4 book ai didi

java - 套接字 java - 初学者

转载 作者:行者123 更新时间:2023-11-30 11:08:15 25 4
gpt4 key购买 nike

我开始使用 java 并且遇到套接字问题,我希望我的服务器会见并问候一个值,然后想将其转换为 String,然后包含在条件 如果。但是,尽管服务器毫无问题地批准了文本,但我无法传递 String 的值。

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

客户端代码,发送消息到socket 127.0.0.1", 5000

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.
}
}

谢谢你的帮助

最佳答案

每次调用 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();
}
}
}

关于java - 套接字 java - 初学者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28617466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com