esoe 2 years ago
parent
commit
1d71ea4752
  1. 21
      src/main/java/ru/molokoin/Connection.java
  2. 38
      src/main/java/ru/molokoin/ServerLauncher.java
  3. BIN
      target/classes/ru/molokoin/Connection.class
  4. BIN
      target/classes/ru/molokoin/ExchangeServer.class
  5. BIN
      target/classes/ru/molokoin/ServerLauncher.class

21
src/main/java/ru/molokoin/Connection.java

@ -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){
//
}
}

38
src/main/java/ru/molokoin/ServerLauncher.java

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

BIN
target/classes/ru/molokoin/Connection.class

Binary file not shown.

BIN
target/classes/ru/molokoin/ExchangeServer.class

Binary file not shown.

BIN
target/classes/ru/molokoin/ServerLauncher.class

Binary file not shown.
Loading…
Cancel
Save