【发布时间】:2017-10-26 04:29:14
【问题描述】:
我需要使用缓冲区读取器通过套接字进行通信,我只是想知道如何保留新行。我希望最终在密码学中使用它,所以我不能假设新行位于消息的末尾。
我是一般套接字的新手,因此我们将不胜感激。
客户端发送的文本
ct.txt3nd0fh34d3rl1n31663nd0fh34d3rl1n3This is a test message to see if the file decrypts properly. Nothing special in this file.
Just a pure test to see if my code works. Two chemists walk into a bar...
字符串长度为205
在服务器端我得到
ct.txt3nd0fh34d3rl1n31663nd0fh34d3rl1n3This is a test message to see if the file decrypts properly. Nothing special in this file.
Just a pure test to see if my code works. Two chemists walk into a bar...
这个长度是206,就是在末尾加1个换行符。这是在我尝试实施我发现的一些解决方案之后,但由于我偏离了 1 个字符,它们没有工作。 这是读取客户端输入的代码
while (incoming != null)
{
/* If the client has sent "exit", instruct the server to
* remove this thread from the vector of active connections.
* Then close the socket and exit.
*/
if (incoming.compareTo("exit") == 0)
{
parent.kill (this);
try {
in.close ();
sock.close ();
}
catch (IOException e)
{/*nothing to do*/}
return;
}
/* If the client has sent "die", instruct the server to
* signal all threads to shutdown, then exit.
*/
else if (incoming.compareTo("die") == 0)
{
parent.killall ();
return;
}
System.out.println("LINE: " + incoming.length() + " " + incoming);
if(incoming.equals("3nd0f5krr4hf1l3") && !msgGotten)
{
//Decrypt
SecretKeySpec key = CryptoUtilities.key_from_seed("H".getBytes());
System.out.print(message);
System.out.println(message.length());
byte[] encryptedMessageAndDigest = message.getBytes();
byte[] decryptedMessageAndDigest = CryptoUtilities.decrypt(encryptedMessageAndDigest, key);
if(CryptoUtilities.verify_hash(decryptedMessageAndDigest, key))
{
byte[] headerMessage = CryptoUtilities.extract_message(decryptedMessageAndDigest);
String payload = new String(headerMessage);
String[] plainTextArray = payload.split("3nd0fh34d3rl1n3", 3);
String fileName = plainTextArray[0];
byte[] plaintextBytes = plainTextArray[2].getBytes();
try
{
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(plaintextBytes);
fos.close();
}
catch (IOException e)
{
}
}
else
{
}
}
else
{
/* Otherwise, just echo what was received. */
//System.out.println ("Client " + idnum + ": " + incoming);
if(incoming.length() == 0)
{
message = message + "\n";
}
else
{
message = message + incoming + "\n";
}
}
提前致谢!
编辑:在我的初始传输结束时,我发送 3nd0f5krr4hf1l3 表示它。我需要readLine(),所以我可以一口气解析,但如果read() 确实是最好的方法,我将如何实现它? \n (\r\n) 字符会在读取中出现吗?
【问题讨论】:
标签: java sockets cryptography bufferedreader