자바 93) 네트워크 클라이언트

hahahoho셍·2021년 5월 15일
0

자바 공부

목록 보기
94/97
package ex14_file;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

class Clinent{
	private String ip = "127.0.0.1";//127.0.0.1 내컴퓨터, 로컬호스트라는 뜻
	private int port = 33333;
	void start() {
		Scanner sc = new Scanner(System.in);
		Socket socket = null;
		PrintWriter pw = null;
		//클라이언트에서 서버의 ip와 port번호 필요
		try {
			socket =  new Socket(ip,port);
			System.out.println("클라이언트 서버에 접속");
			pw = new PrintWriter(socket.getOutputStream(), true);
			while(true) {
				System.out.println("보낼 메세지");
				String data = sc.nextLine();
				pw.println(data);
				if (data.equals("quit")) break;
				
			}
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
				try {
					if(pw != null) pw.close();
					if (socket != null)
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
}

public class J20210510_03_network_client {

	public static void main(String[] args) {
		Clinent clinent = new Clinent();
		clinent.start();

	}

}
profile
그냥 공부 한거 적는 벨로그 하하하핳ㅎ하하하핳하ㅏ

0개의 댓글

관련 채용 정보