Initial commit
This commit is contained in:
BIN
Problem 3.18/SocketClient.class
Normal file
BIN
Problem 3.18/SocketClient.class
Normal file
Binary file not shown.
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) {}
|
||||
}
|
||||
}
|
||||
}
|
BIN
Problem 3.18/SocketServer.class
Normal file
BIN
Problem 3.18/SocketServer.class
Normal file
Binary file not shown.
37
Problem 3.18/SocketServer.java
Normal file
37
Problem 3.18/SocketServer.java
Normal file
@ -0,0 +1,37 @@
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
|
||||
public class SocketServer {
|
||||
public static void main(String[] args) {
|
||||
ServerSocket sock = null;
|
||||
Socket client = null;
|
||||
try {
|
||||
sock = new ServerSocket(6017);
|
||||
client = sock.accept();
|
||||
DataInputStream inputStream = new DataInputStream(client.getInputStream());
|
||||
DataOutputStream outputStream = new DataOutputStream(client.getOutputStream());
|
||||
while (true) {
|
||||
try {
|
||||
String message = inputStream.readUTF();
|
||||
System.out.println("Client: " + message);
|
||||
outputStream.writeUTF(message);
|
||||
} catch (IOException e) {
|
||||
if (e.getMessage().equals("null")) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
inputStream.close();
|
||||
outputStream.close();
|
||||
client.close();
|
||||
sock.close();
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
try {
|
||||
sock.close();
|
||||
client.close();
|
||||
} catch (IOException e) {}
|
||||
System.err.println(ioe);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user