J'essaye d'authentifier l'utilisateur en envoyant une clé et un nombre aléatoire du client au serveur.Que puis-je faire pour éviter une exception NullPointerException?
Mon code ne m'affiche pas la réponse du client. J'obtiens une exception de pointeur nul quand j'exécute mon code comme ci-dessous.
import java.io.*;
import java.net.*;
import java.lang.*;
class Client
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the key value");
int key=Integer.parseInt(br.readLine());
double random=Math.random()*50;
System.out.println(random);
int response=key%((int)random);
System.out.println(key);
System.out.println("Authentication begins");
Socket echoSocket = new Socket("localhost", 2000);
FileOutputStream fout=null;
DataOutputStream clientout=null;
clientout.writeDouble(random);
clientout.writeInt(key);
clientout.writeInt(response);
fout.flush();
System.out.println("client is"+response);
echoSocket.close();
}
}
import java.io.*;
import java.net.*;
import java.lang.*;
class Server
{
public static void main(String args[]) throws IOException
{
int response2;
double random2;
int key2;
FileInputStream fin=null;
DataInputStream clientin=null;
ServerSocket s= new ServerSocket(2000);
Socket echoSocket=s.accept();
random2=clientin.readDouble();
key2=clientin.readInt();
response2=clientin.readInt();
response2=key2%((int)random2);
System.out.println("server is"+response2);
s.close();
echoSocket.close();
}
}
S'il vous plaît votre code correctement en retrait. Il est impossible de lire de cette façon. –
@JB Nizet: ohh désolé je vais le faire –
@Parth: "Mettre correctement" ne veut pas dire avoir tout avec la même indentation. Votre éditeur/IDE devrait avoir une fonction pour indenter le code, utilisez ceci avant de le copier. (Je l'ai fait maintenant pour vous.) –