디바이스1을 핫스팟으로 설정하고
디바이스 2, 3이 디바이스1의 핫스팟을이용하여 Wifi를 연결하고 서로 데이터 전송을 하려고 하는데요
WifiManager WM = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo WI = WM.getConnectionInfo();
int ipAddress = WI.getIpAddress();
String ip = Formatter.formatIpAddress(ipAddress);
TextView sIP = (TextView) findViewById(R.id.ip);
TextView msg = (TextView) findViewById(R.id.msg);
sIP.setText(ip);
try {
int portNumber = 5001;
ServerSocket aServerSocket = new ServerSocket(portNumber);
Toast.makeText(getApplicationContext(), "Waiting..." ,Toast.LENGTH_LONG).show();
while (true) {
Toast.makeText(getApplicationContext(), "Start Accept..." ,Toast.LENGTH_LONG).show();
Socket sock = aServerSocket.accept();
Toast.makeText(getApplicationContext(), "Accept..." ,Toast.LENGTH_LONG).show();
ObjectInputStream instream = new ObjectInputStream(sock.getInputStream());
final Object obj = instream.readObject();
msg.setText("Input from client : "+obj);
ObjectOutputStream outstream = new ObjectOutputStream(sock.getOutputStream());
outstream.writeObject(obj+"from Server");
outstream.flush();
sock.close();
}
}
catch (Exception ex) {
Toast.makeText(getApplicationContext(), "Socket fail" ,Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
이게 서버 코드이구요
WifiManager WM = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo WI = WM.getConnectionInfo();
int ipAddress = WI.getIpAddress();
String ip = Formatter.formatIpAddress(ipAddress);
final EditText tf = (EditText) findViewById(R.id.editText1);
final TextView msg = (TextView) findViewById(R.id.msg);
TextView mIP = (TextView) findViewById(R.id.mIP);
mIP.setText(ip);
try {
String hostname = "192.168.43.195";
int port = 5001;
InetAddress serverAddr = InetAddress.getByName(hostname);
Socket sock = new Socket(hostname, port);
Toast.makeText(getApplicationContext(), "Connecton Complete" ,Toast.LENGTH_LONG).show();
ObjectOutputStream outstream = new ObjectOutputStream(sock.getOutputStream());
outstream.writeObject(tf.getText().toString());
outstream.flush();
ObjectInputStream instream = new ObjectInputStream(sock.getInputStream());
String obj = (String) instream.readObject();
msg.setText(obj);
sock.close();
}
catch (Exception ex) {
Toast.makeText(getApplicationContext(), "Connection fail" ,Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
이게 클라이언트 코드입니다.
그런데 서버에서 accept가 안되는거같습니다...ㅠ 대체 왜 이런걸까요 ㅠㅠ
단말기 3개를 같은 wifi망에 물려서 테스트해보시는게 어떨까요 핫스팟 말고요
핫스팟 하면 3G로 연결되서 private IP 가 되기 때문에 안되지 않을까 싶은데요.
(아이디 멋있네요 )