ネットワーク上の JPOS アプリケーションからの接続を送受信し、それらをネットワーク上のプリンターに中継する中継サーバーとして機能するプリンター サーバーをコーディングしようとしています。
私は EPSON レシート プリンターを持っているので、EPSON JPOS ツールを使用してテストしています。
Javaを使用すると、現在UDP接続を適切に送受信できますが、TCP接続の場合、TCPサーバーから何も読み取ることができません。JPOSアプリケーションで「ポートはすでに開いています」というエラーがほぼ即座に発生し、サーバーとの接続を閉じているように見えるためです。
以下はTCPサーバーソケットのコードです:-
public class TcpListener {
private int sendReceiveBufferSize = 256;
public void listen(int port, int bufferSize) throws Exception {
System.out.println("-- Running TCP Server at " + InetAddress.getLocalHost() + ":" + port + " --");
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
} catch (IOException ex) {
System.out.println("Can't setup server on this port number. ");
}
Socket jposSocket = null;
InputStream inJpos = null;
OutputStream outJpos = null;
ByteArrayOutputStream abOutputStream = null;
ByteArrayOutputStream abJposOutputStream = null;
try {
jposSocket = serverSocket.accept();
jposSocket.setSoTimeout(5000);
jposSocket.setReceiveBufferSize(sendReceiveBufferSize);
jposSocket.setSendBufferSize(sendReceiveBufferSize);
} catch (IOException ex) {
System.out.println("Can't accept client connection. ");
}
try {
inJpos = jposSocket.getInputStream();
} catch (IOException ex) {
System.out.println("Can't get socket input stream. ");
}
try {
outJpos = jposSocket.getOutputStream();
} catch (IOException ex) {
System.out.println("Can't get socket input stream. ");
}
abOutputStream = null;
abJposOutputStream = new ByteArrayOutputStream();
Socket printerSocket = null;
InputStream inPrinter = null;
OutputStream outPrinter = null;
int count = 0;
while (true)
{
abOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[bufferSize];
//count = inJpos.read(bytes);
while (true) {
if (inJpos.available() > 0)
{
count = inJpos.read(bytes);
abOutputStream.write(bytes, 0, count);
break;
}
else {
Thread.sleep(1000);
}
}
System.out.println(port + " TCP --> " + abOutputStream.toString("UTF-8"));
if (printerSocket == null)
{
printerSocket = new Socket("192.168.10.31", port);
inPrinter = printerSocket.getInputStream();
outPrinter = printerSocket.getOutputStream();
}
outPrinter.write(abOutputStream.toByteArray());
bytes = new byte[bufferSize];
while (inPrinter.available() > 0 && (count = inPrinter.read(bytes)) > 0) {
abJposOutputStream.write(bytes, 0, count);
}
outJpos.write(abJposOutputStream.toByteArray());
outJpos.flush();
}
}
}
次に、別のスレッドでメイン アプリケーションから Listen メソッドが呼び出されます。
上記のコードは、私が作成したクライアントでテストすると正常に動作し、メッセージを適切に送受信しますが、問題は JPOS 接続にあります。
JPOS アプリケーションからの TCP 接続に何らかの特別な処理はありますか?
どんな助けでも大歓迎です。
ありがとう