I have implemented a simple chat program in Java . However when I run the code and try to send messages from client I get this as output on Server side
例如:
客户:
服务器:服务器(addr=0.0.0.0/0.0.0.0,port=0, Localport=2000)
I get such a response from the client for any message I send..I am basically working on localhost
谁能帮助解决我的问题?
My java 守则:
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());
int random=(int)(Math.random()*50);
System.out.println(random);
int response=((int)random)%(key);
System.out.println(key);
System.out.println("response generated is "+response);
System.out.println("Authentication begins");
Socket echoSocket = new Socket("127.0.0.1", 2500);
BufferedReader sin=new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
PrintStream sout=new PrintStream(echoSocket.getOutputStream());
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = null;
String s;
DataOutputStream clientout=new DataOutputStream(echoSocket.getOutputStream());
clientout.writeInt(random);
clientout.writeInt(key);
clientout.writeInt(response);
System.out.println("client is"+response);
System.out.println("chat is started");
while (true)
{
System.out.print("Client : ");
s=stdin.readLine();
sout.println(s);
s=sin.readLine();
System.out.print("Server : "+s+"
");
if ( s.equalsIgnoreCase("BYE") )
break;
}
echoSocket.close();
sin.close();
sout.close();
stdin.close();
}
}
class Server
{
public static void main(String args[]) throws IOException
{
int random3=(int)(Math.random()*50);
int response2;
int response3;
int random2;
int key2;
ServerSocket s= new ServerSocket(2500);
Socket echoSocket=s.accept();
DataInputStream clientin=new DataInputStream(echoSocket.getInputStream());
BufferedReader cin=new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
PrintStream cout=new PrintStream(echoSocket.getOutputStream());
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
String s1;
random2=clientin.readInt();
key2=clientin.readInt();
response2=clientin.readInt();
System.out.println(key2);
response3=(random2)%(key2);
System.out.println("server is"+response2);
if(response2==response3)
{
System.out.println("client is authenticated..chat starts");
while (true)
{
s1=cin.readLine();
if (s1.equalsIgnoreCase("END"))
{
cout.println("BYE");
break;
}
System. out.print("Client : "+s+"
");
System.out.print("Server : ");
s1=stdin.readLine();
cout.println(s1);
}
}
s.close();
echoSocket.close();
cin.close();
cout.close();
stdin.close();
}
}