2012-07-27 71 views
0

我知道這個問題已經被問到,但給予該人的答案是使用read(),直到EOF。我應用了這個答案,我的服務器仍然發送「連接重置」。對於那些不熟悉問題的人,我試圖將安裝在我車上的GPS連接到我的服務器。它可以與SMS和GPRS命令一起工作,所以想法是成功連接設備。下面的代碼:Java.net.SocketException連接重置GPS

這裏是我的客戶端套接字MANAGER

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); 
      } 
     } 
    } 

} 

這裏的是客戶端SOCKET來電

package tcp.clientservermodel; 


public interface ClientSocketCaller { 

    public void MessageHasBeenRecieved(String message); 

} 

錯誤就坐落於此line

(incoming_char = input_.read())!= - 1

和輸出是

java.net.SocketException異常連接重置

請我需要幫助!

回答

0

我解決了這個問題......我使用Wireshark來查看服務器和GPS之間的包。所以我注意到Wireshark中「win」參數的大小在兩個流之間是不同的。從GPS =>贏= 10880,從服務器=>贏= 8019.我只是改變服務器的勝利元素,並工作!

相關問題