app클라이언트1 -> java 서버 ->app 클라이언트2
이렇게 데이터를 전송하려고 하는데요.
클라이언트1에서 서버로는 전송이 되는데 서버에서 클라이언트2로는 전송이 안되요. 클라이언트2에는 출력이 안되요.
소스좀 봐주세요ㅠㅠ
------------------------------------------------------------------------------------
서버소스
메인
public class Main {
public static void main(String args[]){
new Manager();
}
}
매니져
public class Manager {
private HashMap<String,SocketItem> clients,drones,admins;
public Manager() {
runServer();
}
public void runServer() {
final int port = 3331;
try {
ServerSocket ss = new ServerSocket(port);
System.out.println("**서버시작**");
while (true) {
// System.out.println("Waiting for new client");
Socket socket = ss.accept();
// System.out.println("Accepted");
System.out.println("IP : " + socket.getInetAddress()+" 접속");
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
//int inputCase = Integer.parseInt(br.readLine());
//String id = br.readLine();
SocketItem item = new SocketItem(in, out, socket, this);
Thread t = new Thread(item);
t.start();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("io exception");
}finally{
System.out.println("finally");
}
System.out.println("Manager.runServer ends");
}
public void forwardMessage(){
}
}
소켓 아이템
import java.io.*;
import java.net.*;
public class SocketItem implements Runnable {
private BufferedReader in;
private PrintWriter out;
private Socket socket;
private Manager manager;
public SocketItem(BufferedReader in, PrintWriter out, Socket socket,
Manager manager) {
this.in = in;
this.out = out;
this.socket = socket;
this.manager = manager;
}
@Override
public void run() {
// System.out.println("socketitem run()");
boolean b = true;
String input = null;
int count =0;
while (b) {
count ++ ;
try {
input = in.readLine().toString();
int inputCase = Integer.parseInt(input);
switch(inputCase){
case 1:
System.out.println("Light On");
break;
case 2:
System.out.println("Light Off");
break;
case 3:
System.out.println("Window Open");
break;
case 4:
System.out.println("Window Close");
break;
case 5:
System.out.println("Blind Open");
break;
case 6:
System.out.println("Blind Close");
break;
case 0:
System.out.println("Dimming brightness 0");
break;
case 30:
System.out.println("Dimming brightness 30");
break;
case 70:
System.out.println("Dimming brightness 70");
break;
case 100:
System.out.println("Dimming brightness 100");
break;
}
out.println(input +" from server");
}catch (IOException e) {
e.printStackTrace();
b = false;
System.out.println("read thread exception");
}
}
System.out.println("thread terminated");
}
public void disconnect() {
try {
out.close();
in.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void write(String msg) throws IOException {
out.println(msg);
out.flush();
}
}
-----------------------------------------------------------
클라이언트 2 소스
package com.example.andsocktestclient;
public class MainActivity extends Activity {
public static final String TAG = "MainActivity";
TextView text01;
Handler handler;
Socket socket;
BufferedReader in;
PrintWriter out;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
text01 = (TextView) findViewById(R.id.text01);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
RequestThread thread = new RequestThread(9);
thread.start();
}
});
handler = new Handler();
}
class RequestThread extends Thread{
int sel;
String inStr;
RequestThread(){}
RequestThread(int a){sel = a;}
Boolean loop;
private final int connection_timeout = 3000;
public void run(){
con();
}
private void con() {
try {
socket = new Socket("168.131.174.35",3331);
appendText("Client started : 168.131.174.35, 3331");
while(true){
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
inStr = in.readLine().toString();
text01 = (TextView) findViewById(R.id.text01);
text01.post(new Runnable(){
public void run(){
text01.setText(inStr);
}
});
}
// while(true){
// }
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void appendText(String msg){
final String inMsg = msg;
handler.post(new Runnable() {
public void run() {
text01.append(inMsg + "\n");
}
});
}
public void quit() {
loop = false;
try {
if(socket != null) {
socket.close();
socket = null;
}
Thread.sleep(connection_timeout);
}catch(InterruptedException e) {
}catch (IOException e) {
// e.printStackTrace();
}
}
}
}
왜 출력이 안되는지.... 부탁드려요ㅠㅠㅠ