네트워크 통신 - Server
2023. 7. 21. 12:13ㆍJAVA
통신을 구현하려면 2가지 용어를 알아야 함.
1. IP주소 - 그 컴퓨터의 고유한 주소(숫자): 42억
2. port번호 - 그 컴텨 안에서의 프로그램 분류번호(6만5천개) : 0~1024까지는 사용하지 않을것을 권장
ex) - 부산항(ip주소), 3번항구(port번호)
서버용 프로그램 작성해보기
1) 서버소켓을 만들어 내는 작업
2) 클라이언트의 접속 기다리기...
3) 클라이언트가 보낸 메세지를 읽어오기위한 스트림만들기
4) 클라이언트가 보낸 메세지 읽어오기
//서버용 프로그램 작성
//1) 서버소켓을 만들어 내는 작업
try {
ServerSocket serverSocket = new ServerSocket(10001); //port번호
System.out.println("서버소켓이 생성되었습니다.");
//2) 클라이언트의 접속 기다리기...
System.out.println("클라이언트의 접속을 기다립니다.");
Socket socket = serverSocket.accept(); //여기서 접속할때 까지 커서(스레드)가 대기함.
System.out.println("클라이언트가 접속하셨습니다.");
//3) 클라이언트가 보낸 메세지를 읽어오기위한 스트림만들기
InputStream is=socket.getInputStream();
InputStreamReader isr= new InputStreamReader(is);//바이트스트림-->문자스트림
BufferedReader reader= new BufferedReader(isr); //보조문자스트림
//4) 클라이언트가 보낸 메세지 읽어오기
// String msg= reader.readLine();
// System.out.println("받은 메세지 : " + msg);
while(true) { //메세지 여러개 읽어오기
String msg= reader.readLine();
if(msg==null) break;
System.out.println("받은 메세지 : " + msg);
}
통신이 끝났으면 닫아주기
reader.close();
System.out.println("서버 통신 종료!!");
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class ServerTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 통신을 하려면 IP주소는 알아야 함.
// 자바언어에서 본인 PC의 IP주소를 관리해주는 클래스가 존재함
try {
System.out.println( InetAddress.getLocalHost().getHostAddress() );
System.out.println( InetAddress.getLoopbackAddress().toString() ); //localhost/127.0.0.1 <--본인PC를 지칭하는 특수 IP주소
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//###############################################################
//통신을 구현하려면 2가지 용어를 알아야 함.
//1. IP주소 - 그 컴퓨터의 고유한 주소(숫자): 42억
//2. port번호 - 그 컴텨 안에서의 프로그램 분류번호(6만5천개) : 0~1024까지는 사용하지 않을것을 권장
//ex) - 부산항(ip주소), 3번항구(port번호)
//서버용 프로그램 작성
//1) 서버소켓을 만들어 내는 작업
try {
ServerSocket serverSocket = new ServerSocket(10001); //port번호
System.out.println("서버소켓이 생성되었습니다.");
//2) 클라이언트의 접속 기다리기...
System.out.println("클라이언트의 접속을 기다립니다.");
Socket socket = serverSocket.accept(); //여기서 접속할때 까지 커서(스레드)가 대기함.
System.out.println("클라이언트가 접속하셨습니다.");
//3) 클라이언트가 보낸 메세지를 읽어오기위한 스트림만들기
InputStream is=socket.getInputStream();
InputStreamReader isr= new InputStreamReader(is);//바이트스트림-->문자스트림
BufferedReader reader= new BufferedReader(isr); //보조문자스트림
//4) 클라이언트가 보낸 메세지 읽어오기
// String msg= reader.readLine();
// System.out.println("받은 메세지 : " + msg);
while(true) { //메세지 여러개 읽어오기
String msg= reader.readLine();
if(msg==null) break;
System.out.println("받은 메세지 : " + msg);
}
reader.close();
System.out.println("서버 통신 종료!!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
바이트스트림을 문자스트림으로 변환하고..
더 나아가 보조문자스트림으로..변환
InputStream -> InputStreamReader ->BufferedReader
바이트스트림 --> 문자스트림 --> 보조문자스트림
'JAVA' 카테고리의 다른 글
| GUI(그래픽 유저 인터페이스) (0) | 2023.07.25 |
|---|---|
| 네트워크 통신 - Client (0) | 2023.07.21 |
| FileOutput(파일 출력) (0) | 2023.07.20 |
| FileInput(파일 입력) (0) | 2023.07.20 |
| Thread Control 예제 (0) | 2023.07.19 |