【发布时间】:2016-05-19 11:48:39
【问题描述】:
嗨,我希望你们做得很好。
我目前正在尝试测试一个创建与打印服务器的套接字连接的服务类。
这是服务类:
public class PrintServiceImpl implements PrintService {
private static final Logger LOGGER = LoggerFactory.getLogger(PrintServiceImpl.class);
static final int TIMEOUT_MILLISECOND = 20000;
@Override
public boolean sendLabelToPrintServer(String hostname, int port, String labelData) {
Socket clientSocket = null;
DataOutputStream outToServer = null;
Boolean successful;
try {
// open the connection to the printing server
clientSocket = new Socket();
clientSocket.connect(new InetSocketAddress(hostname, port), TIMEOUT_MILLISECOND);
clientSocket.setSoTimeout(TIMEOUT_MILLISECOND);
outToServer = new DataOutputStream(clientSocket.getOutputStream());
// send data to print
outToServer.writeBytes(labelData);
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(),
StandardCharsets.UTF_8));
// Read HTTP Request CHARACTER BY CHARACTER instead of line by line
while ((char) input.read() != 0 && (char) input.read() != '\r') {
LOGGER.debug("Getting print server answer.");
}
successful = true;
LOGGER.debug("Label printed.");
} catch (Exception e) {
LOGGER.error("Printing failed.", e);
successful = false;
} finally {
try {
// close connection
if (outToServer != null) {
outToServer.close();
}
if (clientSocket != null) {
clientSocket.close();
}
} catch (IOException e) {
LOGGER.error("Exception while closing DataOutputStream/ClientSocket.", e);
}
}
return successful;
}
这是我的测试课。如您所见,@Before 方法在新线程上实例化了一个 SocketServer。
public class PrintServiceImplTest {
private static final Logger LOGGER = LoggerFactory.getLogger(PrintServiceImplTest.class);
PrintServiceImpl whfPrintService = new PrintServiceImpl();
private static final String LOCALHOST = "localhost";
private static final int SERVER_PORT = 3000;
private static final String LABEL_TEXT = "This dummy text is sent to the print server";
private static final String RESPONSE = "Test Label printed correctly";
private ServerSocket server;
private Socket incommingSocket = null;
@Before
public void before() {
Thread myThread = new Thread() {
@Override
public void run() {
try {
server = new ServerSocket(SERVER_PORT);
incommingSocket = server.accept();
} catch (IOException e) {
e.printStackTrace();
}
}
};
myThread.start();
}
@After
public void after() {
try {
incommingSocket.close();
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void returnsTrueIfConnectionSuccessful() {
BufferedReader reader = null;
PrintWriter out = null;
BufferedReader in = null;
String line;
whfPrintService.sendLabelToPrintServer(LOCALHOST, SERVER_PORT, LABEL_TEXT);
try {
in = new BufferedReader(new InputStreamReader(incommingSocket.getInputStream()));
out = new PrintWriter(incommingSocket.getOutputStream());
reader = new BufferedReader(new InputStreamReader(incommingSocket.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println("line : " + line);
}
out.write(RESPONSE);
out.flush();
out.close();
in.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
当我运行测试时,我得到一个 SocketTimoutException。它发生在客户端从服务器读取响应时。这一行:
while ((char) input.read() != 0 && (char) input.read() != '\r') {
所以这意味着客户端没有收到响应。我的猜测是服务器没有发送正确的响应。
我错过了什么?提前谢谢你。
【问题讨论】: