SocketChannel没有使用Applet接收数据

时间:2014-01-29 07:19:43

标签: java 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(); 

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();
}

1 个答案:

答案 0 :(得分:0)

您的客户阅读代码不正确。

  1. 如果read()返回-1,则应关闭频道,而不是取消该键。

  2. 您的读取/翻转循环是无意义的。您应该阅读,直到您有完整的消息,但是您确定,然后翻转并从缓冲区获取数据。

  3. 当只有一个连接时,我真的不明白为什么你在客户端使用NIO。实惠零利益。我会在客户端使用java.net,然后看看你是否有任何理由改变它。

相关问题