package chapter19;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressEx {
public static void main(String[] args) {
try {
// getByName메서드로 InetAddress 객체 생성
InetAddress ip = InetAddress.getByName("www.google.co.kr");
System.out.println("hostname : " + ip.getHostName());
System.out.println("ip : " + ip.getHostAddress());
System.out.println("=============================");
// getLocalHost 메서드로 InetAddress 객체 생성 호출
InetAddress local = InetAddress.getLocalHost();
System.out.println("내컴퓨터 IP: " + local);
} catch (UnknownHostException e) {
System.out.println(e.getMessage());
}
}
}
package chapter19;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
public class URLEx {
public static void main(String[] args) {
try {
URL url = null;
url = new URL("https://www.egovframe.go.kr/EgovIntro.jsp?menu=1&submenu=1");
System.out.println("authority : " + url.getAuthority());
System.out.println("content : " + url.getContent());
System.out.println("protocol : " + url.getProtocol());
System.out .println("host : " + url.getHost());
System.out.println("port : " + url.getPort());
System.out.println("path : " + url.getPath());
System.out.println("file : " + url.getFile());
System.out.println("query : " + url.getQuery());
// URL 을 통해서 정보 받기
int data = 0;
try {
Reader is = new InputStreamReader(url.openStream());
while ((data = is.read()) != -1) {
System.out.print((char) data);
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
package chapter19;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
public static void main(String[] args) {
try {
ServerSocket socket = new ServerSocket(5000);
System.out.println("== Server Running!! ==");
while (true) {
Socket s = socket.accept();
InputStream is = s.getInputStream(); // 소켓과 스트림연결
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
System.out.println("== Client Connected!! ==");
System.out.println(">> " + br.readLine());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
메세지 한번 보내기
package chapter19;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
public class TCPClient {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 5000);
System.out.println("== Client Running!! ==");
OutputStream os = s.getOutputStream(); // 소켓과 스트림연결
OutputStreamWriter isr = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(isr);
pw.println("hi~~~hello");
pw.close();
s.close();
System.out.println("== Client Closed!! ==");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
메세지 반복 보내기
package chapter19;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class TCPClientLoop {
public static void main(String[] args) {
try {
System.out.println("== Client Running!! ==");
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("msg >> ");
String msg = sc.nextLine();
if(msg.equals("q")) break;
Socket s = new Socket("127.0.0.1", 5000);
OutputStream os = s.getOutputStream(); // 소켓과 스트림연결
OutputStreamWriter isr = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(isr);
pw.println(msg);
pw.close();
s.close();
}
//pw.close();
System.out.println("== Client Closed!! ==");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}