我目前正在制作一个消息传递应用程序,稍后我还将对其添加加密。我有一个服务器打开一个套接字,等待客户端连接。客户机连接后,它为该客户机创建一个新线程。然后等待消息等
我遇到的问题是,当我试图将公钥指数和模数发送给客户端时,客户端无法使用datainputstream(clientSocket.getInputStream()).readLine
。
指数和模数的发送发生在line 173 and 174
线上,客户端的接收发生在125 and 136
线上。
发送数据的服务器。
os.println(modulusMap.get(requestName));
os.println(exponentMap.get(requestName));
客户端试图将数据写入变量:
pubModulusString = is.readLine().trim();
BigInteger pubModulus = new BigInteger(pubModulusString);
HashMap<String, BigInteger> modulusMap = new HashMap<String, BigInteger>();
modulusMap.put(message.substring(1), pubModulus);
String pubExponentString = is.readLine();
我已经测试了数据的发送只是一个打印流,它是完美的工作。只有当我试图读取数据并将其保存为变量时,问题才会发生(line 125 and 136)
。
大多数情况下,readLine()
将模数读取为空白,但有时也会成功读取。我找不到一个模式来说明它什么时候成功,什么时候失败。
同样非常奇怪的是,我使用完全相同的代码将数据从客户端发送到服务器。这可以在客户端line 89 and 91
上看到。服务器接收lines 113 and 119
上的数据
感谢您的帮助和建议。
下面张贴的是完整的代码,任何1是善良的,有时间运行它。
服务器: import java.io.*;
import java.math.*;
import java.net.Socket;
import java.net.ServerSocket;
import java.util.*;
/*
* A chat server that delivers public and private messages.
*/
public class MultiThreadChatServerSync {
// The server socket.
private static ServerSocket serverSocket = null;
// The client socket.
private static Socket clientSocket = null;
// This chat server can accept up to maxClientsCount clients' connections.
private static final int maxClientsCount = 10;
private static final clientThread[] threads = new clientThread[maxClientsCount];
public static void main(String args[]) {
// The default port number.
int portNumber = 2222;
if (args.length < 1) {
System.out.println("Usage: java MultiThreadChatServerSync <portNumber>n"
+ "Now using port number=" + portNumber);
} else {
portNumber = Integer.valueOf(args[0]).intValue();
}
/*
* Open a server socket on the portNumber (default 2222). Note that we can
* not choose a port less than 1023 if we are not privileged users (root).
*/
try {
serverSocket = new ServerSocket(portNumber);
} catch (IOException e) {
System.out.println(e);
}
/*
* Create a client socket for each connection and pass it to a new client
* thread.
*/
while (true) {
try {
clientSocket = serverSocket.accept();
int i = 0;
for (i = 0; i < maxClientsCount; i++) {
if (threads[i] == null) {
(threads[i] = new clientThread(clientSocket, threads)).start();
break;
}
}
if (i == maxClientsCount) {
PrintStream os = new PrintStream(clientSocket.getOutputStream());
os.println("Server too busy. Try later.");
os.close();
clientSocket.close();
}
} catch (IOException e) {
System.out.println(e);
}
}
}
}
/*
* The chat client thread. This client thread opens the input and the output
* streams for a particular client, ask the client's name, informs all the
* clients connected to the server about the fact that a new client has joined
* the chat room, and as long as it receive data, echos that data back to all
* other clients. The thread broadcast the incoming messages to all clients and
* routes the private message to the particular client. When a client leaves the
* chat room this thread informs also all the clients about that and terminates.
*/
class clientThread extends Thread {
private String clientName = null;
private DataInputStream is = null;
private BufferedReader inputLine = null;
private PrintStream os = null;
private Socket clientSocket = null;
private final clientThread[] threads;
private int maxClientsCount;
public clientThread(Socket clientSocket, clientThread[] threads) {
this.clientSocket = clientSocket;
this.threads = threads;
maxClientsCount = threads.length;
}
public void run() {
int maxClientsCount = this.maxClientsCount;
clientThread[] threads = this.threads;
HashMap<String, BigInteger> modulusMap = new HashMap<String, BigInteger>();
HashMap<String, BigInteger> exponentMap = new HashMap<String, BigInteger>();
try {
/*
* Create input and output streams for this client.
*/
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
inputLine = new BufferedReader (new InputStreamReader(System.in));
String name;
while (true) {
os.println("Enter your name.");
name = is.readLine().trim();
String pubModulusString = is.readLine().trim();
BigInteger pubModulus = new BigInteger(pubModulusString);
modulusMap.put(name, pubModulus);
String pubExponentString = is.readLine().trim();
BigInteger pubExponent = new BigInteger(pubExponentString);
exponentMap.put(name, pubExponent);
if (name.indexOf('@') == -1) {
break;
} else {
os.println("The name should not contain '@' character.");
}
}
/* Welcome the new the client. */
os.println("Welcome " + name
+ " to our chat room.nTo leave enter /quit in a new line.");
synchronized (this) {
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i] == this) {
clientName = "@" + name;
break;
}
}
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i] != this) {
threads[i].os.println("*** A new user " + name
+ " entered the chat room !!! ***");
}
}
}
/* Start the conversation. */
while (true) {
String line = is.readLine();
if (line.startsWith("/quit")) {
break;
}
if (line.startsWith("!")){
String requestName;
requestName = line;
requestName = requestName.substring(1);
os.println(modulusMap.get(requestName));
os.println(exponentMap.get(requestName));
}
else {
/* If the message is private sent it to the given client. */
if (line.startsWith("@")) {
String[] words = line.split("\s", 2);
if (words.length > 1 && words[1] != null) {
words[1] = words[1].trim();
if (!words[1].isEmpty()) {
synchronized (this) {
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i] != this
&& threads[i].clientName != null
&& threads[i].clientName.equals(words[0])) {
threads[i].os.println("<" + name + "> " + words[1]);
/*
* Echo this message to let the client know the private
* message was sent.
*/
this.os.println(">" + name + "> " + words[1]);
break;
}
}
}
}
}
} else {
/* The message is public, broadcast it to all other clients. */
synchronized (this) {
this.os.println("Please select who you wish to message by typing '@username *Your Message*'");
}
}
}
}
synchronized (this) {
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] != null && threads[i] != this
&& threads[i].clientName != null) {
threads[i].os.println("*** The user " + name
+ " is leaving the chat room !!! ***");
}
}
}
os.println("*** Bye " + name + " ***");
/*
* Clean up. Set the current thread variable to null so that a new client
* could be accepted by the server.
*/
synchronized (this) {
for (int i = 0; i < maxClientsCount; i++) {
if (threads[i] == this) {
threads[i] = null;
}
}
}
/*
* Close the output stream, close the input stream, close the socket.
*/
is.close();
os.close();
clientSocket.close();
} catch (IOException e) {
}
}
}
客户 import java.io.DataInputStream;
import java.io.*;
import java.math.*;
import java.math.BigInteger;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.UnknownHostException;
import java.security.*;
import java.security.spec.*;
import java.util.*;
import javax.crypto.*;
public class MultiThreadChatClient implements Runnable {
// The client socket
private static Socket clientSocket = null;
// The output stream
private static PrintStream os = null;
// The input stream
private static DataInputStream is = null;
private static BufferedReader inputLine = null;
private static boolean closed = false;
private static final String PUBLIC_KEY_FILE = "Public.key";
private static final String PRIVATE_KEY_FILE = "Private.key";
public static void main(String[] args) {
// The default port.
int portNumber = 2222;
// The default host.
String host = "192.168.0.16";
if (args.length < 2) {
System.out
.println("Usage: java MultiThreadChatClient <host> <portNumber>n"
+ "Now using host=" + host + ", portNumber=" + portNumber);
} else {
host = args[0];
portNumber = Integer.valueOf(args[1]).intValue();
}
/*
* Open a socket on a given host and port. Open input and output streams.
*/
try {
clientSocket = new Socket(host, portNumber);
inputLine = new BufferedReader(new InputStreamReader(System.in));
os = new PrintStream(clientSocket.getOutputStream());
is = new DataInputStream(clientSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + host);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to the host "
+ host);
}
/*
* If everything has been initialized then we want to write some data to the
* socket we have opened a connection to on the port portNumber.
*/
if (clientSocket != null && os != null && is != null) {
try {
/* Create a thread to read from the server. */
new Thread(new MultiThreadChatClient()).start();
String myName = inputLine.readLine();
os.println(myName.trim());
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048); //1024 used for normal securities
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
//Pullingout parameters which makes up Key
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec rsaPubKeySpec = (RSAPublicKeySpec) keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);
RSAPrivateKeySpec rsaPrivKeySpec = (RSAPrivateKeySpec) keyFactory.getKeySpec(privateKey, RSAPrivateKeySpec.class);
os.println(rsaPubKeySpec.getModulus());
System.out.println(rsaPubKeySpec.getModulus());
os.println(rsaPubKeySpec.getPublicExponent());
FileOutputStream fos = null;
ObjectOutputStream oos = null;
fos = new FileOutputStream(PUBLIC_KEY_FILE);
oos = new ObjectOutputStream(new BufferedOutputStream(fos));
oos.writeObject(rsaPubKeySpec.getModulus());
oos.writeObject(rsaPubKeySpec.getPublicExponent());
oos.close();
fos.close();
fos = new FileOutputStream(PRIVATE_KEY_FILE);
oos = new ObjectOutputStream(new BufferedOutputStream(fos));
oos.writeObject(rsaPrivKeySpec.getModulus());
oos.writeObject(rsaPrivKeySpec.getPrivateExponent());
oos.close();
fos.close();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}catch (InvalidKeySpecException e) {
e.printStackTrace();
}
while (!closed) {
String message = inputLine.readLine();
if (message.startsWith("!")){
os.println(message.trim());
String pubModulusString = is.readLine().trim();
BigInteger pubModulus = new BigInteger(pubModulusString);
HashMap<String, BigInteger> modulusMap = new HashMap<String, BigInteger>();
modulusMap.put(message.substring(1), pubModulus);
String pubExponentString = is.readLine();
BigInteger pubExponent = new BigInteger(pubExponentString);
HashMap<String, BigInteger> exponentMap = new HashMap<String, BigInteger>();
exponentMap.put(message.substring(1), pubExponent);
System.out.println(modulusMap.get(message.substring(1)) + " Hi");
System.out.println(exponentMap.get(message.substring(1)) + "asdk");
}
else{
// INSERT ENCRYPTION ON "message" here
os.println(message.trim());
}
}
/*
* Close the output stream, close the input stream, close the socket.
*/
os.close();
is.close();
clientSocket.close();
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
/*
* Create a thread to read from the server. (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
/*
* Keep on reading from the socket till we receive "Bye" from the
* server. Once we received that then we want to break.
*/
String responseLine;
try {
while ((responseLine = is.readLine()) != null) {
System.out.println(responseLine);
if (responseLine.indexOf("*** Bye") != -1)
break;
}
closed = true;
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
如果readLine()返回空行,这是因为发送方发送了一个空行。如果返回null,则表示发送方已关闭连接。
你找错地方了。DataInputStream的readLine方法已弃用。我不知道你为什么要用它,通常如果你使用像eclipse这样的IDE,你会看到这个方法被划掉。
您应该使用BufferedReader而不是Java文档本身所解释的,这里是一个链接