[英]SocketChannel not receiving data with Applet
我有这段代码,当我从独立的Java应用程序调用时,它可以很好地工作,因为我可以连接到服务器,并成功地从服务器发送和接收数据。
但是,当我在applet中使用相同的代码时,我可以连接并发送数据,但不能接收数据,并且在服务器或客户端上都不会收到任何错误消息。
它们都连接到同一服务器应用程序,因此消除了服务器问题。
我已授予Applet所有权限
我们将高度赞赏您的帮助主应用代码
公共类NewJFrame扩展了javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
List<String> bb=new ArrayList<String>();
bb.add("Customer");
bb.add("ID");
byte [] serialized=ECSStreamUtil.serializeObject(bb);
ByteBuffer toSend=ByteBuffer.allocate(serialized.length);
toSend.put(serialized);
toSend.flip();
JavaApplication1.write(toSend);
toSend.clear();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String args[]) throws Exception{
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
new JavaApplication1();
package javaapplication1;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
public class JavaApplication1 {
static int x;
private static SocketChannel client ;
public JavaApplication1()throws Exception
{
client = SocketChannel.open();
// nonblocking I/O
client.configureBlocking(false);
// Connection to host port 4444
client.connect(new java.net.InetSocketAddress("localhost",4444));
// Create selector
Selector selector = Selector.open();
// Record to selector (OP_CONNECT type)
SelectionKey clientKey = client.register(selector, SelectionKey.OP_CONNECT);
// Waiting for the connection
while (true)
{
if(selector.select(5000)==0)return ;
// Get keys
Set keys = selector.selectedKeys();
Iterator i = keys.iterator();
// For each key...
while (i.hasNext())
{
SelectionKey key = (SelectionKey)i.next();
// Remove the current key
i.remove();
// Get the socket channel held by the key
SocketChannel channel = (SocketChannel)key.channel();
if(!channel.finishConnect())
return;
if(key.isConnectable())
{
SocketChannel sc=(SocketChannel)key.channel();
sc.register(selector,SelectionKey.OP_READ);
System.out.println("conne");
continue;
}
if(key.isReadable())
{
ByteBuffer buf=ByteBuffer.allocate(89);
int x=channel.read(buf);
if(x==-1)
{
key.cancel();
continue;
}
while((channel.read(buf)>0))
{
buf.flip();
}
byte c[]=buf.array();
System.out.println(new String(c));
//buf.clear();
}
}
keys.clear();
}
}
public static void write(ByteBuffer data)
{
try
{
client.write(data);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
List<String> bb=new ArrayList<String>();
bb.add("Customer");
bb.add("ID");
byte [] serialized=ECSStreamUtil.serializeObject(bb);
ByteBuffer toSend=ByteBuffer.allocate(serialized.length);
toSend.put(serialized);
toSend.flip();
JavaApplication1.write(toSend);
//Send information
}
catch(Exception io)
{
io.printStackTrace();
}
}
我正在从applet的init方法调用此代码
try
{
new JavaApplication1();
}
catch(Exception ex)
{
ex.printStackTrace();
}
您的客户读取代码不正确。
如果read()返回-1,则您应该关闭通道,而不仅仅是取消键。
您的读取/翻转循环是胡说八道。 您应该阅读直到获得完整的消息为止,但是您可以确定该消息, 然后翻转并从缓冲区中获取数据。
当只有一个连接时,我真的看不到为什么在客户端中完全使用NIO。 真正的零收益。 我将使其与客户端中的java.net一起使用,然后查看您是否有任何理由对其进行更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.