我是论坛的新手,可能已将此错误发布 - 请帮助新手。
作为开发Android App项目的一部分,我需要能够从应用程序连接到一个设置位置,该位置具有一个基于微处理器(不可连接)的LAN接口,它将接受十六进制的set命令。响应以十六进制给出。
使用我试图修改的示例套接字程序(参见下面的代码),我可以看到(使用wireshark)传输的字符串是ASCII,而不是十六进制,所以我发送38个字节,而不是18个字节使用0x前缀。
我尝试过使用LONG,但我尝试发送的值太大(请参阅String process ='0x .....'代码中途),我得到'字面值0x ...类型int的。超出范围。
我希望有人指出我正确的方向。
由于
// package bdn;
/* The java.net package contains the basics needed for network operations. */
import java.net.*;
/* The java.io package contains the basics needed for IO operations. */
import java.io.*;
/** The SocketClient class is a simple example of a TCP/IP Socket Client.
* For a detailed explanation of the classes in this project see
* bdn.borland.com/article/0,1410,31995,00.html
*/
public class SocketClient
{
public static void main(String[] args)
{
/** Define a host server */
String host = "192.168.1.199";
/** Define a port */
int port = 3376;
int i = 0;
StringBuffer instr = new StringBuffer();
// String TimeStamp;
System.out.println("SocketClient initialized to " + host + ", port " + port );
try
{
/** Obtain an address object of the server */
InetAddress address = InetAddress.getByName(host);
/** Establish a socket connection */
Socket connection = new Socket(address, port);
/** Instantiate a BufferedOutputStream object */
BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());
/** Instantiate an OutputStreamWriter object with the optional character
* encoding.
*/
OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
// TimeStamp = new java.util.Date().toString();
String process = "0x308101000c00234C51050000000099670007";
System.out.println("Data to be sent is " + process) ;
/** Write across the socket connection and flush the buffer */
osw.write(process);
osw.flush();
System.out.println("Data sent is " + process) ;
/** Instantiate a BufferedInputStream object for reading
* incoming socket streams.
*/
BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
/**Instantiate an InputStreamReader with the optional
* character encoding.
*/
InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");
/**Read the socket's InputStream and append to a StringBuffer */
int c;
for (i = 0; i <= 18; i++ )
{
c = isr.read() ;
instr.append((char) c);
System.out.println("character " + i + " value " + c);
}
/** Close the socket connection. */
connection.close();
System.out.println(instr);
}
catch (IOException f)
{
System.out.println("IOException: " + f);
}
catch (Exception g)
{
System.out.println("Exception: " + g);
}
}
}
答案 0 :(得分:2)
我尝试使用LONG,但我尝试发送的值太大
没错。该值对应
4225287479528688525220725405996728206163975
并且long的最大值是
9223372036854775807
您可以将号码存储在BigInteger
中,如下所示:
String process = "0x308101000c00234C51050000000099670007";
BigInteger bi = new BigInteger(process.substring(2), 16);