Java SocketChannel未使用小程序接收数据
我有一段代码,当我从一个独立的java应用程序调用它时,它工作得很好,因为我可以连接到服务器并成功地从服务器发送和接收数据 但是,当我在小程序中使用相同的代码时,我可以连接和发送数据,但无法接收数据,并且在服务器或客户端上都没有收到任何错误消息 它们都连接到同一个服务器应用程序,因此我消除了服务器的问题 我已授予小程序的所有权限 非常感谢你的帮助 主应用程序代码Java SocketChannel未使用小程序接收数据,java,applet,Java,Applet,我有一段代码,当我从一个独立的java应用程序调用它时,它工作得很好,因为我可以连接到服务器并成功地从服务器发送和接收数据 但是,当我在小程序中使用相同的代码时,我可以连接和发送数据,但无法接收数据,并且在服务器或客户端上都没有收到任何错误消息 它们都连接到同一个服务器应用程序,因此我消除了服务器的问题 我已授予小程序的所有权限 非常感谢你的帮助 主应用程序代码 公共类NewJFrame扩展了javax.swing.JFrame{ /** * Creates new form NewJFram
公共类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();
}
小程序代码
您的客户端读取的代码不正确
当只有一个连接时,我真的不明白为什么要在客户机中使用NIO。真的没有任何好处。我会让它在客户机中与java.net一起工作,然后看看您是否有任何理由更改它。1.我已经关闭了频道,但仍然没有得到任何响应。2.读取/翻转在独立应用程序上工作得很好,因此在小程序上不工作。我使用NIO,因为我将使用小程序将此解决方案部署到大约4000个用户read/flip没有任何作用,所以我不知道“完美工作”可能意味着什么。这是毫无意义和毫无意义的。如果只想忽略刚刚读取的数据,请使用clear()。flip()用于当您将要取出数据时,它后面应该总是紧跟着compact()或clear()。终于成功了。我所做的唯一更改是return语句继续。我将
if(selector.select(5000)==0)return
替换为if(selector.select(5000)==0)continue
仍然不理解为什么相同的代码在Applet和应用程序上表现不同。
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();
}
}
try
{
new JavaApplication1();
}
catch(Exception ex)
{
ex.printStackTrace();
}