サーバーからクライアントにデータを送信するアプリケーションを作成しました....コード(サーバーとクライアント)はデスクトップアプリケーションで完全に機能しますが、クライアントをAndroidタブレットに、サーバーをデスクトップに配置すると、機能しません
Androidとデスクトップ間の通信にwifiを使用しています
私が設定した権限は以下のとおりです
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
この問題の背後にあるものを誰か教えてください.....
この例外が発生しています
java.net.ConnectException: failed to connect to /192.168.1.74 (port 4545): connect failed: ECONNREFUSED (Connection refused)
サーバー.java
public class Server
{
private int TAB_SERVER_PORT = 4545;
private static PrintWriter TABout;
private HashSet <PrintWriter> TABhs=new HashSet<PrintWriter>();
public static void main(String args[])
{
new Thread(new TABServerThread()).start();
}
private class TABServerThread implements Runnable {
private ServerSocket TABserver;
@Override
public void run() {
try {
TABserver = new ServerSocket(TAB_SERVER_PORT);
System.out.println("Server Start the server at port " + TAB_SERVER_PORT + " and waiting for clients...");
while (true) {
Socket socket = TABserver.accept();
System.out.println("Server Accept socket connection: "+ socket.getLocalAddress());
new Thread(new TABClientHandler(socket)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class TABClientHandler implements Runnable {
private Socket clientSocket;
private Scanner in;
public TABClientHandler(Socket clietSocket) {
this.clientSocket = clietSocket;
}
@Override
public void run() {
try {
TABout = new PrintWriter(clientSocket.getOutputStream());
TABhs.add(TABout);
in = new Scanner(clientSocket.getInputStream());
String line;
while ((line = in.nextLine()) != null) {
if (line.equals("Reply")){
TABout.print("Server replies");
TABout.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
クライアント.java
public class Client implements Runnable {
private int TAB_SERVER_PORT = 4545;
private Socket tabletclient;
private PrintWriter tabletout;
private Scanner in;
@Override
public void run() {
String line;
boolean flag=true;
System.out.println("Waiting for server connection....");
while(flag){
try {
tabletclient = new Socket("192.168.1.74", TAB_SERVER_PORT);
tabletout = new PrintWriter(tabletclient.getOutputStream());
in = new Scanner(tabletclient.getInputStream());
try
{
if((line = in.nextLine())!=null)
{
System.out.println("Client Server says: " + line);
if (line.equals("Hello client")) {
tabletout.println("Reply");
tabletout.flush();
}
}
}catch(Exception d){
System.out.println("Connection from server has lost.........");
}
} catch (UnknownHostException e) {
} catch (IOException e) {
}
}
}
public static void main(String[] args) {
new Thread(new Client()).start();
}
}