相关疑难解决方法(0)

我应该如何使用AsynchronousServerSocketChannel接受连接?

我想用Java 7和NIO 2编写一个异步服务器.

但是我应该如何使用AsynchronousServerSocketChannel?

例如,如果我开始:

final AsynchronousServerSocketChannel server = 
    AsynchronousServerSocketChannel.open().bind(
        new InetSocketAddress(port));
Run Code Online (Sandbox Code Playgroud)

然后,当我这样做时server.accept(),程序终止,因为该调用是异步的.如果我将该代码置于无限循环中,AcceptPendingException则抛出一个.

有关如何编写简单异步服务器的任何建议AsynchronousServerSocketChannel?

这是我的完整示例(类似于JavaDoc中的示例):

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

public class AsyncServer {

    public static void main(String[] args) {
        int port = 8060;
        try {
            final AsynchronousServerSocketChannel server = 
                    AsynchronousServerSocketChannel.open().bind(
                            new InetSocketAddress(port));

            System.out.println("Server listening on " + port);

            server.accept("Client connection", 
                    new CompletionHandler<AsynchronousSocketChannel, Object>() {
                public void completed(AsynchronousSocketChannel ch, Object att) {
                    System.out.println("Accepted …
Run Code Online (Sandbox Code Playgroud)

java asynchronous nio java-7

19
推荐指数
1
解决办法
1万
查看次数

下载视频和播放

我想实现在线视频播放功能并下载它.我的意思是应该使用相同的下载流进行下载和播放,以便保存视频以供离线使用,并防止分别播放和下载两倍的数据费用.

到目前为止,我已经实现了视频下载asyncTask和播放OnPostExecute.以下是代码:

public class MainActivity extends AppCompatActivity {


private Button btnPlay;
private MediaPlayer player;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    outFilePath = getExternalFilesDir("/") + "/video.mp4";
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    prepareVideoView();

}

private VideoView videoView;
String videoPath = "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_5mb.mp4";
String outFilePath = "";//

private void prepareVideoView() {
    MediaController …
Run Code Online (Sandbox Code Playgroud)

android

11
推荐指数
2
解决办法
8018
查看次数

如何在java中使接受的套接字无阻塞

我接受来自客户端的连接,然后将连接的套接字传递给另一个对象,但是,该套接字需要是非阻塞的.我正在尝试使用,getChannel().configureBlocking(false)但似乎没有用.它需要是非阻塞的,因为下面的方法每100ms调用一次.还有其他方法我应该做这个非阻塞?谢谢你的帮助!

public void checkForClients() {
  DataOutputStream out;
  DataInputStream in;
  Socket connection;
  InetAddress tempIP;
  String IP;

  try {
     connection = serverSocket.accept();
     connection.getChannel().configureBlocking(false);

     System.err.println("after connection made");

     in = new DataInputStream(connection.getInputStream());
     out = new DataOutputStream(connection.getOutputStream());
     tempIP = connection.getInetAddress();
     IP = tempIP.toString();

     System.err.println("after ip string");

     // create a new user ex nihilo
     connectedUsers.add(new ConnectedUser(IP, null, connection, in, out));


     System.err.println("after add user");
  } catch (SocketTimeoutException e) {
     System.err.println("accept timeout - continuing execution");
  } catch (IOException e) {
     System.err.println("socket accept failed");
  }
}
Run Code Online (Sandbox Code Playgroud)

java sockets nonblocking

5
推荐指数
2
解决办法
1万
查看次数

如何使用 java.net.ServerSocket 持续监听端口

我有一个java.net.ServerSocket用于侦听连接。我正在使用它的accept()方法来从客户端获取连接,然后适当地处理它们。我希望不断倾听客户的声音,永远不要让他们无法使用。目前我有类似的代码:

ServerSocket serverSocket = ...
while (shouldBeListening) {
    handleClient(serverSocket.accept());
}
Run Code Online (Sandbox Code Playgroud)

该handleClient方法可能需要少量时间(小于十分之一毫秒)。我担心在ServerSocket.accept()方法返回和再次调用之间可能会错过连接请求。解决此问题的最佳方法是什么?

编辑:我实现它的方式目前在handleClient方法中创建了一个新线程,但即使这样也需要时间(特别是因为这是在Raspberry Pi上运行的),我担心如果handleClient在执行方法时请求连接那么它可能会被拒绝,因为accept()没有被运行。

java network-programming serversocket

2
推荐指数
1
解决办法
3912
查看次数