建议和帮助解决蜂窝调制解调器项目的客户端/服务器交互问题

时间:2013-02-16 22:46:50

标签: java sockets client socketserver

我需要一些建议并帮助解决我正在进行的蜂窝调制解调器项目的客户端/服务器交互。客户端和服务器是用Java编写的。

我需要一些建议的问题如下:

(1)我正在寻求关于我正在采用的方法是否会扩展大型文件(代码要遵循)的建议,特别是在网络可能意外丢失的蜂窝网络环境中。大文件大约1GB。大文件表示从服务器传输到客户端的映像。这代表了最糟糕的情况。基本案例包含相对较小的文件,包含GPS数据和时间戳信息。这些文件很可能在KB到几MB范围内并经常传输。

(2)需要有关对客户端/服务器代码进行故障排除的建议。即使这种方法不适用于较大的文件,我也希望在不久的将来为基本案例提供代码以支持概念验证测试。概念证明不需要软件更新。

客户端/服务器交互的背景知识。客户端联系服务器。服务器检测启动新线程以处理客户端的客户端请求。客户端发送串行数据包。数据包包含一些标头信息(文件大小,crc,文件类型)和数据有效负载。在接收到数据包对象时,服务器验证crc和文件大小是否与标头中的值包括匹配。服务器以数据包对象响应,该对象指示传输是否有效。如果客户端从服务器收到有效响应,则客户端发送一个良好的再见数据包以关闭会话。如果服务器的响应无效,客户端将重新发送数据,并在x尝试失败后最终退出。最终,客户端将从服务器获取一组指令,指示它上传日志文件,下载新的.jre文件,甚至是新的固件映像。

以下是我在服务器代码上遇到的错误:

Feb 16, 2013 7:36:40 AM noaa.logbook.server.ServerConnectionHandler run
SEVERE: null
java.io.EOFException
at
java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2553)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1296)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
    at noaa.logbook.server.ServerConnectionHandler.run(ServerConnectionHandler.java:69)
    at java.lang.Thread.run(Thread.java:662)

数据作为服务器到达,下次我尝试读取objectinputstream上的一个对象时出现异常,这是再见包。

客户代码:

public static boolean clientTransmit(Socket sockToServer, String fileName, int dataPacketType, String id, String fileTimeStamp)    {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
//TCPConnection tcpConn = null;
DataPacket packet = null;

File inputFile = new File(fileName);
boolean fileExists = inputFile.exists();

int size = 0;
int failedAttempts = 0;
String contents = null;
byte[] ref = null;
boolean success = false;
boolean bye = false;
try
{
  sockToServer.setSoTimeout(5000);

  if ((sockToServer.isConnected()) && (fileExists)) {
    System.out.println("LogBookClientCommunications: Connected to Server");
    System.out.print("Stage 0");
    contents = readFile(fileName);

    packet = LogBookUtilities.packageDataPacket(DataPacket.UPLOAD_DATA, contents,    LogBookClient.SOFTWARE_VERSION, LogBookClient.serialNumber);

    oos = new ObjectOutputStream(sockToServer.getOutputStream());
    oos.writeObject(packet);
    oos.flush();
    System.out.println("LogBookClientCommunications: Sending DataPacket");

    ois = new ObjectInputStream(sockToServer.getInputStream());

    while(!success && failedAttempts < 3) {
      Object object = ois.readObject();
      if (object instanceof DataPacket) {
        System.out.println("LogBookClientCommunications: Received a DataPacket Object");
        DataPacket inPacket = (DataPacket)object;
        byte[] compressedByteRef = inPacket.getDataArray();
        boolean sizeValid = verifySize(compressedByteRef, inPacket.getLength());
        boolean crcValid = verifyCRC(inPacket);
        if ((sizeValid) && (crcValid)) {
            System.out.println("LogBookClientCommunications: Size & CRC Valid");
            String uncompressed = new String(uncompress(compressedByteRef));
            String[] strRef = lookupResponsePairs(dataPacketType);

            if (uncompressed.equals(strRef[0])) {
                success = true;
                System.out.println("LogBookClientCommunications: File arrived uncorrupted");
                //tcpConn.disconnect();
            } else if (uncompressed.equals(strRef[1])) {
                success = false;
                failedAttempts++;
                System.out.println("LogBookClientCommunications: File arrived corrupted");
            }
        } else {
            success = false;
            failedAttempts++;
            if (sizeValid)
                System.out.println("LogBookClientCommunications: CRC InValid");
            else
                System.out.println("LogBookClientCommunications: Size InValid");
        }
      }//end if object instanceof
      else {
        System.out.println("LogBookClientCommunications: Not a DataPacket Object");
        failedAttempts++;
      }
    }//while
    //Close Connection by sending bye
    System.out.println("LogBookClientCommunications: Sending Good Bye...");
    DataPacket goodbye  = LogBookUtilities.packageDataPacket(DataPacket.RESPONSE, quit", LogBookClient.SOFTWARE_VERSION, LogBookClient.serialNumber);
    oos.writeObject(goodbye);
    oos.flush();
  }
  else
  {
    System.out.println("LogBookClientCommunications: Failed to Connect or File Did Not Exist");
    success = false;
  }
}
catch (ClassNotFoundException ex) {
  Logger.getLogger(LogBookUtilities.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
  Logger.getLogger(LogBookUtilities.class.getName()).log(Level.SEVERE, null, ex);
} finally {
  try {
    oos.close();
    ois.close();
    sockToServer.close();
  } catch (IOException ex) {
    Logger.getLogger(LogBookUtilities.class.getName()).log(Level.SEVERE, null, ex);
  }
}
return success;
}

服务器连接处理程序代码:

public void run()
{
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
int failedAttempts = 0;
boolean success = false;
boolean sendResponse = false;
Socket soc = getSocket();
Object obj = new Object();
long time = System.currentTimeMillis();

DataPacket inPacket = null;
DataPacket outPacket = null;
try {
  System.out.println("Server Connection Handler: Receiving Connection From - " + soc.getRemoteSocketAddress());
  soc.setSoTimeout(15000);
  oos = new ObjectOutputStream(soc.getOutputStream());
  oos.flush();

  ois = new ObjectInputStream(soc.getInputStream());

  if (ois == null | oos == null) {
      System.out.println("Server Connection Handler: Successfull Opened Streams");
      if (ois == null) { System.out.println("Server Connection Handler: ObjectInputStream Failed to Open");}
      else {System.out.println("Server Connection Handler: ObjectOutputStream Failed to Open"); }
  }
  while (true) {
      inPacket = (DataPacket)ois.readObject();
      boolean validPacket = LogBookUtilities.isPacketValid(inPacket);
      if (validPacket) {
          if(inPacket.getField() == DataPacket.RESPONSE) {
              byte[] ref = inPacket.getDataArray();
              String data = LogBookUtilities.uncompress(ref);
              if (data.equalsIgnoreCase("bye")) {
                  System.out.println("Server Connection Handler: Bye....");
                  break;
              }
          }
          else if (inPacket.getField() == DataPacket.UPLOAD_DATA) {
              System.out.println("Server Connection Handler: Writing data to file");
              LogBookUtilities.processClientPacket(inPacket);
              System.out.println("Server Connection Handler: File Successfully Transfered");
              outPacket = LogBookUtilities.makeResponse(inPacket.getField(), true, LogBookServer.SOFTWARE_VERSION, LogBookServer.ID);
              sendResponse = true;
          }
      } 
      else {
          if (inPacket.getField() == DataPacket.UPLOAD_DATA) {
                sendResponse = true;
                outPacket = LogBookUtilities.makeResponse(inPacket.getField(), true, LogBookServer.SOFTWARE_VERSION, LogBookServer.ID);
          }
      }

      if (sendResponse) {
        oos.writeObject(outPacket);
        oos.flush();
      }

  }//end while


    }
    catch (ClassNotFoundException ex) {
      Logger.getLogger(ServerConnectionHandler.class.getName()).log(Level.SEVERE, null,     ex);
    }
    catch (IOException ex) {
      Logger.getLogger(ServerConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    finally {
      try {
        ois.close();
        oos.close();
        soc.close();
      }
      catch (IOException ex) {
        Logger.getLogger(ServerConnectionHandler.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    }

服务器代码:

public class LogBookServer
{
public static final String ID = "666666";
public static final String SOFTWARE_VERSION = "0.02";
public static final String VFILE = "vFile";
public static final String DASH = "-";
public static final String DATEXT = ".dat";
public static final int FAILED_THRESHOLD = 3;
private int port = 6767;
private String ip = "";

public int getListeningPort() {
  return this.port;
}

public void setListeningPort(int port) {
  this.port = port;
}

public void run()
  throws Exception
{
Selector acceptSelector = SelectorProvider.provider().openSelector();

ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);

InetAddress lh = InetAddress.getLocalHost();
InetSocketAddress isa = new InetSocketAddress(lh, this.port);
ssc.socket().bind(isa);

SelectionKey acceptKey = ssc.register(acceptSelector, 16);

int keysAdded = 0;

while ((keysAdded = acceptSelector.select()) > 0)
{
  Set readyKeys = acceptSelector.selectedKeys();
  Iterator i = readyKeys.iterator();

  while (i.hasNext()) {
    SelectionKey sk = (SelectionKey)i.next();
    i.remove();

    ServerSocketChannel nextReady = (ServerSocketChannel)sk.channel();

    Socket s = nextReady.accept().socket();

    handleConnection(s);
  }
 }
}

void handleConnection(Socket socket)
{
  System.out.println("hadling connection....");
  ServerConnectionHandler connectionHandler = new ServerConnectionHandler(socket);
  new Thread(connectionHandler).start();
}
}

1 个答案:

答案 0 :(得分:0)

在您的客户端中(为了便于阅读,分割为差异线):

DataPacket goodbye  = 
    LogBookUtilities.packageDataPacket(DataPacket.RESPONSE, 
                                       "quit", 
                                       LogBookClient.SOFTWARE_VERSION,
                                       LogBookClient.serialNumber);

然后在你的服务器中你有:

if (data.equalsIgnoreCase("bye")) {

其中哪一个与另一个不一样? ;)

您的服务器读取“再见”数据包,但无法识别它,然后再次循环并尝试从已关闭的套接字读取。 Presto,IOException

至于你的“可扩展性”问题......效率并不高。如果您担心网络从您身下辍学,那么发送序列化对象可能不是可行的方法;没有办法恢复 - 部分发送必须完全重新发送,如果它是一个数据,你说...这是不好的。最好使用write() OutputStream方法,并使用合理的缓冲区大小。这将允许您跟踪已发送的数据并在网络恢复后恢复传输(这显然需要在客户端和服务器之间实现一些逻辑,以便您可以确定服务器已经收到的内容在网络故障的情况下)。