자바 다 중 스 레 드 다 중 채 팅 server

1074 단어 자바
package tcom.tcp.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.List;

public class ServerReceiveThread implements Runnable {

	private Socket socket;
	private List list;
	public ServerReceiveThread(Socket socket,String name,List list){
		this.socket = socket;
		this.list = list;
	}
	public void run() {
		try {
			InputStream is = socket.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			while(true){
				String msg = br.readLine();
				for (int i = 0; i < list.size(); i++) {
					Socket sk = list.get(i);
					OutputStream os = sk.getOutputStream();
					BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
					bw.write(msg);
					bw.newLine();
					bw.flush();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

좋은 웹페이지 즐겨찾기