esoe
2 years ago
5 changed files with 59 additions and 0 deletions
@ -0,0 +1,21 @@ |
|||||||
|
package ru.molokoin; |
||||||
|
|
||||||
|
import java.nio.ByteBuffer; |
||||||
|
import java.nio.channels.SocketChannel; |
||||||
|
|
||||||
|
public class Connection { |
||||||
|
public SocketChannel accepted = null; |
||||||
|
public Connection(){} |
||||||
|
public Connection(SocketChannel accepted){ |
||||||
|
this.accepted = accepted; |
||||||
|
} |
||||||
|
//читаем из подключения
|
||||||
|
public void read(ByteBuffer byteBuffer){ |
||||||
|
//
|
||||||
|
} |
||||||
|
//пишем в подключение
|
||||||
|
public void write(ByteBuffer byteBuffer){ |
||||||
|
//
|
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,38 @@ |
|||||||
|
package ru.molokoin; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.net.InetSocketAddress; |
||||||
|
import java.nio.channels.ServerSocketChannel; |
||||||
|
import java.nio.channels.SocketChannel; |
||||||
|
import java.util.Queue; |
||||||
|
|
||||||
|
/** |
||||||
|
* Поток в котором запускается сервер в режим ожидания подключений. |
||||||
|
* Подключения ставятся в очередь |
||||||
|
*/ |
||||||
|
public class ServerLauncher implements Runnable{ |
||||||
|
private int tcpPort = 0; |
||||||
|
private ServerSocketChannel channel = null; |
||||||
|
private Queue<Connection> connectionQueue = null; |
||||||
|
|
||||||
|
@Override |
||||||
|
public void run() { |
||||||
|
try{ |
||||||
|
this.channel = ServerSocketChannel.open(); |
||||||
|
this.channel.bind(new InetSocketAddress(tcpPort)); |
||||||
|
} catch(IOException e){ |
||||||
|
e.printStackTrace(); |
||||||
|
return; |
||||||
|
} |
||||||
|
while(true){ |
||||||
|
try{ |
||||||
|
SocketChannel accepted = this.channel.accept(); |
||||||
|
System.out.println("Socket accepted: " + accepted); |
||||||
|
//todo check if the queue can even accept more sockets.
|
||||||
|
this.connectionQueue.add(new Connection(accepted)); |
||||||
|
} catch(IOException e){ |
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue