套接字不读取数据 [英] Socket is not reading data
本文介绍了套接字不读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试一个简单的Java程序来发送和接收数据......服务器正在发送数据而客户端收到了......但这会产生Socket异常:连接重置
这是我发送Integer值的服务器端代码...
尝试
{
ServerSocket server = new ServerSocket( 9089 );
套接字套接字= server.accept();
OutputStream out = socket.getOutputStream();
out.write( 12 );
out.flush();
System.out.println( Data Sent ....);
Thread.sleep( 5000 );
}
catch (例外e)
{
System.out.println( 服务器错误: + e.toString());
}
这是我的客户端代码,用于接收该Int值...
尝试
{
Socket client = new Socket(InetAddress.getLocalHost(), 9089 );
System.out.println( 已连接....);
InputStream in = client.getInputStream();
while (in.available()> 0)
{
System.out.println( 不可用......);
}
System.out.println( 收到: + in。 read());
in.close();
}
catch (例外e)
{
System.out.println( 发件人错误: + e.toString());
}
请帮我解决这个问题....
解决方案
我得到了解决方案...
而不是
while (in.available()> 0)
我需要做的事情
while (in.available()== 0)
I am trying a simple Java program to send and receive data... Server is sending the data and Client receives that... But this generates Socket Exception : Connection Reset
This is my Server side code to sent Integer value...
try
{
ServerSocket server = new ServerSocket(9089);
Socket socket = server.accept();
OutputStream out = socket.getOutputStream();
out.write(12);
out.flush();
System.out.println("Data Sent....");
Thread.sleep(5000);
}
catch(Exception e)
{
System.out.println("Server Error : " + e.toString());
}
This is my Client side code to receive that Int value...
try
{
Socket client = new Socket(InetAddress.getLocalHost(), 9089);
System.out.println("Connected ....");
InputStream in = client.getInputStream();
while(in.available()>0)
{
System.out.println("Unavailable...");
}
System.out.println("Received : " + in.read() );
in.close();
}
catch(Exception e)
{
System.out.println("Sender Error : " + e.toString());
}
Please help me solving this....
解决方案
I got the solution...
Instead of
while(in.available()>0)
I need to do
while(in.available()==0)
这篇关于套接字不读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文