Using Winsock "send" function in Java (or similar)
I have a feeling my problem is a bit strange, but here goes...
I have a C++ program (from another organization) that interacts with a camera connected to hardware via Ethernet using sockets. On the surface, the initial function that sends a message to the socket is this:
char* cmd = "#TRGON\x0d";
m_pClient->Socket_Write( (BYTE*)cmd, strlen( cmd ));
After looking digging thro开发者_JAVA百科ugh the code, I found the function at the lowest level that actually sends the data:
// Try to send what's in the buffer
int nFlags = 0;
int nSent = ::send( m_hSocket, (char*)m_pSendBuffer, m_nSendBufLen, nFlags );
...checking for errors, etc.
Now, the program I'm working on needs to send a similar command to the camera, except it's written in Java. I've been playing with Java sockets all day and haven't been able to get the camera to do much of anything other than connect to the appropriate address (the command there is to trigger the LED lights on).
I was looking around online and saw that this "send" command is from the Winsock library which the C++ program encapsulates. Is there any similar way to do this in Java? Or am I just not using the right combination of DataOutputStreams, sockets, etc. in Java? Is there some "import winsock" I'm missing (Googling seems to say there isn't such a thing)?
Thanks very much for any help.
EDIT:
I don't really know if any code will help! At this point I'm just doing basic things: connecting and trying to write to it. I've seemingly tried every combination, conversion, etc. This is just some test code (obviously not robust or proper, like the catch block):
InetAddress addr = null;
Socket sock = null;
ObjectInputStream in = null;
ObjectOutputStream out = null;
String msg = "#TRGON\\x0d";
try {
addr = InetAddress.getByAddress(new byte[] { (byte) 192, (byte) 168,
(byte) 81, (byte) 58 });
sock = new Socket(addr, PORT);
out = new ObjectOutputStream(sock.getOutputStream());
out.writeBytes(msg);
in = new ObjectInputStream(sock.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
It's hanging at the last line there, the in = new ... as if it's not picking up any type of input stream from the camera. I know I can continue playing around with it, but any outside advice is appreciated.
In general, it should be as easy as:
Socket s = new Socket("hostname", port);
OutputStream os = s.getOutputStream();
os.write("#TRGON\r".getBytes());
os.flush();
So what exactly are your problems? Of course, if the connection looks character-based, you should not use a DataOutputStream
but instead write simple ASCII-Strings to it.
[edit: replaced the \x0d
with \r
]
Looks like you C++ accepts plain bytes. So you should do the same with Java:
InetAddress addr = InetAddress.getByName("192.168.81.58");
Socket s = new Socket(addr, PORT);
OutputStream out = s.getOutputStream();
out.write("#TRGON\\x0d".getBytes());
s.close();
That's as raw as it gets I suppose.
Turns out the command String being sent needed a different format. Instead of
"#TRGON\x0d"
I needed to use
"#TRGON\r"
Didn't need the hex specifier thing. Thanks for the replies!
精彩评论