Android 채팅 서버 소스 01

Programming/Android 2013. 8. 20. 18:26 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

class perClientThread extends Thread {
	Socket socket;
	static ArrayList alp = new ArrayList();

	public perClientThread(Socket socket) {
		this.socket = socket;
	}

	public void run() {
		try {
			PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "utf-8"), true);
		
			BufferedReader in = new BufferedReader(new InputStreamReader(
					socket.getInputStream(), "utf-8"));
			String inputLine, outputLine;
			alp.add(out);

			while ((inputLine = in.readLine()) != null) {
				for(int i = 0 ; i < alp.size() ; i++)
				{
					alp.get(i).println(inputLine);
				}
				System.out.println(inputLine);
			}
			alp.remove(out);
			out.close();
			in.close();
			socket.close();
			super.run();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

class ReceiveThread extends Thread {
	ServerSocket ss;

	@Override
	public void run() {
		try {
			ss = new ServerSocket(2013);
			// 수신
			while (true) {
				Socket clientSocket = ss.accept();
				perClientThread pct = new perClientThread(clientSocket);
				pct.start();
			}
			// 수신성공
			// system.out.println
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
public class Test {
	public static void main(String[] args) {
		ReceiveThread rt = new ReceiveThread();
		rt.start();
	}
}


'Programming > Android' 카테고리의 다른 글

Android 채팅 소스 05  (1) 2013.08.21
Android 채팅 소스 04  (0) 2013.08.21
Android 채팅 소스 03  (0) 2013.08.20
Android 채팅 소스 02  (0) 2013.08.20
Android 채팅 소스 01  (0) 2013.08.20