Programming/JAVA

[펌] 서버 소켓 예제

영웅기삼 2005. 6. 24. 23:06

import java.io.*;
import java.net.*;

 

public class GameJava3_05_ServeSocket{
 public static void main(String[] args){
 try{
  ServerSocket myServerSocket=new ServerSocket(8080);
  System.out.println("클라이언트가 접속하길 기다리고 있습니다");

 

  Socket mySocket=myServerSocket.accept();
  System.out.println("클라이언트가 접속했습니다");

 

  BufferedReaderin=new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
  PrintWriterout=new PrintWriter(new OutputStreamWriter(mySocket.getOutputStream()));

 

  System.out.println("클라이언트로부터 받은 메세지:"+in.readLine());
  String msg="###접속을 허락합니다.";
  out.println(msg);
  out.flush();
  System.out.println("클라이언트에 보낸 메세지:"+msg);

 

  mySocket.close();
 }catch(MalformedURLException e){
  System.out.println(e.toString());
 }catch(IOException e){
  System.out.println(e.toString());
 }
 }
}