我有一组两个通过套接字相互通信的 Java 应用程序。大多数通信都是自动化的(一个发送一些东西,另一个接收并计算它,发送其他东西)。大多数进程从 Member 的键盘输入开始,然后开始与服务器进行交换。
服务器:
public class Server implements Runnable {
private static int port = 1234;
private Socket connection;
@SuppressWarnings("unused")
private int id;
private static Primer p;
private static Map<String, Object> users;
private static BigInteger two;
public static void main(String[] args){
two = new BigInteger("2");
int count = 0;
users = new HashMap<String, Object>();
p = new Primer();
p.keygen();
try {
ServerSocket ss1 = new ServerSocket(port);
System.out.println("Serwer włączony");
while(true){
Socket connection = ss1.accept();
Runnable runn = new Server(connection, ++count);
Thread t = new Thread(runn);
t.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Server(Socket s, int i) {
this.connection = s;
this.id = i;
}
@SuppressWarnings("deprecation")
@Override
public void run() {
// TODO Auto-generated method stub
try {
InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message="";
String received="";
System.out.println("Chat Server Started");
do{
inStream = connection.getInputStream();
inDataStream = new DataInputStream ( inStream );
received = inDataStream.readUTF();
System.out.println("Client sent: " + received);
outStream = connection.getOutputStream();
outDataStream = new DataOutputStream (outStream);
analyzeREC(received, outDataStream);
outDataStream.flush();
//DataInputStream dis = new DataInputStream(System.in);
//message = dis.readLine();
//System.out.println("Enter your message here: ");
//outDataStream.writeUTF(message);
}while(!message.equals("bye"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
connection.close();
}
catch (IOException e){}
}
}
public void analyzeREC(String msg, DataOutputStream dos){
String[] m = msg.split(" ");
if(m[0].equals("hello")){
m[1] = checkName(m[1]);
users.put(m[1], null);
try {
dos.writeUTF("registered " + m[1]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(m[0].equals("join")){
System.out.println("Client requested join");
try {
dos.writeUTF("gpp " + p.getGpp().toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(m[0].equals("gppConf")){
try {
dos.writeUTF("gpk " + p.getGpk().toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(m[0].equals("gpkConf")){
try {
dos.writeUTF("startJoining");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(m[0].equals("verifyProof")){
Random r = new Random();
if(verifyProof(new BigInteger(m[1]), new BigInteger(m[2]), new BigInteger(m[2]), new BigInteger(m[4]))){
System.out.println("Proof verified");
BigInteger alfafala = new BigInteger(p.getGpp().getLx(), r);
alfafala.setBit(0);
BigInteger betafala = new BigInteger(p.getGpp().getLx(), r);
try {
dos.writeUTF("sendAB " + alfafala.toString() + " " + betafala.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("Wrong proof");
}
}
}
成员:
public class Member {
private static String host = "localhost";
private static int port = 1234;
private static Primer pr;
private static String ident;
private static GroupPublicKey gpk;
private static GroupPublicParameter gpp;
private MemberSignatureKey msk;
private static ProofOfKnowledge U;
@SuppressWarnings("deprecation")
public static void main(String[] args){
InputStream inStream;
pr = new Primer();
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message = "";
String received = "";
try {
InetAddress addr = InetAddress.getByName(host);
Socket sock;
sock = new Socket(addr, port);
System.out.println("Chat Client Started");
do{
DataInputStream dis = new DataInputStream(System.in);
message = dis.readLine();
outStream = sock.getOutputStream();
outDataStream = new DataOutputStream (outStream);
outDataStream.writeUTF(message);
inStream = sock.getInputStream();
inDataStream = new DataInputStream ( inStream );
received = inDataStream.readUTF();
System.out.println("Server Sent: " + received);
analyzeMSG(received, outDataStream);
outDataStream.flush();
}while(!message.equals("bye"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//private static boolean isPortAvailable(int port) {
//}
public static void analyzeMSG(String msg, DataOutputStream dos){
String[] m = msg.split(" ");
if(m[0].equals("registered")){
ident = m[1];
}
if(m[0].equals("gpp")){
gpp = new GroupPublicParameter(Integer.parseInt(m[1]), Integer.parseInt(m[2]), Integer.parseInt(m[3]), Integer.parseInt(m[4]), Integer.parseInt(m[5]), Integer.parseInt(m[6]), Double.parseDouble(m[7]));
try {
dos.writeUTF("gppConf");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(m[0].equals("gpk")){
gpk = new GroupPublicKey(new BigInteger(m[1]), new BigInteger(m[2]), new BigInteger(m[3]), new BigInteger(m[4]), new BigInteger(m[5]), new BigInteger(m[6]));
try {
dos.writeUTF("gpkConf");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(m[0].equals("startJoining")){
try {
dos.writeUTF("verifyProof " + startJoining());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(m[0].equals("sendAB")){
System.out.println("Proof confirmed");
}
}
public static String startJoining(){
System.out.println("Started join");
Random r = new Random();
int scale = r.nextInt(gpp.getLx());
BigInteger xfala = new BigInteger(scale, r);
scale = ((gpk.getN().multiply(new BigInteger("2"))).subtract(new BigInteger("1"))).bitLength();
BigInteger rfala = Primer.primeLessThan((gpk.getN().multiply(new BigInteger("2"))).subtract(new BigInteger("1")), scale);
BigInteger C1 = (pr.pow(gpk.getG(), xfala).multiply(pr.pow(gpk.getH(), rfala))).mod(gpk.getN());
genProof(C1, xfala, rfala);
return C1.toString() + " " + U.toString();
}
private static void genProof(BigInteger C1, BigInteger xfala, BigInteger rfala){
System.out.println("Generating Proof");
Random r = new Random();
BigInteger t1 = new BigInteger((int) (gpp.getTeta()*(gpp.getLx() + gpp.getK())), r);
BigInteger t2 = new BigInteger((int) (gpp.getTeta()*((2*gpp.getLp()) + gpp.getK())) - 1, r);
BigInteger D = (pr.pow(gpk.getG(), t1).multiply(pr.pow(gpk.getH(), t2))).mod(gpk.getN());
BigInteger cfala = pr.H(gpk.getG().toString() + gpk.getH().toString() + C1.toString() + D.toString());
BigInteger s1fala = t1.subtract(cfala.multiply(xfala));
BigInteger s2fala = t2.subtract(cfala.multiply(rfala));
U = new ProofOfKnowledge(cfala, s1fala, s2fala);
}
问题是,在交换了几条消息之后,在服务器中调用了 writeUTF 方法,但是以“gpk”开头的消息直到我重新输入之前发送的确认消息 (gppConf) 才被发送。从那时起,我必须将每条消息加倍才能收到。这是什么原因?