[英]Basic Chat Program with Java sockets, Client not receiving messages from Server
[英]Client not able to send messages to server in java chat program
我用Java实现了一个简单的聊天程序。 但是当我运行代码并尝试从客户端发送消息时,我将其作为服务器端的输出
例如:
客户:嗨
服务器:ServerSocket [addr = 0.0.0.0 / 0.0.0.0,port = 0,localport = 2000]
对于我发送的任何消息,我从客户端得到了这样的响应。我基本上是在使用localhost
任何人都可以帮我解决问题吗?
我的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+"\n");
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+"\n");
System.out.print("Server : ");
s1=stdin.readLine();
cout.println(s1);
}
}
s.close();
echoSocket.close();
cin.close();
cout.close();
stdin.close();
}
}
没有显示任何代码,几乎无法分辨。 请发一些。
但是,地址0.0.0.0
看起来很可疑。 如果您正在使用localhost
,请尝试127.0.0.1
。
您得到输出,因为您使用了错误的变量。 您应该从服务器打印的变量是s1而不是s。
变量s指的是套接字,这就是你获取套接字信息而不是客户端响应的原因
s1=cin.readLine();
if (s1.equalsIgnoreCase("END"))
{
cout.println("BYE");
break;
}
System. out.print("Client : "+s1+"\n"); // note that this should be s1 and not s
作为一种好的做法,您应该清楚地命名变量,以便您和其他人可以随时阅读代码。 有了s,s1等等,只会随着代码变大而让你感到困惑。 这也是一个很好的习惯,让与你合作的其他工程师更快乐:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.