- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个 Java 客户端/服务器应用程序,它将在每个程序之间来回传递 2 个 map 。客户端填充初始映射并将其发送到服务器。服务器按预期接收 map 并显示数据。服务器然后构建一个响应映射,并将其返回给客户端。客户端按预期接收 map 并显示数据。一切似乎都很好。现在客户端清除 map 并用更多数据重新填充 map 并将 map 发送到服务器。服务器按预期接收它,构建并发送响应映射。客户端读取 map 的“objectinputstream”,但是 map 现在是空的。客户端清除 map 并用更多数据重新填充 map ,然后再次将 map 发送到服务器。服务器似乎收到了 map ,但是显示的数据是第二次成功传输时收到的数据,而不是当前数据。谁能阐明这里发生了什么?非常感谢所有帮助!
这是服务器的日志:
- awaiting connection
- about to readobject
- read this>I-InkjetBadLabel = `Send`
- read this>I-LogoPakBadLabel = `Send`
- read this>I-InkjetBadLabelAck = `Response`
- read this>I-LogoPakBadLabelAck = `Response`
- writing this>I-InkjetBadLabel = `Send`
- writing this>I-LogoPakBadLabel = `Send`
- writing this>I-InkjetBadLabelAck = `Response`
- writing this>I-LogoPakBadLabelAck = `Response`
- wrote socket
- about to readobject
- read this>InkjetBadLabel = `False0`
- read this>LogoPakBadLabel = `False0`
- writing this>InkjetBadLabel = `False0`
- writing this>LogoPakBadLabel = `False0`
- wrote socket
- about to readobject
- read this>InkjetBadLabel = `False0`
- read this>LogoPakBadLabel = `False0`
- writing this>InkjetBadLabel = `False0`
- writing this>LogoPakBadLabel = `False0`
- wrote socket
- about to readobject
- read this>InkjetBadLabel = `False0`
- read this>LogoPakBadLabel = `False0`
- writing this>InkjetBadLabel = `False0`
- writing this>LogoPakBadLabel = `False0`
- wrote socket
- about to readobject
- read this>InkjetBadLabel = `False0`
- read this>LogoPakBadLabel = `False0`
- writing this>InkjetBadLabel = `False0`
- writing this>LogoPakBadLabel = `False0`
- wrote socket
这是客户的日志:
- PLC_socket. open!!
- istream = new ObjectInputStream
- ostream = new ObjectOutputStream
- sending externalProgramCommInitializeMap
- externalProgramCommInitializeMap =>I-InkjetBadLabel = `Send`
- externalProgramCommInitializeMap =>I-LogoPakBadLabel = `Send`
- externalProgramCommInitializeMap =>I-InkjetBadLabelAck = `Response`
- externalProgramCommInitializeMap =>I-LogoPakBadLabelAck = `Response`
- ostream.writeObject
- istream.readObject() 1
- initialize received externalProgramCommBitMapOutOfPFC response -
- externalProgramCommBitMap =>I-InkjetBadLabel = `Send`
- externalProgramCommBitMap =>I-LogoPakBadLabel = `Send`
- externalProgramCommBitMap =>I-InkjetBadLabelAck = `Response`
- externalProgramCommBitMap =>I-LogoPakBadLabelAck = `Response`
- xx=0
- externalProgramCommBitMapIntoPFC.clear
- externalProgramCommBitMapIntoPFC =>InkjetBadLabel = `False0`
- externalProgramCommBitMapIntoPFC =>LogoPakBadLabel = `False0`
- writing map!
- ostream.flush
- done writing
- data - istream.readObject()
- data - received externalProgramCommBitMapOutOfPFC response
- !!!!!! RESPONSE MAP IS EMPTY !!!!!!
- xx=1
- externalProgramCommBitMapIntoPFC.clear
- externalProgramCommBitMapIntoPFC =>InkjetBadLabel = `False1`
- externalProgramCommBitMapIntoPFC =>LogoPakBadLabel = `False1`
- writing map!
- ostream.flush
- done writing
- data - istream.readObject()
- data - received externalProgramCommBitMapOutOfPFC response
- !!!!!! RESPONSE MAP IS EMPTY !!!!!!
- xx=2
- externalProgramCommBitMapIntoPFC.clear
- externalProgramCommBitMapIntoPFC =>InkjetBadLabel = `False2`
- externalProgramCommBitMapIntoPFC =>LogoPakBadLabel = `False2`
- writing map!
- ostream.flush
- done writing
- data - istream.readObject()
- data - received externalProgramCommBitMapOutOfPFC response
- !!!!!! RESPONSE MAP IS EMPTY !!!!!!
- xx=3
- externalProgramCommBitMapIntoPFC.clear
- externalProgramCommBitMapIntoPFC =>InkjetBadLabel = `False3`
- externalProgramCommBitMapIntoPFC =>LogoPakBadLabel = `False3`
- writing map!
- ostream.flush
- done writing
- data - istream.readObject()
- data - received externalProgramCommBitMapOutOfPFC response
- !!!!!! RESPONSE MAP IS EMPTY !!!!!!
这是我的客户端程序:
package com.agfa.Verify;
import java.io.*;
import java.net.*;
import java.net.UnknownHostException;
import java.util.*;
import org.apache.log4j.Logger;
public class testVerifyScannedLabels implements Serializable {
private static final long serialVersionUID = 1L;
private final Logger log = Logger.getLogger(testVerifyScannedLabels.class.getSimpleName());
private boolean debugEnabled = false;
private Map<String, String> externalProgramCommBitMapIntoPFC = new LinkedHashMap<String, String>();
private Map<String, String> externalProgramCommBitMapOutOfPFC = new LinkedHashMap<String, String>();
private Socket PLC_socket = null;
private Map<String, String> externalProgramCommInitializeMap = new LinkedHashMap<String, String>();
private ObjectOutputStream ostream = null;
private ObjectInputStream istream = null;
private List<String> errorMessages = new ArrayList<String>();
private void initialize() {
externalProgramCommInitializeMap.put("I-InkjetBadLabel", "Send");
externalProgramCommInitializeMap.put("I-LogoPakBadLabel", "Send");
externalProgramCommInitializeMap.put("I-InkjetBadLabelAck", "Response");
externalProgramCommInitializeMap.put("I-LogoPakBadLabelAck", "Response");
}
public testVerifyScannedLabels() {
initialize();
/***********************************************************************
* sockets client for PLC Communicator
**********************************************************************/
if (PLC_socket == null) {
log.debug("PLC_socket == null");
try {
istream = null;
ostream = null;
PLC_socket = new Socket(InetAddress.getLocalHost(), 60103);
PLC_socket.setKeepAlive(true);
log.debug("PLC_socket opened on port 60103");
} catch (NumberFormatException x) {
log.warn("PLC_CommMapPort-NumberFormatException Exception! " + x.getMessage());
} catch (ConnectException x) {
log.warn("PLC_CommMapPort-Connection Exception! " + x.getMessage());
} catch (UnknownHostException x) {
log.warn("PLC_CommMapPort-UnknownHostException Exception! " + x.getMessage());
} catch (IOException x) {
log.warn("PLC_CommMapPort-IOException Exception! " + x.getMessage());
}
}
if (PLC_socket == null) {
log.error("PLC_socket was not established!");
istream = null;
ostream = null;
} else {
log.debug("PLC_socket not null!!!");
try { // Socket client..
if (PLC_socket.isConnected() && !PLC_socket.isClosed()) {
log.info("PLC_socket. open!!");
if (istream == null) {
log.debug("istream = new ObjectInputStream");
istream = new ObjectInputStream(PLC_socket.getInputStream());
}
if (ostream == null) {
log.debug("ostream = new ObjectOutputStream");
ostream = new ObjectOutputStream(PLC_socket.getOutputStream());
log.debug("sending externalProgramCommInitializeMap");
final Iterator<Map.Entry<String, String>> it = externalProgramCommInitializeMap
.entrySet().iterator();
while (it.hasNext()) {
final Map.Entry<String, String> pairs = it.next();
log.debug("externalProgramCommInitializeMap =>" + pairs.getKey()
+ " = `" + pairs.getValue() + "`");
}
log.debug("ostream.writeObject");
ostream.writeObject(externalProgramCommInitializeMap);
log.debug("istream.readObject() 1 ");
externalProgramCommBitMapOutOfPFC = (Map<String, String>) istream
.readObject();
log.debug(" initialize received externalProgramCommBitMapOutOfPFC response -");
final Iterator<Map.Entry<String, String>> rit = externalProgramCommBitMapOutOfPFC
.entrySet().iterator();
while (rit.hasNext()) {
final Map.Entry<String, String> pairs = rit.next();
log.debug("externalProgramCommBitMap =>" + pairs.getKey() + " = `"
+ pairs.getValue() + "`");
}
}
for (int xx = 0; xx < 100; xx++) { // for testing only
log.debug("xx=" + xx);
log.debug("externalProgramCommBitMapIntoPFC.clear ");
externalProgramCommBitMapIntoPFC.clear();
externalProgramCommBitMapIntoPFC.put("InkjetBadLabel", "False" + xx);
externalProgramCommBitMapIntoPFC.put("LogoPakBadLabel", "False" + xx);
externalProgramCommBitMapOutOfPFC.clear();
for (String line : errorMessages) {
log.info("build externalProgramCommBitMapIntoPFC " + line.toUpperCase());
if (line.toUpperCase().contains("INKJET")) {
log.debug("in ink");
externalProgramCommBitMapIntoPFC.put("InkjetBadLabel", "True" + xx);
}
if (line.toUpperCase().contains("LOGOPAK")) {
log.debug("in logo");
externalProgramCommBitMapIntoPFC
.put("LogoPakBadLabel", "True" + xx);
}
}
final Iterator<Map.Entry<String, String>> wit = externalProgramCommBitMapIntoPFC
.entrySet().iterator();
while (wit.hasNext()) {
final Map.Entry<String, String> pairs = wit.next();
log.debug("externalProgramCommBitMapIntoPFC =>" + pairs.getKey()
+ " = `" + pairs.getValue() + "`");
}
log.debug("writing map!");
ostream.writeObject(externalProgramCommBitMapIntoPFC);
log.debug("ostream.flush");
ostream.flush();
log.debug("done writing");
log.debug("data - istream.readObject() ");
externalProgramCommBitMapOutOfPFC = (Map<String, String>) istream
.readObject();
log.debug("data - received externalProgramCommBitMapOutOfPFC response");
final Iterator<Map.Entry<String, String>> rit = externalProgramCommBitMapOutOfPFC
.entrySet().iterator();
if (rit.hasNext()) {
while (rit.hasNext()) {
final Map.Entry<String, String> pairs = rit.next();
log.debug("externalProgramCommBitMap =>" + pairs.getKey() + " = `"
+ pairs.getValue() + "`");
}
} else
log.debug(" !!!!!! RESPONSE MAP IS EMPTY !!!!!!");
Thread.sleep(4000L);
/**/} // end for testing ..
} else {
log.error("VerifyScannedLabels unable to connect to PLC_Datacomm via Sockets");
}
// end of Socket client
} catch (UnknownHostException e) {
log.error("VerifyScannedLabels unable to connect to PLC_Datacomm via Sockets "
+ e.getMessage());
PLC_socket = null;
} catch (IOException e) {
log.error("VerifyScannedLabels unable to connect to PLC_Datacomm via Sockets "
+ e.getMessage());
PLC_socket = null;
} catch (ClassNotFoundException e) {
log.error("VerifyScannedLabels unable to connect to PLC_Datacomm via Sockets "
+ e.getMessage());
PLC_socket = null;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (debugEnabled) {
log.debug("VerifyScannedLabels processing Ends.");
}
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
testVerifyScannedLabels vsl = new testVerifyScannedLabels();
}
}
这是我的服务器程序:
package com.agfa.sockets;
import java.net.*;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.io.*;
import org.apache.log4j.Logger;
public class testSocketThreadSendReceiveMap {
private Logger log = Logger.getLogger(testSocketThreadSendReceiveMap.class.getSimpleName());
private ServerSocket ssock = null;
private Socket socket = null;
private ObjectOutputStream ostream = null;
private ObjectInputStream istream = null;
private Map<String, String> dataMapIntoRemote = new LinkedHashMap<String, String>();
private Map<String, String> dataMapFromRemote = new LinkedHashMap<String, String>();
public static void main(String[] args) {
testSocketThreadSendReceiveMap tSTSRM = new testSocketThreadSendReceiveMap();
}
@SuppressWarnings("unchecked")
public testSocketThreadSendReceiveMap() {
try {
log.info("awaiting connection");
ssock = new ServerSocket(60103);
socket = ssock.accept();
ostream = new ObjectOutputStream(socket.getOutputStream());
istream = new ObjectInputStream(socket.getInputStream());
while (true) {
//log.debug("following is for testing - remove from production");
/*
* in real life dataMapIntoRemote will be provided by parent
* Thread
*/
dataMapIntoRemote.clear();
/****** remove above *****/
dataMapFromRemote.clear();
log.debug("about to readobject");
dataMapFromRemote.putAll((Map<String, String>) istream.readObject());
final Iterator<Map.Entry<String, String>> it = dataMapFromRemote.entrySet()
.iterator();
while (it.hasNext()) {
final Map.Entry<String, String> pairs = it.next();
log.debug("read this>" + pairs.getKey() + " = `" + pairs.getValue() + "`");
//log.debug("following is for testing - remove from production");
/*
* in real life dataMapIntoRemote will be provided by
* parent Thread
*/
dataMapIntoRemote.put(pairs.getKey(), pairs.getValue());
/****** remove above *****/
}
final Iterator<Map.Entry<String, String>> wit = dataMapIntoRemote.entrySet()
.iterator();
while (wit.hasNext()) {
final Map.Entry<String, String> pairs = wit.next();
log.debug("writing this>" + pairs.getKey() + " = `" + pairs.getValue() + "`");
}
ostream.writeObject(dataMapIntoRemote);
ostream.flush();
log.debug("wrote socket");
}
} catch (IOException e) {
log.error("SocketThreadSendReceiveMap-While(True).IOException! " + e.getMessage());
} catch (Exception e) {
log.error("SocketThreadSendReceiveMap-While(True).Exception! " + e.getMessage());
}
try {
log.warn("SocketThreadSendReceiveMap.class socket.close");
socket.close();
} catch (IOException e) {
log.error("SocketThreadSendReceiveMap-socket.close().IOException! " + e.getMessage());
}
}
}
最佳答案
您还需要在调用 ostream.flush()
之后立即调用 ostream.reset()
。
ObjectOutputStream
维护它编写的引用的内部映射,以便在您最终多次编写相同的引用时节省序列化时间,这就是您在发送映射时所做的事情。在 flush()
之后调用 reset()
会清除此内部引用表并导致对象在下次调用 writeObject(object)
时重新序列化,无论之前是否传入过相同的引用。
关于Java 客户端/服务器双向套接字不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16968892/
我正在维护一些 Java 代码,我目前正在将它们转换为 C#。 Java 代码是这样做的: sendString(somedata + '\000'); 在 C# 中,我正在尝试做同样的事情: sen
如何确定函数中传递的参数是字符串还是字符(不确定如何正确调用它)文字? 我的函数(不正确): void check(const char* str) { // some code here }
我真的不知道如何准确地提出这个问题,但我希望标题已经说明了这一点。 我正在寻找一种方法(一个框架/库),它提供了执行 String.contains() 函数的能力,该函数告诉我给定的字符串是否与搜索
我正在尝试编写一些读取 Lambda 表达式并输出 beta 缩减版本的东西。 Lambda 的类型如下:\variable -> expression,应用程序的形式为 (表达式) (表达式)。因此
StackOverflow 上的第 1 篇文章,如果我没能把它做好,我深表歉意。我陷入了一个愚蠢的练习,我需要制作一个“刽子手游戏”,我尝试从“.txt”文件中读取单词,然后我得到了我的加密函数,它将
我想在 Groovy 中测试我的 Java 自定义注释,但由于字符问题而未能成功。 Groovyc: Expected 'a' to be an inline constant of type cha
当我尝试在单击按钮期间运行 javascript location.href 时,出现以下错误“字 rune 字中的字符过多”。 最佳答案 这应该使用 OnClientClick相反? 您可能还想停
我想要类似的东西: let a = ["v".utf8[0], 1, 2] 我想到的最接近的是: let a = [0x76, 1, 2] 和 "v".data(using: String.Encod
有没有办法在 MySQL 中指定 Unicode 字 rune 字? 我想用 Ascii 字符替换 Unicode 字符,如下所示: Update MyTbl Set MyFld = Replace(
阅读 PNG 规范后,我有点惊讶。我读过字 rune 字应该用像 0x41 这样的二进制值进行硬编码,而不是在(程序员友好的)'A' 中。问题似乎是在具有不同底层字符集的不同系统上编译期间字 rune
考虑一个具有 UTF-8 执行字符集的 C++11 编译器(并且符合要求 char 类型为有符号 8 位字节的 x86-64 ABI) . 字母 Ä(元音变音)具有 0xC4 的 unicode 代码
为什么即使有 UTF-8 字符串文字,C11 或 C++11 中也没有 UTF-8 字 rune 字?我知道,一般来说,字 rune 字表示单个 ASCII 字符,它与单字节 UTF-8 代码点相同,
我怎样才能用 Jade 做到这一点? how would I do this 我几乎可以做任何事情,除了引入一个 span 中间句子。 最佳答案 h3.blur. how would I do t
这似乎是一个非常简单的问题,但我只是想澄清我的疑问。我正在查看其他开发人员编写的代码。有一些涉及 float 的计算。 示例:Float fNotAvlbl = new Float(-99); 他为什
我想知道第 3 行“if dec:”中的“dec”是什么意思 1 def dec2bin(dec): 2 result='' 3 if dec:
我试图在字符串中查找不包含任何“a”字符的单词。我写了下面的代码,但它不起作用。我怎么能对正则表达式说“不包括”?我不能用“^”符号表示“不是”吗? import re string2 = "asfd
这个问题在这里已经有了答案: Is floating point math broken? (31 个答案) Is floating point arbitrary precision availa
我正在创建一个时尚的文本应用程序,但在某些地方出现错误(“字 rune 字中的字符太多”)。我只写了一个字母,但是当我粘贴它时,它会转换成许多这样的字母:“\uD83C\uDD89”,原始字母是“🆉
我正在尝试检查用户是否在文本框中输入了一个数字值,是否接受了小数位。非常感谢任何帮助。 Private Sub textbox1_AfterUpdate() If IsNumeric(textbox1
我知道一个 Byte 是 8 位,但其他的代表什么?我正在参加一个使用摩托罗拉 68k 架构的汇编类(class),我对目前的词汇感到困惑。 最佳答案 如 operator's manual for
我是一名优秀的程序员,十分优秀!