我知道这个问题已经被问过了,但是给那个人的答案是使用 read(),直到 EOF。我应用了这个答案,我的服务器仍然发送“连接重置”。对于那些不熟悉这个问题的人,我正在尝试将安装在我车上的 GPS 连接到我的服务器。它适用于 SMS 和 GPRS 命令,因此想法是成功连接设备。这是代码:
这是我的客户套接字管理器
package tcp.clientservermodel;
import java.net.*;
import java.io.*;
public class ClientSocketManager extends Thread{
Socket client_socket_;
InputStream input_;
OutputStream output_;
ClientSocketCaller caller_;
public ClientSocketManager( ClientSocketCaller caller,Socket incoming_socket){
this.caller_=caller;
this.client_socket_=incoming_socket;
this.start();
}
public void run(){
try{
input_=this.client_socket_.getInputStream();
output_=this.client_socket_.getOutputStream();
int incoming_char=0;
StringBuffer incoming_message=new StringBuffer();
while((incoming_char=input_.read())!=-1){
// System.out.println(incoming_message.toString());
if(incoming_char=='\n'){ ProcessMessage(this. client_socket_.getInetAddress().toString()+": "+incoming_message.toString());
incoming_message=new StringBuffer();
}else{
incoming_message.append((char)incoming_char);
}
}
}catch(Exception err){
}
}
public boolean SendMessage(String message){
boolean success=true;
try{
this.output_.write(("123456G").getBytes());
}catch(Exception err){
success=false;
}
return success;
}
public void ProcessMessage(String message){
System.out.println(message);
caller_.MessageHasBeenRecieved(message);
}
}
服务器套接字管理器:
package tcp.clientservermodel;
import java.net.*;
import java.io.*;
import java.util.*;
public class ServerSocketManager extends Thread
implements ClientSocketCaller{
int port_;
ServerSocket server_socket_;
public boolean is_enabled_=true;
Vector client_sockets_vector_=new Vector();
public ServerSocketManager(int port){
this.port_=port;
this.start();
}
public void run(){
try{
this.server_socket_=new ServerSocket(port_);
while(is_enabled_){
Socket client_socket=this.server_socket_.accept();
System.out.println("Connection Succesful "+client_socket.getInetAddress().toString());
this.client_sockets_vector_.add(new ClientSocketManager(this,client_socket));
}
}catch(Exception err){
System.out.println(err +" 3");
}
}
public void MessageHasBeenRecieved(String message) {
for(int i=0;i<this.client_sockets_vector_.size();i++){
ClientSocketManager current=(ClientSocketManager)this.client_sockets_vector_.get(i);
if(current!=null){
current.SendMessage(message);
}
}
}
}
这是客户端套接字调用程序
package tcp.clientservermodel;
public interface ClientSocketCaller {
public void MessageHasBeenRecieved(String message);
}
错误位于此行
(incoming_char=input_.read())!=-1
输出是
java.net.SocketException 连接重置
我需要帮助!