【发布时间】:2014-09-15 17:39:37
【问题描述】:
我试图制作一个控制台聊天服务器。我面临的主要问题是我无法将消息发送到服务器..正如您在我上传的 img 中看到的服务器和客户端已连接。但是当我在客户端输入任何内容时。客户端变得无响应,我必须关闭 cmd 提示符。 我该如何解决这个问题?
是我的电脑有问题还是代码有问题?
public class MyClient
{
Socket s ;
DataInputStream din ;
DataOutputStream dout;
public MyClient()
{
try
{
s= new Socket("localhost",10);
System.out.println(s);
din = new DataInputStream(s.getInputStream());
dout= new DataOutputStream(s.getOutputStream());
ClientChat();
}
catch(Exception e)
{
System.err.println(e);
}
}
public void ClientChat() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//Scanner s2 = new Scanner(System.in);
String s1;
System.out.println("start the conversation");
do
{
s1=br.readLine();
//s1=s2.nextLine();
dout.flush();
System.out.println("server:"+din.readUTF());
}
while(!s1.equals("stop"));
}
public static void main (String args[])
{
new MyClient();
}
}
【问题讨论】:
-
你能分享一个聊天系统在互联网上工作的代码吗?
标签: java networking chat