esoe 2 years ago
parent
commit
869775ed59
  1. 13
      src/main/java/ru/molokoin/Connection.java
  2. 10
      src/main/java/ru/molokoin/ServerLauncher.java
  3. BIN
      target/classes/ru/molokoin/Connection.class
  4. BIN
      target/classes/ru/molokoin/ServerLauncher.class

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

@ -13,20 +13,20 @@ public class Connection {
public Connection(){} public Connection(){}
public Connection(SocketChannel accepted){ public Connection(SocketChannel accepted){
this.accepted = accepted; this.accepted = accepted;
this.write(); write();
this.read(); read();
} }
//читаем из подключения //читаем из подключения
public void read(){ public void read(){
try{ try{
RandomAccessFile aFile; //RandomAccessFile aFile;
ByteBuffer buf = ByteBuffer.allocate(1024); ByteBuffer buf = ByteBuffer.allocate(1024);
int bytesRead = accepted.read(buf); int bytesRead = accepted.read(buf);
while (bytesRead != -1){ while (bytesRead != -1){
System.out.println("Read " + bytesRead); System.out.println("Read " + bytesRead);
buf.flip(); buf.flip();
while(buf.hasRemaining()){ while(buf.hasRemaining()){
System.out.print((char) buf.get()); System.out.print((char)buf.get());
//accepted.write(buf); //accepted.write(buf);
} }
buf.clear(); buf.clear();
@ -37,7 +37,7 @@ public class Connection {
e.printStackTrace(); e.printStackTrace();
} }
} }
//пишем в подключение //пишем в подключение из файла
public void write(){ public void write(){
//accepted.write(null); //accepted.write(null);
RandomAccessFile aFile; RandomAccessFile aFile;
@ -53,7 +53,8 @@ public class Connection {
System.out.println("Write " + bytesRead); System.out.println("Write " + bytesRead);
buf.flip(); buf.flip();
while(buf.hasRemaining()){ while(buf.hasRemaining()){
//System.out.print((char) buf.get()); //char c = (char) buf.get();
//System.out.print(c);
accepted.write(buf); accepted.write(buf);
} }
buf.clear(); buf.clear();

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

@ -11,28 +11,28 @@ import java.util.Queue;
* Подключения ставятся в очередь * Подключения ставятся в очередь
*/ */
public class ServerLauncher implements Runnable{ public class ServerLauncher implements Runnable{
private int tcpPort = 8081; private int port = 8081;
private ServerSocketChannel channel = null; private ServerSocketChannel channel = null;
private Queue<Connection> connectionQueue = null; private Queue<Connection> connectionQueue = null;
private boolean isActive = false;//флаг проверки, запущен ли сервер private boolean isActive = false;//флаг проверки, запущен ли сервер
ServerLauncher(int port, Queue<Connection> connectionQueue){ ServerLauncher(int port, Queue<Connection> connectionQueue){
tcpPort = port; this.port = port;
this.connectionQueue = connectionQueue; this.connectionQueue = connectionQueue;
} }
@Override @Override
public void run() { public void run() {
try{ try{
this.channel = ServerSocketChannel.open(); channel = ServerSocketChannel.open();
this.channel.bind(new InetSocketAddress(tcpPort)); channel.bind(new InetSocketAddress(port));
} catch(IOException e){ } catch(IOException e){
e.printStackTrace(); e.printStackTrace();
return; return;
} }
while(true){ while(true){
try{ try{
SocketChannel accepted = this.channel.accept(); SocketChannel accepted = channel.accept();
System.out.println("Socket accepted: " + accepted); System.out.println("Socket accepted: " + accepted);
//todo check if the queue can even accept more sockets. //todo check if the queue can even accept more sockets.
this.connectionQueue.add(new Connection(accepted)); this.connectionQueue.add(new Connection(accepted));

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

Binary file not shown.

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

Binary file not shown.
Loading…
Cancel
Save