Java 如何持续与tcp服务器通信?
我有一个TCP客户端,它目前正在作为终端工作。所以我可以手动向服务器发送消息。但这只是为了测试 但我需要与tcp服务器进行连续通信。我有不同的片段来显示不同的数据。我要发送消息,这取决于片段。属于片段的消息总是相同的 例如: 如果片段1在GUI上处于活动状态:Message1、Answer1、M2、A2、M3、A3、M1、A1、M2、A2 如果片段2在GUI上处于活动状态:M5、A5、M6、A6、M7、A7、M5、A5 所以消息总是重复,服务器的答案将显示在GUI上 我该怎么做 TCP客户端的代码:Java 如何持续与tcp服务器通信?,java,android,sockets,tcp,Java,Android,Sockets,Tcp,我有一个TCP客户端,它目前正在作为终端工作。所以我可以手动向服务器发送消息。但这只是为了测试 但我需要与tcp服务器进行连续通信。我有不同的片段来显示不同的数据。我要发送消息,这取决于片段。属于片段的消息总是相同的 例如: 如果片段1在GUI上处于活动状态:Message1、Answer1、M2、A2、M3、A3、M1、A1、M2、A2 如果片段2在GUI上处于活动状态:M5、A5、M6、A6、M7、A7、M5、A5 所以消息总是重复,服务器的答案将显示在GUI上 我该怎么做 TCP客户端的代
public class Client {
public static String ServerIP = "192.168.0.10";
public static int ServerPort = 35000;
// message to send to the server
private String mServerMessage;
// sends message received notifications
private OnMessageReceived mMessageListener = null;
// while this is true, the server will continue running
private boolean mRun = false;
// used to send messages
private OutputStream mBufferOut;
// used to read messages from the server
private BufferedReader mBufferIn;
/**
* Constructor of the class. OnMessagedReceived listens for the messages received from server
*/
public Client(OnMessageReceived listener) {
mMessageListener = listener;
}
/**
* Sends the message entered by client to the server
*
* @param message text entered by client
*/
public void sendMessage(String message) {
if (mBufferOut != null ) {
try {
mBufferOut.write((message + "\r\n").getBytes());
mBufferOut.flush();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* Close the connection and release the members
*/
public void stopClient() {
Log.i("Debug", "stopClient");
// send mesage that we are closing the connection
//sendMessage(Constants.CLOSED_CONNECTION + "Kazy");
mRun = false;
if (mBufferOut != null) {
try {
mBufferOut.flush();
mBufferOut.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
mMessageListener = null;
mBufferIn = null;
mBufferOut = null;
mServerMessage = null;
Log.i("Debug", "disconnected");
}
public void run() {
mRun = true;
try {
//here you must put your computer's IP address.
InetAddress serverAddr = InetAddress.getByName(ServerIP);
Log.e("TCP Client", "C: Connecting...");
//create a socket to make the connection with the server
Socket socket = new Socket(serverAddr, ServerPort);
try {
Log.i("Debug", "inside try catch");
//sends the message to the server
mBufferOut = socket.getOutputStream();
//receives the message which the server sends back
mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
MainActivity.ColorBoxHandler.sendEmptyMessage(0);
while (mRun) {
mServerMessage = mBufferIn.readLine();
if (mServerMessage != null && mMessageListener != null) {
//call the method messageReceived from MyActivity class
mMessageListener.messageReceived(mServerMessage);
}
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + mServerMessage + "'");
} catch (Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
//the socket must be closed. It is not possible to reconnect to this socket
// after it is closed, which means a new socket instance has to be created.
socket.close();
MainActivity.ColorBoxHandler.sendEmptyMessage(1);
}
} catch (Exception e) {
Log.e("TCP", "C: Error", e);
}
}
//Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
//class at on asynckTask doInBackground
public interface OnMessageReceived {
public void messageReceived(String message);
}
}
你能先解释一下你现在有哪些问题吗?为什么此代码不适用于您?
需要与tcp服务器持续通信。
。我不明白你的意思。你是说永久连接吗?这只是一个普通的TCP客户端。我正在为OBD2汽车诊断读卡器编程。我的应用程序将连接到车载的OBD2加密狗。这意味着我没有手动发送消息。我需要一种根据活动片段自动连续发送消息的方法。片段显示不同的数据,例如发动机转速或燃油率。。。根据片段的不同,还有另一条信息“例行程序”,您没有回答任何问题。只是重复你自己。你什么都没弄清楚。你也没有弄清楚手动或自动发送会有什么关联或区别。我想一次又一次地循环发送消息