Initial commit

This commit is contained in:
2019-03-17 23:28:35 +08:00
commit 78b57e0eb3
10 changed files with 188 additions and 0 deletions

Binary file not shown.

View 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) {}
}
}
}

Binary file not shown.

View 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);
}
}
}