完全なTCPサーバープログラムモデル


import java.io.*;
import java.net.*;
/**
 TCP 
*/
class Servicer implements Runnable{
	Socket s;
	public Servicer(Socket s){
		this.s = s;
	}
	public void run(){
		try{
			InputStream ips = s.getInputStream();
			OutputStream ops = s.getOutputStream();
			
			BufferedReader br = new BufferedReader(new 
				InputStreamReader(ips));
			DataOutputStream dos = new DataOutputStream(ops);
			while(true){
				String strWord = br.readLine();
				//System.out.println(strWord+":"+strWord.length());
				if(strWord.equalsIgnoreCase("quit")){
					break;	
				}
				String strEcho = (new StringBuffer(strWord).reverse()).toString();
				//dos.writeBytes(strWord+":"+"---->"+strEcho+"\r
"); dos.writeBytes(strWord+":"+"---->"+strEcho+ System.getProperty("line.separator")); } br.close(); dos.close(); s.close(); }catch(Exception e){ e.printStackTrace(); } } } public class TestTCPServer{ public static void main(String[] args){ try{ ServerSocket ss = new ServerSocket(8888); while(true){ Socket s = ss.accept(); new Thread(new Servicer(s)).start(); } //ss.close(); }catch(Exception e){ e.printStackTrace(); } } }