Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@enginebai
Created May 24, 2018 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enginebai/6688f694651a03e2173f58cedc9580b8 to your computer and use it in GitHub Desktop.
Save enginebai/6688f694651a03e2173f58cedc9580b8 to your computer and use it in GitHub Desktop.
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