套接字和Java进程 [英] Sockets and Processes in Java

查看:107
本文介绍了套接字和Java进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,什么将最好的办法是有一个不断监听端口开放,并且还发在收到一个数据包。我不是跟目前网络编程尤其精通,所以我在网上找到的教程是不是特别有帮助。

In Java, what would the best way be to have a constantly listening port open, and still send upon receipt of a packet. I am not particularly savvy with network programming at the moment, so the tutorials I have found on the net aren't particularly helpful.

难道有意义有监听套接字作为一个ServerSocket,并在一个单独的线程的socket运行它我使用将数据发送到服务器?

Would it make sense to have the listening socket as a serversocket and run it in a separate thread to the socket I'm using to send data to the server?

在一个松散相关的问题。有谁知道,如果编程简单对于Java,在NetBeans然后将其导出上使用黑莓(使用插件)的插座,仍能正常工作?

In a loosely related question. Does anyone know if programming simply for java, in netbeans then exporting it for use on a blackberry (using a plugin) the sockets would still work ?

推荐答案

如果你能负担得起的线程,试试这个(记住,我离开了像异常处理的一些细节和播放尼斯线程)。你可能想看看SocketChannels和/或NIO异步插座/选择器。这应该让你开始。

If you can afford the threading, try this (keep in mind I've left out some details like exception handling and playing nice with threads). You may want to look into SocketChannels and/or NIO async sockets / selectors. This should get you started.

boolean finished = false;
int port = 10000;
ServerSocket server = new ServerSocket(port);

while(!finished)
{
    // This will block until a connection is made
    Socket s = server.accept();
    // Spawn off some thread (or use a thread pool) to handle this socket
    // Server will continue to listen
}

这篇关于套接字和Java进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆