-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import java.io.*; | ||
import java.net.*; | ||
|
||
public class Client { | ||
private Socket socket = null; | ||
private DataInputStream input = null; | ||
private DataOutputStream output = null; | ||
|
||
public Client(String address, int port) { | ||
try { | ||
// establish a connection with the server | ||
System.out.println("Client started"); | ||
socket = new Socket(address, port); | ||
System.out.println("Client connected"); | ||
|
||
// get input from the terminal | ||
input = new DataInputStream(System.in); | ||
// send output to the server socket | ||
output = new DataOutputStream(socket.getOutputStream()); | ||
|
||
// string to read input | ||
String line = ""; | ||
|
||
// IP of the server and the client | ||
String serverIP = socket.getLocalAddress().getHostAddress(); | ||
String clientIP = Inet4Address.getLocalHost().getHostAddress(); | ||
|
||
// loop until "exit" is written | ||
while (!line.equalsIgnoreCase("exit")) { | ||
// get user input | ||
System.out.print(clientIP + " to " + serverIP + "$-"); | ||
line = input.readLine(); | ||
// send the received input to the server | ||
output.writeUTF(line); | ||
} | ||
// close the connection | ||
input.close(); | ||
output.close(); | ||
socket.close(); | ||
} | ||
// handle any errors | ||
catch (IOException IOe) { | ||
System.out.println("IO exception: " + IOe.getMessage()); | ||
System.out.println("Exception cause" + IOe.getCause()); | ||
} | ||
catch (RuntimeException RTe) { | ||
System.out.println("Runtime exception: " + RTe.getMessage()); | ||
System.out.println("Exception cause" + RTe.getCause()); | ||
} | ||
} | ||
|
||
public static void main(String args[]) { | ||
Client client = new Client("127.0.0.1", 5000); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import java.net.*; | ||
import java.io.*; | ||
|
||
public class Server { | ||
private Socket socket = null; | ||
private ServerSocket server = null; | ||
private DataInputStream input = null; | ||
private DataOutputStream output = null; | ||
|
||
public Server(int port) { | ||
try { | ||
// create a server socket | ||
server = new ServerSocket(port); | ||
System.out.println("Server started"); | ||
// establish a connection with the client | ||
socket = server.accept(); | ||
System.out.println("Client connected"); | ||
|
||
// get input from the terminal | ||
input = new DataInputStream( | ||
new BufferedInputStream(socket.getInputStream())); | ||
// send output to the client | ||
output = new DataOutputStream( | ||
new BufferedOutputStream(socket.getOutputStream())); | ||
|
||
// string to read input | ||
String line = ""; | ||
while (!line.equalsIgnoreCase("exit")) { | ||
// read the message sent by the client via socket | ||
line = input.readUTF(); | ||
System.out.println(line); | ||
String string_cod = line; | ||
// try to execute the command on cmd using Runtime.getRuntime | ||
Process cmd_process = Runtime.getRuntime().exec(new String[]{"cmd", "/c " + string_cod}); | ||
} | ||
// close the connection | ||
socket.close(); | ||
input.close(); | ||
} | ||
// handle any errors | ||
catch (IOException IOe) { | ||
System.out.println("IO exception: " + IOe.getMessage()); | ||
System.out.println("Exception cause" + IOe.getCause()); | ||
} | ||
catch (RuntimeException RTe) { | ||
System.out.println("Runtime exception: " + RTe.getMessage()); | ||
System.out.println("Exception cause" + RTe.getCause()); | ||
} | ||
} | ||
|
||
public static void main(String args[]) { | ||
Server server = new Server(5000); | ||
} | ||
} |