hello
I want to connect a PC and an Android for speaking together via wifi in a LAN. (For example I want to compose a game which plays between a PC and an android) I have used Server Socket in java and I can connect two PCs, but this Technic does not work between PC and android.
I think the InetAddress class does not work in android. I mean this class cannot find the Android device from a PC or inverse. (I check this class from another simple program and it didn't work!)
I should say that I have used the permissions below in my android project:
Would you please be so kind, to tell me, how can I connect an android and a PC.<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
Sincerely yours
****************************
The simple Server/Client java Code:
import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Scanner; public class TalkServer { private static Scanner ScreenInput; private static ServerSocket SServer; private static Scanner socketIN; public static void main(String[] args) { System.out.println("TalkServer Version 1.0"); System.out.print("Type the port: "); ScreenInput = new Scanner(System.in); String sPort=ScreenInput.nextLine(); int iPort=Integer.parseInt(sPort); System.out.println("Port Nomber is:"+iPort); System.out.println("Wait for a client..."); try { SServer = new ServerSocket(iPort); Socket SClient; SClient=SServer.accept(); String client; client = SClient.getInetAddress().toString(); System.out.println("Connected to " + client); socketIN = new Scanner(SClient.getInputStream()); PrintWriter SocketOut; SocketOut=new PrintWriter(SClient.getOutputStream(),true); while (true) { System.out.print("Listening for Client:"); String sInput=socketIN.nextLine(); System.out.println(sInput); SocketOut.println("Server:"+sInput); if (sInput.equalsIgnoreCase("bye")) break; } SClient.close(); SServer.close(); System.out.println("Server closed"); } catch(Exception e) { e.printStackTrace(); System.out.println("Error in connection"); } } } ****************************************** import java.io.IOException; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class TalkClient { private static Scanner keyBoard; private static Scanner socketIN; public static void main(String[] args) { System.out.println("TalkClient Version 1.0"); System.out.print("Type the port:"); keyBoard = new Scanner(System.in); String sPort=keyBoard.nextLine(); int iPort=Integer.parseInt(sPort); System.out.println("Port Nomber is "+iPort); System.out.print("Type the Host:"); String sHost=keyBoard.nextLine(); Socket s; InetAddress ip; try { ip=InetAddress.getByName(sHost); System.out.println("This Host has The IPs below:"); InetAddress[] address=InetAddress.getAllByName(sHost); for (InetAddress iIP: address) System.out.println(iIP.toString()); System.out.println("Now Connecting to the Server..."); s=new Socket(ip,iPort); System.out.println("Connected to "+iPort); socketIN = new Scanner(s.getInputStream()); PrintWriter SocketOut; SocketOut=new PrintWriter(s.getOutputStream(),true); while(true) { System.out.print("Say something:"); String sSay=keyBoard.nextLine(); SocketOut.println(sSay); String sListen=socketIN.nextLine(); System.out.println("check back:"+sListen); if (sSay.equalsIgnoreCase("bye")) break; } } catch (UnknownHostException e) { System.out.println("the hos t is unknown"); } catch (IOException e) { System.out.println("Netwerk Error"); } catch (Exception e) { e.printStackTrace(); } } }
A simple java Inetaddress Code:
In PC this code work very well. It find not only the IP addresses of the stations in Local Lan but also the IP addresses of Sites in Internet.
/*********************************/import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Scanner; public class HostName { private static Scanner ScreenInput; public static void main(String[] args) { System.out.println("IP checker"); ScreenInput = new Scanner(System.in); while (true) { System.out.print("Type the host name: "); String sIP=ScreenInput.nextLine(); if (sIP.equals("")) break; try { InetAddress[] address=InetAddress.getAllByName(sIP); for (InetAddress iIP: address) System.out.println(iIP.toString()); } catch(UnknownHostException e) { System.out.println("Unknown host"); } catch(Exception e) { System.out.println("Other Errrors"); } System.out.println("**********"); } } }