As a fully open-source mobile phone platform, Android has been regarded by many developers as the most promising smartphone operating system in the future.
In addition, a large number of third-party applications have appeared in the Android Market for users to download and use in a short time,
Some programs enhance the APP functions of mobile phones, while others will make full use of the interaction between mobile phones and other electronic devices.
Today, we will explain how to implement communication between Android and PC through two examples.
1. The first example of communication between Android and PC:
PC: Java Server
Android: Java client
Java Server Source Code:
Public class tcp1_topserver implements runnable {<br/> Public static final string serverip = "192.168.0.149"; <br/> Public static final int SERVERPORT = 51706; </P> <p> Public void run () {<br/> try {<br/> system. out. println ("s: Connecting... "); <br/> serversocket = new serversocket (SERVERPORT); <br/> while (true) {<br/> Socket Client = serversocket. accept (); <br/> system. out. println ("s: inserting ing... "); <br/> try {<br/> bufferedreader in = new bufferedreader (New inputstreamreader (client. getinputstream (); <br/> string STR = in. readline (); <br/> system. out. println ("s: pinned ed: '" + STR + "'"); <br/>}catch (exception e) {<br/> system. out. println ("s: error"); <br/> E. printstacktrace (); <br/>}finally {<br/> client. close (); <br/> system. out. println ("s: Done. "); <br/>}< br/>} catch (exception e) {<br/> system. out. println ("s: error"); <br/> E. printstacktrace (); <br/>}< br/> Public static void main (string a []) {<br/> thread threads topserverthread = new thread (New tcp1_topserver (); <br/> threads topserverthread. start (); <br/>}< br/>}
Introduce the source code above:
Specify the port and IP address of the server to listen.
Public static final string serverip = "192.168.0.149 ";
Public static final int serverports = 51706;
Create a serversocket object for the IP address and port specified before the application.
Serversocket = new serversocket (SERVERPORT );
It is used to listen to and capture clients connected through socket.
Socket Client = serversocket. Accept ();
Use SOCKET to create a bufferedreader object to receive data in socket stream.
Bufferedreader in = new bufferedreader (New inputstreamreader (client. getinputstream ()));
Android client source code:
Inetaddress serveraddr = inetaddress. getbyname ("192.168.0.149"); // tcpserver. serverip <br/> log. D ("TCP", "C: Connecting... "); <br/> Socket socket = new socket (serveraddr, 51706); <br/> string message =" androidres, where is my pig (Android )? "; <Br/> try {<br/> log. D ("TCP", "C: sending: '" + message + "'"); <br/> printwriter out = new printwriter (New bufferedwriter (New outputstreamwriter (socket. getoutputstream (), true); <br/> out. println (Message); <br/>} catch (exception e) {<br/> log. E ("TCP", "s: error", e); <br/>} finally {<br/> socket. close (); <br/>}< br/>
Introduce the source code above:
The IP address of the specified server.
Inetaddress serveraddr = inetaddress. getbyname ("192.168.0.149 ");
Create a socket object using the IP address and port of the application server.
Socket socket = new socket (serveraddr, 51706 );
Create printwriter based on the established socket and send the information to the server through this object, which contains three parts:
Outputstreamwriter
Bufferedwriter
Printwriter
Printwriter out = new printwriter (New bufferedwriter (New outputstreamwriter (socket. getoutputstream (), true );
The above is an example of communication between Android and the Java server running on the PC. Source provided by anddev.org
2. The second example of communication between Android and PC:
PC: CSHARP Server
Android: Java client
Through the above examples, I have implemented communication between Android and CSHARP server through some conversions. There is no big difference in principle, but the method of application is slightly different.
Source code of CSHARP Server:
TcpClient ConnectedClient = ServerListener.AcceptTcpClient(); NetworkStream netStream = ConnectedClient.GetStream(); BinaryReader br = new BinaryReader(netStream); byte[] tempByte = new byte[8192]; tempByte = br.ReadBytes(8192); string RecevieString = System.Text.Encoding.Default.GetString(tempByte);
Introduce the source code above:
Create a tcpclient object based on the client information obtained by tcplistener.
Tcpclient connectedclient = serverlistener. accepttcpclient ();
Extract the networkstream object from the tcpclient object to establish a socket data stream.
Networkstream netstream = connectedclient. getstream ();
The binaryreader object is created to provide an interface for the newly created networkstream. This interface can be used to conveniently read information.
Binaryreader BR = new binaryreader (netstream );
Reads byte array data in stream and converts it to a string.
Byte [] tempbyte = new byte [1, 8192];
Tempbyte = Br. readbytes (8192 );
String receviestring = system. Text. encoding. Default. getstring (tempbyte );
Android client source code:
The method used by this android client is very different from that used in the first example. Please pay special attention to it (through my repeated verification, this is a feasible communication method between Java and CSHARP ).
String message = "androidres, where is my pig (Android )? "; <Br/> Socket socket = new socket (" 192.168.0.149 ", 51706); <br/> outputstream Ops = socket. getoutputstream (); <br/> dataoutputstream dos = new dataoutputstream (OPS); <br/> dos. write (message. getbytes (); <br/> dos. close ();
Introduce the source code above:
Create a socket object.
Socket socket = new socket ("192.168.0.149", 51706 );
Create an outputstream object based on the socket object. You can easily understand the purpose of this object based on the name. It is to create an output networkstream.
Outputstream Ops = socket. getoutputstream ();
Similar to binarywriter, dataoutputstream creates an interface for outputstream to write data to the other end of the socket.
Dataoutputstream dos = new dataoutputstream (OPS );
The last two lines of code are the write process.
Dos. Write (message. getbytes ());
Dos. Close ();
The above are the two communication methods provided by this site for Android and PC (the first method is from anddev.org, which should be noted here ).
If you have more clever or convenient methods, I hope to share them with you!
Note that you need to add the permission to androidmanifest. XML (permission activation)
<Uses-Permission Android: Name = "android. Permission. Internet"/>
Appendix timeout handling:
Socket = new socket (); <br/> ISA = new inetsocketaddress (host, Port); <br/> socket. Connect (ISA, timeout); // timeout