Initial commit
This commit is contained in:
35
Problem 3.18/SocketClient.java
Normal file
35
Problem 3.18/SocketClient.java
Normal file
@ -0,0 +1,35 @@
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class SocketClient {
|
||||
public static void main(String[] args) {
|
||||
Socket sock = null;
|
||||
Scanner scanner = null;
|
||||
try {
|
||||
sock = new Socket("127.0.0.1", 6017);
|
||||
scanner = new Scanner(System.in);
|
||||
DataOutputStream outputStream = new DataOutputStream(sock.getOutputStream());
|
||||
DataInputStream inputStream = new DataInputStream(sock.getInputStream());
|
||||
while (true) {
|
||||
try {
|
||||
System.out.print("Input: ");
|
||||
String message = scanner.nextLine();
|
||||
outputStream.writeUTF(message);
|
||||
System.out.println("Echo: " + inputStream.readUTF());
|
||||
} catch (IOException e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
}
|
||||
finally {
|
||||
try {
|
||||
scanner.close();
|
||||
sock.close();
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user