더 복습해야 할 부분
- 컬렉션
- OOP
- 오버라이딩
- 오버로딩
- this, super
- 제한자
- 식별자
- 패키지 (클래스패스==빌드)
- 인터페이스
- finally
- 쓰레드
- 스트림
- HDD정보 : java.io.File 디렉토리 + 파일
AServer.java
import java.io.*;
import java.net.*;
class AClient extends Thread {
Socket s;
String ip = "localhost";
int port = 2000;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
OutputStream os;
DataOutputStream dos;
InputStream is;
DataInputStream dis;
PrintStream ps = System.out;
AClient(){
try{
s = new Socket(ip, port);
pln("서버와 연결 성공");
readyIO();
start();
speak();
}catch(NullPointerException ne){
}catch(UnknownHostException ne){
}catch(IOException ie){
pln("서버("+ip+")를 네트워크에서 찾을 수 없음");
}
}
void readyIO(){
try{
os = s.getOutputStream();
dos = new DataOutputStream(os);
is = s.getInputStream();
dis = new DataInputStream(is);
}catch(IOException ie){
}
}
void speak(){
try{
String line = br.readLine();
while((line=br.readLine()) != null){
dos.writeUTF(line);
dos.flush();
}
}catch(IOException ie){
}finally{
try{
if(dos != null) dos.close();
if(os != null) os.close();
if(br != null) br.close();
if(s != null) s.close();
}catch(IOException ie){}
}
}
void listen(){
try{
String line = null;
while((line = dis.readUTF()) != null){
ps.println("Server>> " + line);
}
}catch(NullPointerException ne){
}catch(IOException ie){
}finally{
try{
if(dis != null) dis.close();
if(is != null) is.close();
if(ps != null) ps.close();
if(s != null) s.close();
if(ss != null) ss.close();
}catch(IOException ie){}
}
}
public void run(){
listen();
}
void pln(String str){
System.out.println(str);
}
public static void main(String[] args){
new AClient();
}
}
AClient.java
import java.io.*;
import java.net.*;
class AClient extends Thread {
Socket s;
String ip = "localhost";
int port = 2000;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
OutputStream os;
DataOutputStream dos;
InputStream is;
DataInputStream dis;
PrintStream ps = System.out;
AClient(){
try{
s = new Socket(ip, port);
pln("서버와 연결 성공");
readyIO();
start();
speak();
}catch(NullPointerException ne){
}catch(UnknownHostException ne){
}catch(IOException ie){
pln("서버("+ip+")를 네트워크에서 찾을 수 없음");
}
}
void readyIO(){
try{
os = s.getOutputStream();
dos = new DataOutputStream(os);
is = s.getInputStream();
dis = new DataInputStream(is);
}catch(IOException ie){
}
}
void speak(){
try{
String line = br.readLine();
while((line=br.readLine()) != null){
dos.writeUTF(line);
dos.flush();
}
}catch(IOException ie){
}finally{
try{
if(dos != null) dos.close();
if(os != null) os.close();
if(br != null) br.close();
if(s != null) s.close();
}catch(IOException ie){}
}
}
void listen(){
try{
String line = null;
while((line = dis.readUTF()) != null){
ps.println("Server>> " + line);
}
}catch(NullPointerException ne){
}catch(IOException ie){
}
}
public void run(){
listen();
}
void pln(String str){
System.out.println(str);
}
public static void main(String[] args){
new AClient();
}
}
내가 한 메모
127.0.0.1
: localhost IP 주소 기억해둬야 함
물리적 주소
: 하드웨어의 고유한 주소 =MAC 주소이다. MAC 주소의 3byte(6가지 문자)는 제조사를 구분할 수 있다. 16진수로 표현할 수 있다.
포트
: 포트의 범위는 0 ~ 65535
정도이다. 여기서, Well-Known port : 0~1023라고 불리는 것은 자주 사용하면 안 되는 관례가 있다.