gpt4 book ai didi

java - 通过套接字发送字符串。服务器没有收到

转载 作者:行者123 更新时间:2023-12-01 21:52:14 25 4
gpt4 key购买 nike

我最近一直在努力寻找一种通过套接字文件传递字符串的方法。我计划创建一个远程工具(客户端)来根据收到的消息(服务器)执行操作。我在谷歌上搜索了我的问题的答案,我发现了一些东西并设法理解了一些东西,但我也遇到了一些问题(我是编程新手,还没有上大学)。

如果您能在此事上得到任何帮助,我将不胜感激

SocketService.java ---- 类文件=服务器端

 package socket;

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;

public class ServiceSocket {
static ServerSocket myService;
static Socket thesocket;
static Thread socketThread;
public static boolean socketRunning;
public static DataInputStream socketMessage;


public static void initialise(String localhost, int portNumber ){
// make a server socket//////
try {
myService = new ServerSocket(portNumber);
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
//////////////////////////////

}

public static void deploySocket(){
socketThread = new Thread() {
public void run(){

// making connection
System.out.println("VVaiting for connection...");
try {
thesocket = myService.accept();
System.out.println("Connection made");
socketRunning = true;
} catch (IOException e) {
e.printStackTrace();
}
////////////////////////////////////
try {
StartBrain();
} catch (IOException e1) {
e1.printStackTrace();
}

if(socketRunning = false) {
try {
thesocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

};
socketThread.start();
}

public static String getSocketMessage() throws IOException {

try {
socketMessage = new DataInputStream(thesocket.getInputStream());
} catch (IOException e1) {
e1.printStackTrace();
}
boolean looprunning = true;
String message = null;
System.out.println("entering loop");
do {
try {
while (socketMessage.readUTF() != null) {

message = socketMessage.readUTF();
looprunning = false;
}
} catch (EOFException e) {

}
}while(looprunning);

System.out.println("Message received from UTF: " + message);
System.out.println("loop exited vvith message");

if(message == null) {
message = "no message";
}

return message;
}

public static void StartBrain() throws IOException {
System.out.println("socket brain started");
String BrainMessage = getSocketMessage();
if(BrainMessage == "command") {
System.out.println("Command EXECUTED HAHA");
} else if(BrainMessage == "taskschedule") {
System.out.println("task scheduled");
} else {
System.out.println("no command received");
}

}

Main.java ----- 类文件 = 服务器端 包主;

import socket.ServiceSocket;

public class Main {

public static void main(String[] args) {

ServiceSocket.initialise("localhost", 3535);
ServiceSocket.deploySocket();
}
}
}

Main.java = 客户端

package mainPackage;

import java.io.*;
import java.net.*;
import java.util.concurrent.TimeUnit;

public class Main {
private static Socket clientSocket;

public static void sendMessage(String message) throws IOException, InterruptedException {
DataOutputStream dOut = new DataOutputStream(Main.clientSocket.getOutputStream());

dOut.writeUTF(message);
dOut.flush();

dOut.close();
}

public static void main(String[] args) throws Exception {
// String modifiedSentence;

clientSocket = new Socket("localhost", 3535);
System.out.println("Initializing");
sendMessage("command");

boolean running = true;
while(running) {
TimeUnit.SECONDS.sleep(3);
sendMessage("taskschedule");
}
clientSocket.close();

}
}

主要问题

 do {
try {
while (socketMessage.readUTF() != null) {

message = socketMessage.readUTF();
looprunning = false;
}
} catch (EOFException e) {

}
}while(looprunning);

它不读取字符串/UTF

最佳答案

它确实读取了它,在这里:

while (socketMessage.readUTF() != null) {

然后将其丢弃,因为您没有将返回值分配给变量,然后尝试读取另一个变量,如下所示:

message = socketMessage.readUTF();

但是您发送的一条(第一条)消息已经消失了。

关于java - 通过套接字发送字符串。服务器没有收到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35022937/

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