added BudaClientServerStuff, an example for some client server functionality.
This commit is contained in:
parent
eeb259af92
commit
687e68be6c
8
Übung/BudaServerClientStuff/.idea/.gitignore
generated
vendored
Normal file
8
Übung/BudaServerClientStuff/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
6
Übung/BudaServerClientStuff/.idea/misc.xml
generated
Normal file
6
Übung/BudaServerClientStuff/.idea/misc.xml
generated
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
Übung/BudaServerClientStuff/.idea/modules.xml
generated
Normal file
8
Übung/BudaServerClientStuff/.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/BudaServerClientStuff.iml" filepath="$PROJECT_DIR$/BudaServerClientStuff.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
11
Übung/BudaServerClientStuff/BudaServerClientStuff.iml
Normal file
11
Übung/BudaServerClientStuff/BudaServerClientStuff.iml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
10
Übung/BudaServerClientStuff/README.txt
Normal file
10
Übung/BudaServerClientStuff/README.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
This is a demo of some (very basic) client / server functionality.
|
||||||
|
Run BudaClient.java for the Client and BudaServer.java for the Server. Everything connects locally via port 8090
|
||||||
|
|
||||||
|
Type "Name:Jonas B." to change the client's name to Jonas B (the "." is where the name stops).
|
||||||
|
|
||||||
|
Type "Names" for the Server to display everyone who is connected along with their name (if they have set a name).
|
||||||
|
|
||||||
|
Typing "Quitx" should quit everything but I havent quite managed to implement that yet so just repeatedly use ctrl+c to quit everything.
|
||||||
|
|
||||||
|
-Jonas
|
||||||
BIN
Übung/BudaServerClientStuff/src/BudaClient.class
Normal file
BIN
Übung/BudaServerClientStuff/src/BudaClient.class
Normal file
Binary file not shown.
29
Übung/BudaServerClientStuff/src/BudaClient.java
Normal file
29
Übung/BudaServerClientStuff/src/BudaClient.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class BudaClient {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Socket sock = null;
|
||||||
|
try {
|
||||||
|
sock = new Socket("localhost", 8090);
|
||||||
|
OutputStream out= sock.getOutputStream();
|
||||||
|
BufferedReader conin = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
String line = "";
|
||||||
|
while (true) {
|
||||||
|
line = conin.readLine();
|
||||||
|
out.write(line.getBytes());
|
||||||
|
if (line.equalsIgnoreCase("Quitx")) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
//line.startsWith() //todo: automatically handle name lengths
|
||||||
|
//TODO: Implement inputStream in.
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
BIN
Übung/BudaServerClientStuff/src/BudaClientThread.class
Normal file
BIN
Übung/BudaServerClientStuff/src/BudaClientThread.class
Normal file
Binary file not shown.
73
Übung/BudaServerClientStuff/src/BudaClientThread.java
Normal file
73
Übung/BudaServerClientStuff/src/BudaClientThread.java
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class BudaClientThread implements Runnable {
|
||||||
|
int number;
|
||||||
|
Socket socket;
|
||||||
|
String name;
|
||||||
|
|
||||||
|
|
||||||
|
public BudaClientThread(int number, Socket socket) {
|
||||||
|
this.number = number;
|
||||||
|
this.socket = socket;
|
||||||
|
name = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
System.out.println("Connection " + number + " established.");
|
||||||
|
try {
|
||||||
|
InputStream in = socket.getInputStream();
|
||||||
|
OutputStream out = socket.getOutputStream();
|
||||||
|
byte[] command;
|
||||||
|
String comString;
|
||||||
|
while (true) {
|
||||||
|
command = new byte[5];
|
||||||
|
System.out.println("Waiting to receive a line");
|
||||||
|
in.read(command);
|
||||||
|
System.out.println("Got a line!");
|
||||||
|
comString = new String(command);
|
||||||
|
System.out.println("Client number " + number + " sent this message: " + comString);
|
||||||
|
if (comString.equalsIgnoreCase("Quitx")) {
|
||||||
|
BudaServer.quit = true;
|
||||||
|
System.out.println("I just set quit to true!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//todo: do as switch.
|
||||||
|
|
||||||
|
if (comString.equalsIgnoreCase("NAME:")) { //todo: implement these as methods?
|
||||||
|
setName(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (comString.equalsIgnoreCase("NAMES")) {
|
||||||
|
printnames();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printnames() {
|
||||||
|
for (BudaClientThread t: BudaServer.Clients) {
|
||||||
|
System.out.println(t.name + " connected (#" + t.number + ")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(InputStream in) throws IOException {
|
||||||
|
//byte[] namebyte = new byte[0];
|
||||||
|
String nameString = "";
|
||||||
|
int i;
|
||||||
|
while (true) {
|
||||||
|
i = in.read();
|
||||||
|
if (i == 46) break;
|
||||||
|
nameString = nameString + (char) i;
|
||||||
|
}
|
||||||
|
this.name = nameString;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
BIN
Übung/BudaServerClientStuff/src/BudaServer.class
Normal file
BIN
Übung/BudaServerClientStuff/src/BudaServer.class
Normal file
Binary file not shown.
25
Übung/BudaServerClientStuff/src/BudaServer.java
Normal file
25
Übung/BudaServerClientStuff/src/BudaServer.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
public class BudaServer {
|
||||||
|
public static boolean quit = false; //todo: meaningfully implement this
|
||||||
|
public static HashSet<BudaClientThread> Clients = new HashSet<BudaClientThread>();
|
||||||
|
static int connections = 0;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ServerConnector ServC = new ServerConnector();
|
||||||
|
Thread ServCThread = new Thread(ServC);
|
||||||
|
ServCThread.start();
|
||||||
|
System.out.println("Server has entered its main loop");
|
||||||
|
while (!quit) {
|
||||||
|
//Main server stuff goes here
|
||||||
|
}
|
||||||
|
//ServCThread.stop(); //todo: find some alternative for this.
|
||||||
|
System.out.println("stopping the main BudaServer thread.");
|
||||||
|
System.out.println("Quitting after the next connection is made.");
|
||||||
|
}
|
||||||
|
}
|
||||||
25
Übung/BudaServerClientStuff/src/ClientListener.java
Normal file
25
Übung/BudaServerClientStuff/src/ClientListener.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import java.io.*;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class ClientListener implements Runnable{
|
||||||
|
private final Socket sock;
|
||||||
|
|
||||||
|
public ClientListener(Socket sock) {
|
||||||
|
this.sock = sock;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run(){
|
||||||
|
byte[] command = new byte[5];
|
||||||
|
String comString;
|
||||||
|
|
||||||
|
try {
|
||||||
|
InputStream in = sock.getInputStream();
|
||||||
|
in.read(command);
|
||||||
|
System.out.println("Got a line!");
|
||||||
|
comString = new String(command);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
Übung/BudaServerClientStuff/src/ServerConnector.class
Normal file
BIN
Übung/BudaServerClientStuff/src/ServerConnector.class
Normal file
Binary file not shown.
27
Übung/BudaServerClientStuff/src/ServerConnector.java
Normal file
27
Übung/BudaServerClientStuff/src/ServerConnector.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import java.io.IOException;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class ServerConnector implements Runnable{
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
System.out.println(
|
||||||
|
"Warte auf Verbindungen auf Port 8090...");
|
||||||
|
ServerSocket servSock = new ServerSocket(8090);
|
||||||
|
while (true) {
|
||||||
|
Socket socket = servSock.accept();
|
||||||
|
System.out.println("got a connection: socket " + BudaServer.connections + socket.toString());
|
||||||
|
BudaClientThread newClientThread = new BudaClientThread(++BudaServer.connections, socket);
|
||||||
|
BudaServer.Clients.add(newClientThread);
|
||||||
|
Thread bCT = new Thread(newClientThread);
|
||||||
|
bCT.start();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("server got an error");
|
||||||
|
System.err.println(e);
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user