0

客户端 (C)- 服务器 (S) 连接已使用 TCP 成功建立。

C--------> S(使用 DataOutputStream 发送第一个数据)

C--------> S(使用 DataOutputStream 发送第二个数据)

S-----> C(使用BufferedWriter发送数据)

C-->XS

客户端发送最后一个数据并自动关闭连接,服务器没有收到数据并显示连接重置错误;

仅当在两台不同的机器上执行时才会发生这种情况。

该程序在 localhost (127.0.0.1) 中运行良好。

为什么会这样?请帮我。

以下是服务器和客户端代码

Server.java

public class Party_B extends Thread {

public static BigInteger p,q,g,rB,s,y,mA,nA,DH1, DH2,yA,yB,sA,sB,n,rA,mB;
public static String M="";
private static BigInteger xB,w;


 public static ServerSocket serverSocket;

public Party_B(int port) throws IOException
{
  serverSocket = new ServerSocket(port);
  serverSocket.setSoTimeout(10000);
}

public static void receive_initial_parameters( DataInputStream dis )throws IOException
{

                    {
                        int len = dis.readInt();
                        byte[] data = new byte[len];
                        if (len > 0)
                        {
                                dis.readFully(data);
                                String InitialAB = new String(data, "UTF-8");

                                    String[] temp;
                            String delimiter = "\\|";

                             temp =InitialAB .split(delimiter);
                         p= new BigInteger(temp[0], 16);
                         q= new BigInteger(temp[1], 16);
                         g= new BigInteger(temp[2], 16);
                         yA= new BigInteger(temp[3], 16);

                         }

                    }

  }


    public static  void connect_to_PartyA (Socket server)throws IOException 
    {
        System.out.println("Just connected to "+ server.getRemoteSocketAddress());
        BufferedReader reader = new BufferedReader( new InputStreamReader(server.getInputStream()));

         String line = reader.readLine();
         System.out.println(line);

         BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(server.getOutputStream()));
            writer.write("Thank you for connecting to "+ server.getLocalSocketAddress());
            writer.newLine();
            writer.flush();
    }
    public static  StringBuffer receive_mA(DataInputStream dis) throws IOException
    {
        int len = dis.readInt();
    byte[] data = new byte[len];
    if (len > 0) {
        dis.readFully(data);
     }
            String s1 = new String(data, "UTF-8");
            StringBuffer s2=new StringBuffer(s1);
            return s2;
    }
    public static  void send_mB_sB (BufferedWriter writer2) throws IOException
    {

        writer2.write("ED546546|BB657657|CD56546\r\n");

                      writer2.flush();
    }
     public static  String receive_sA(DataInputStream dis2) throws IOException
     {
          int  len = dis2.readInt();
         byte[] data = new byte[len];

                if (len > 0) {
                dis2.readFully(data);
             }

                  String s1 = new String(data, "UTF-8");
                 StringBuffer s2=new StringBuffer(s1);

              return s2.toString();
     }
public static  void main  (String args[]) throws IOException 
{
      System.out.println("\t\t\t\t PARTY B");
    int  port =1300;
            // create a server socket and wait for client's connection
    ServerSocket serverSocket = new ServerSocket(port);

try
     {
         System.out.println("\n\n\nWaiting for Party A on port " +serverSocket.getLocalPort() + "...");
        Socket server = serverSocket.accept();

        connect_to_PartyA(server);
        InputStream in = server.getInputStream();
        DataInputStream dis = new DataInputStream(in);
        receive_initial_parameters(dis);


    //receive commitment
       in = server.getInputStream();
       dis = new DataInputStream(in);


            StringBuffer s2= receive_mA(dis);
           mA= new BigInteger (s2.toString(), 16);

           System.out.println("\n\nReceived from Party A: mA="+mA.toString(10));

     System.out.println("Sending Party A yB,mB,sB");

          BufferedWriter writer2 = new BufferedWriter( new OutputStreamWriter(server.getOutputStream()));              

            send_mB_sB(writer2) ;     

             InputStream in2 = server.getInputStream();
             DataInputStream dis2 = new DataInputStream(in2);


        sA = new BigInteger (receive_sA(dis2),16);
        rA=mA.mod(q);
         System.out.println("rA="+rA.toString(10));
     System.out.println("\nReceived from Party A sA="+sA.toString(10)+")");



     }
catch(SocketTimeoutException s)
     {
        System.out.println("Socket timed out!");

     }
}

客户端.java

public class Party_A  {

  public static BigInteger p,q,g,yA,sA,n,rA,nA,mA,DH1,DH2 ;
  public static BigInteger yB,sB,mB,nB ,rB;
  private static BigInteger xA,v;

             public static void send_inital_paramaters(DataOutputStream out ) throws IOException
     {
          String InitialAB= new String ("FFAB10234|E02918|AB78E|DC435435");


                 byte buf[]=new byte[1024];
                 buf=InitialAB.getBytes();
                 out.flush();
                out.writeInt(buf.length);
                out.write(buf, 0, buf.length);

}

public static  void connect_to_PartyB(Socket client) throws IOException 
{
     System.out.println("Just connected to "+ client.getRemoteSocketAddress());

 OutputStream outToServer = client.getOutputStream();
    BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(outToServer));


    writer.write("Hello from "+ client.getLocalSocketAddress());
    writer.newLine();
    writer.flush();


    BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
    String result= reader.readLine();
     System.out.println("\nReceived from Party B: "+result);
}
public static  void send_mA(DataOutputStream out)throws IOException
{
     System.out.println("\nSending to Party B mA=FF09123");
       String Xs= new String ("FF09123");
       byte buf[]=new byte[1024];
       buf=Xs.getBytes();
       out.writeInt(buf.length);
       out.write(buf, 0, buf.length);
}
public static  boolean receive_mB_sB(BufferedReader rreader)throws IOException
{
    boolean flag=true;
    String InitialBA=null ;



    while (InitialBA==null)
    {  
        InitialBA = rreader.readLine();
         System.out.println("\nReceived from  Party B: mB|sB");

    }
   String[] temp;
         String delimiter = "\\|";
         temp =InitialBA.split(delimiter);


                         yB=new BigInteger(temp[0], 16);   
                         mB=new BigInteger(temp[1], 16); 
                         sB=new BigInteger(temp[2], 16); 
                         System.out.print("yB="+yB+"\nmB="+mB+"\nsB="+sB);


          return flag;

}
 public static  void send_sA(DataOutputStream out2, BigInteger sA)throws IOException
 {
     String Ys= new String("98967AC");
     System.out.println("\nSending to Party B sA="+Ys);
     byte buf[]=new byte[1024];
     buf=Ys.getBytes();
                    out2.writeInt(buf.length);
                    out2.write(buf, 0, buf.length);
 }


public static  void main(String[] args) throws Exception 
{

    System.out.println("\t\t\t\t PARTY A");

       String serverName = "172.16.111.8";
             int port = 1300;
      try
     {


         System.out.println("\n\nConnecting to " + serverName + " on port " + port);
         Socket client = new Socket(serverName, port);

         connect_to_PartyB(client);

        OutputStream outToServer = client.getOutputStream();

       DataOutputStream out = new DataOutputStream(outToServer);
        send_inital_paramaters(out);


        send_mA(out);

       try {
                   Thread.sleep(5000);
           } catch (InterruptedException ie) {
                  System.out.println(ie); //Handle exception
            }

      BufferedReader rreader= new BufferedReader(new InputStreamReader(client.getInputStream()));

       if(receive_mB_sB(rreader))
       {


         send_sA(out,sA);
        Thread.sleep(3000);
         client.close();

       }
       else System.out.println("Verification is incorrect-> Invalid User");


}
      catch(IOException e)
  {
     e.printStackTrace();
  }
}
}
4

1 回答 1

0

我找到了错误的原因:我没有在Client.java中写client.close()

因此,如果我们没有明确地关闭,客户端进程会自动关闭其套接字。

我已经更新了工作代码。谢谢你。

于 2013-11-09T06:08:22.430 回答