commit
13ba8a3359
@ -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 |
@ -0,0 +1,6 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project version="4"> |
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" project-jdk-name="17" project-jdk-type="JavaSDK"> |
||||
<output url="file://$PROJECT_DIR$/out" /> |
||||
</component> |
||||
</project> |
@ -0,0 +1,8 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project version="4"> |
||||
<component name="ProjectModuleManager"> |
||||
<modules> |
||||
<module fileurl="file://$PROJECT_DIR$/.idea/sockets-java.iml" filepath="$PROJECT_DIR$/.idea/sockets-java.iml" /> |
||||
</modules> |
||||
</component> |
||||
</project> |
@ -0,0 +1,9 @@ |
||||
<?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$" /> |
||||
<orderEntry type="inheritedJdk" /> |
||||
<orderEntry type="sourceFolder" forTests="false" /> |
||||
</component> |
||||
</module> |
@ -0,0 +1,15 @@ |
||||
public class Client { |
||||
public static void main(String[]args){ |
||||
System.out.println("hello from Client"); |
||||
ClientObject client = new ClientObject(); |
||||
client.startConnection("localhost", 10069); |
||||
String answer = client.sendMessage("Hallo from the other side!"); |
||||
|
||||
System.out.println("-----------------------------------"); |
||||
System.out.printf("\t%s\t", answer); |
||||
System.out.println("-----------------------------------"); |
||||
|
||||
client.stopConnection(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,42 @@ |
||||
import java.io.BufferedReader; |
||||
import java.io.InputStreamReader; |
||||
import java.io.PrintWriter; |
||||
import java.net.Socket; |
||||
|
||||
public class ClientObject{ |
||||
private Socket clientSocket; |
||||
private PrintWriter out; |
||||
private BufferedReader in; |
||||
|
||||
public void startConnection(String ip, int port) { |
||||
try{ |
||||
clientSocket = new Socket(ip, port); |
||||
out = new PrintWriter(clientSocket.getOutputStream(), true); |
||||
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); |
||||
}catch(Exception e){ |
||||
System.out.println("Error starting client connection"); |
||||
System.out.println(e); |
||||
} |
||||
} |
||||
|
||||
public String sendMessage(String msg) { |
||||
out.println(msg); |
||||
String resp = ""; |
||||
try{ |
||||
resp = in.readLine(); |
||||
}catch(Exception e){ |
||||
System.out.println("Error reading message"); |
||||
} |
||||
return resp; |
||||
} |
||||
|
||||
public void stopConnection() { |
||||
try{ |
||||
in.close(); |
||||
out.close(); |
||||
clientSocket.close(); |
||||
}catch(Exception e){ |
||||
System.out.println("Error closing connection"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,13 @@ |
||||
package server; |
||||
|
||||
public class Server { |
||||
|
||||
public static void main(String[] args) { |
||||
System.out.println("Hello from Server"); |
||||
|
||||
|
||||
Service s = new Service(); |
||||
s.start(10069); |
||||
|
||||
} |
||||
} |
@ -0,0 +1,48 @@ |
||||
package server; |
||||
|
||||
import java.io.BufferedReader; |
||||
import java.io.InputStreamReader; |
||||
import java.io.PrintWriter; |
||||
import java.net.ServerSocket; |
||||
import java.net.Socket; |
||||
|
||||
public class Service { |
||||
|
||||
|
||||
private ServerSocket serverSocket; |
||||
private Socket clientSocket; |
||||
private PrintWriter out; |
||||
private BufferedReader in; |
||||
|
||||
public void start(int port) { |
||||
try{ |
||||
serverSocket = new ServerSocket(port); |
||||
clientSocket = serverSocket.accept(); |
||||
out = new PrintWriter(clientSocket.getOutputStream(), true); |
||||
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); |
||||
String greeting = in.readLine(); |
||||
if ("Hallo from the other side!".equals(greeting)) { |
||||
out.println("hello client"); |
||||
} |
||||
else { |
||||
out.println("unrecognised greeting"); |
||||
} |
||||
}catch(Exception e){ |
||||
System.out.println("Error starting"); |
||||
System.out.println(e); |
||||
} |
||||
} |
||||
|
||||
public void stop() { |
||||
try{ |
||||
in.close(); |
||||
out.close(); |
||||
clientSocket.close(); |
||||
serverSocket.close(); |
||||
}catch(Exception e){ |
||||
System.out.println("Error stopping"); |
||||
System.out.println(e); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue