esoe 2 years ago
parent
commit
869775ed59
  1. 11
      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

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

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

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

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