01-08 08:57
Recent Posts
Recent Comments
Tags
- ์จ์ผ๋ํ
- ์๋์ด๋ ธ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ICT๋ฉํ ๋ง
- DATABASE
- ict๊ณต๋ชจ์
- appetizer
- RaspberryPi
- ํ๋ก๋ณด๋ ธ
- JOBํ๊ณ
- ํ์ด์ฌ
- python
- ICT
- DB
- API๋ง์ผํ๋ ์ด์ค
- API MarketPlace ๊ธ๋ก๋ฒ ์ํฌํฐ์ฆ
- ์๋ฐ
- Naver Cloud
- ์คํฝ์ค๋น
- ์กํ๊ณ
- TSQL
- ์คํฝ๋ ํ
- SQL
- Java
- linux
- ํ์ด์
- mysql
- ์ด๋ธ์
- Spring
- ํ์ด์๊ณต๋ชจ์
- Today
- Total
miinsun
[Network/Java] ์๋ฐ ์์ผ ํต์ ๋ณธ๋ฌธ
๐ฌ ์๊ตฌ ์ฌํญ
Java socket ํต์ ์ ๊ตฌํ
java_server.java ์ java_client.java๋ก ๊ตฌ์ฑ
๐ Server ์ฝ๋
์๋ฒ ์์ผ ์์ฑ
ServerSocket serverSocket = new ServerSocket(9000);
์๋ฒ๋ client ์ ์์ ๋๊ธฐํ๋ค๊ฐ client๊ฐ ์ ์ํ๋ฉด accept ํด์ค๋ค
Socket socket = serverSocket.accept();
์ ์๋ ํด๋ผ์ด์ธํธ์ ์ฃผ์๋ฅผ ์ถ๋ ฅ
System.out.println("Connected by ( " + socket.getLocalAddress()+ ", "+ socket.getLocalPort()+ ") ");
Client๊ฐ ๋ณด๋ธ ๋ฉ์ธ์ง ์ถ๋ ฅ
- ๋ฒํผ๋ฅผ ์ฌ์ฉํ๋ฉด ์ ์ถ๋ ฅ์ ํจ์จ์ด ๋์์ง๋ค
- ์๋๋ฐฉ์ด ๋ณด๋ธ ๋ฒํผ๋ฅผ ์ฝ์ผ๋ ค๋ฉด Socket์ ๊ธฐ๋ฅ ์ค Stream์ ๋ถ๋ฌ์ InputStreanReader ๊ฐ์ฒด์ ๋ด๋๋ค
BufferedReader bufReader = new BufferedReader( new InputStreamReader( socket.getInputStream()));
String message = bufReader.readLine();
System.out.println("Message : " + message );
Client์ ๋ฐ์ดํฐ ์ ์ก
BufferedWriter bufWriter = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream()));
๋ฐ์ ๋ฌธ์์ด์ ๋ค์ ํด๋ผ์ด์ธํธ์ ์ ์ก
bufWriter.write(message);
๋ฒํผ ๊ฐํ
bufWriter.newLine();
๋ฒํผ์ ๋จ์ ๋ฐ์ดํฐ ์ถ๋ ฅ
bufWriter.flush();
ํด๋ผ์ด์ธํธ, ์๋ฒ ์์ผ์ ๋ชจ๋ ๋ซ๋๋ค
socket.close();
serverSocket.close();
bufReader.close();
bufWriter.close();
Server ์ ์ฒด ์ฝ๋
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Java_server {
public static void main(String[] args ) {
try {
ServerSocket serverSocket = new ServerSocket(9000);
Socket socket = serverSocket.accept();
System.out.println("Connected by ( " + socket.getLocalAddress()+ ", "+ socket.getLocalPort()+ ") ");
BufferedReader bufReader = new BufferedReader( new InputStreamReader( socket.getInputStream()));
String message = bufReader.readLine();
System.out.println("Message : " + message );
BufferedWriter bufWriter = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream()));
bufWriter.write(message);
bufWriter.newLine();
bufWriter.flush();
socket.close();
serverSocket.close();
bufReader.close();
bufWriter.close();
}
catch( Exception e ) {
e.printStackTrace();
}
}
๐ Client ์ฝ๋
์๋ฒ ์ ์, localhost 9000๋ฒ ํฌํธ์ ์ ์
Socket socket = new Socket("127.0.0.1", 9000);โ
์๋ฒ์ ๋ณด๋ผ ๋ฐ์ดํฐ๋ฅผ ์ด๋ค
- ๋ฒํผ๋ฅผ ์ฌ์ฉํ๋ฉด ์ ์ถ๋ ฅ์ ํจ์จ์ด ๋์์ง๋ค
- Socket์ ๊ธฐ๋ฅ ์ค Stream์ ๋ถ๋ฌ์ OutPutStreamWriter ๊ฐ์ฒด์ ๋ด๋๋ค
BufferedWriter bufWriter = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream()));
๋ฒํผ ์ถ๋ ฅ
bufWriter.write("hello world");
๋ฒํผ ๊ฐํ
bufWriter.newLine();
๋จ์ ์๋ ๋ฐ์ดํฐ ๋ชจ๋ ์ถ๋ ฅ
bufWriter.flush();
์๋ฒ๊ฐ ๋ณด๋ธ ๋ฐ์ดํฐ ์ถ๋ ฅ
BufferedReader bufReader = new BufferedReader( new InputStreamReader( socket.getInputStream()));
String message = bufReader.readLine();
System.out.println("Message : " + message );
Client ์ ์ฒด ์ฝ๋
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class Java_client {
public static void main(String[] args ) {
try {
Socket socket = new Socket("127.0.0.1", 9000);
BufferedWriter bufWriter = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream()));
bufWriter.write("hello world");
bufWriter.newLine();
bufWriter.flush();
BufferedReader bufReader = new BufferedReader( new InputStreamReader( socket.getInputStream()));
String message = bufReader.readLine();
System.out.println("Message : " + message );
socket.close();
bufReader.close();
bufWriter.close();
}
catch( Exception e ){
e.printStackTrace();
}
}
}
๊ฒฐ๊ณผ
'Infra > Network' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Network] TCP TimeOut, TCP ์ฌ์ ์ก (0) | 2022.07.12 |
---|---|
[Network] TCP์ ์ดํด (0) | 2022.07.12 |
[Network] TCP์ UDP์ ์ฐจ์ด (0) | 2021.12.03 |
[Network/Python]ํ์ด์ฌ ์์ผ ํต์ (0) | 2021.12.02 |
[Network] ๋คํธ์ํฌ ๊ธฐ๋ณธ ์ฉ์ด (0) | 2021.11.25 |
Comments