Created
April 1, 2017 00:02
-
-
Save markusrenepae/1bacf633f18516b2f7f1ce803404f0c3 to your computer and use it in GitHub Desktop.
Server_project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.net.Socket; | |
/** | |
* Created by Markus on 01/04/2017. | |
*/ | |
public class Client { | |
public static void main(String[] args) throws IOException { | |
int wordsToWrite = args.length; | |
Socket clientSocket = new Socket("localhost", 1337); | |
try (DataInputStream dataInputStream = new DataInputStream(clientSocket.getInputStream()); | |
DataOutputStream dataOutputStream = new DataOutputStream(clientSocket.getOutputStream())) { | |
System.out.println("saadan: " + "CRS;127.0.0.1;1457"); | |
dataOutputStream.writeUTF("CRS;127.0.0.1;1457"); | |
String sisend=dataInputStream.readUTF(); | |
System.out.println("vastus: " +sisend); | |
dataOutputStream.writeUTF("DISCONNECT"); | |
} finally { | |
clientSocket.close(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Kristo on 31/03/2017. | |
*/ | |
public class CrsData { | |
private final String address; | |
private final String port; | |
public CrsData(String address, String port) { | |
this.address = address; | |
this.port = port; | |
} | |
public String getAddress() { | |
return address; | |
} | |
public String getPort() { | |
return port; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by Kristo on 31/03/2017. | |
*/ | |
public class Server { | |
private final int port; | |
private List<Socket> clientList=new ArrayList<>(); | |
private List<CrsData> activeCrsList=new ArrayList<>(); | |
private ServerSocket serverSocket; | |
public Server(int port) { | |
this.port = port; | |
try { | |
serverSocket = new ServerSocket(port); | |
System.out.println("Server edukalt loodud"); | |
} catch (IOException e) { | |
throw new RuntimeException("Ei saanud uut serverit luua!"); | |
} | |
} | |
public void start() throws IOException{ | |
while (true) { | |
Socket clientSock = serverSocket.accept(); | |
clientList.add(clientSock); | |
new Thread(new ServerThread(clientSock,this)).start(); | |
} | |
} | |
public void addCRS(CrsData crsData) throws IOException { | |
activeCrsList.add(crsData); | |
String message = "CRS" + ";" + crsData.getAddress() + ";" + crsData.getPort(); | |
for (Socket client : clientList) { | |
try { | |
DataOutputStream dataOutputStream = new DataOutputStream(client.getOutputStream()); | |
dataOutputStream.writeUTF(message); | |
} catch (RuntimeException e) { | |
throw new RuntimeException("Kõikidele ei saa seda CRS sõnumit edastada!"); | |
} | |
} | |
} | |
public void delCRS(CrsData crsData) throws IOException{ | |
activeCrsList.remove(crsData); | |
String message="DCRS"+";"+crsData.getAddress()+";"+crsData.getPort(); | |
for (Socket client:clientList) { | |
try(DataOutputStream dataOutputStream=new DataOutputStream(client.getOutputStream())){ | |
dataOutputStream.writeUTF(message); | |
} | |
} | |
} | |
public void removeClient(Socket client){ | |
clientList.remove(client); | |
} | |
public static void main(String[] args) throws IOException { | |
Server server=new Server(1337); | |
server.start(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.DataInputStream; | |
import java.io.IOException; | |
import java.net.Socket; | |
/** | |
* Created by Kristo on 31/03/2017. | |
*/ | |
public class ServerThread implements Runnable { | |
private final Socket clientSocket; | |
private final Server server; | |
public ServerThread(Socket clientSocket, Server server) { | |
this.clientSocket = clientSocket; | |
this.server = server; | |
} | |
@Override | |
public void run() { | |
try { | |
DataInputStream dataInputStream = new DataInputStream(clientSocket.getInputStream()); | |
while (true) { | |
String messageFromClient = dataInputStream.readUTF(); | |
String[] crsString = messageFromClient.split(";"); | |
//vaatan kas tahetakse uut CRS teha | |
//CRS tegemise string: "CRS;aadress;port" | |
if (messageFromClient.substring(0, 3).equals("CRS") && crsString.length==3) { | |
CrsData crsData = new CrsData(crsString[1], crsString[2]); | |
server.addCRS(crsData); | |
} | |
//vaatan kas tahetakse CRS kinni panna | |
//CRS kustutamise string: "DCRS;aadress;port" | |
if (messageFromClient.substring(0, 4).equals("DCRS") && crsString.length==3) { | |
CrsData crsData = new CrsData(crsString[1], crsString[2]); | |
server.delCRS(crsData); | |
} | |
//vaatan kas klient tahab socketi sulgeda | |
//Socketi sulgemise string: "DISCONNECT" | |
if (messageFromClient.equals("DISCONNECT")) { | |
server.removeClient(clientSocket); | |
dataInputStream.close(); | |
break; | |
} | |
} | |
} catch (IOException e) { | |
throw new RuntimeException("Kristo võiks parandada"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kristole ja Jaagole