?????????????

??????????????Hello???????????????IP???????????????κ?????????????????????(Hello?? Java Socket)??????????????????????????????????????????

??????????

??????????????Socket?????Hello??????????????????????????????????????????????????????

????????????

????????JAVA?????????????????????API?????o????????JAVA SOCKET?????????????????????????????????????readline()?????Socket?????????????????????????з???/r/n???????????????????????????????????

?????????Щ??????????????г??????????????????????????а?????????????????????????????????????????????????????????а????????????????????????????з???????????????????Щ??????println()???????????readline()??????????????????????????????????????????Щ?????????????????????????????????????????£??????flush()????????????????????????л???????????????????д????

???????????????


package com.gloomyfish.socket.tutorial.two;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class HelloService extends Thread {
 private ServerSocket serverSocket;
 public HelloService(int port) throws IOException {
  serverSocket = new ServerSocket(port);
 }

 public void run()
 {
  try {
   while(true)
   {
    System.out.println("Waiting for client on port " + serverSocket.getLocalPort());
    Socket client = serverSocket.accept(); // blocked & waiting for income socket
    System.out.println("Just connected to " + client.getRemoteSocketAddress());
    DataOutputStream dos = new DataOutputStream(client.getOutputStream());
    byte[] hello = "Hello?? Java Socket".getBytes();
    dos.write(hello?? 0?? hello.length);
    dos.close();
    client.close();
   }
  } catch(Exception e) {
   e.printStackTrace();
  }
 }
 
 public static void main(String[] args) {
  try {
   HelloService service = new HelloService(9999);
   service.start();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}
??????????????9999?????????????????Java Thread?????????????????