Created
May 24, 2018 00:21
-
-
Save enginebai/6688f694651a03e2173f58cedc9580b8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.util.Scanner; | |
public class Client { | |
public static void main(String[] args) throws IOException { | |
String host = ""; | |
int port = 5987; | |
Socket socket = null; | |
Scanner consoleInput = new Scanner(System.in); | |
System.out.println("請輸入Server端位址"); | |
host = consoleInput.nextLine(); | |
try { | |
socket = new Socket(host, port); | |
DataInputStream input = null; | |
DataOutputStream output = null; | |
try { | |
input = new DataInputStream(socket.getInputStream()); | |
output = new DataOutputStream(socket.getOutputStream()); | |
while (true) { | |
System.out.println(input.readUTF()); | |
break; | |
} | |
} | |
catch (IOException e) { | |
} | |
finally { | |
if (input != null) | |
input.close(); | |
if (output != null) | |
output.close(); | |
} | |
} | |
catch (IOException e) { | |
e.printStackTrace(); | |
} | |
finally { | |
if (socket != null) | |
socket.close(); | |
if (consoleInput != null) | |
consoleInput.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment