2013年7月19日星期五

Android and PC communications?

android eclipse can write down the use of a TCP client, and then want to get hold of the server on the PC forehead.
Found on the network has a master wrote:
PC-SERVER but he was written in JAVA yo, do not know what is under the PC software can be used to execute JAVA generate a file?

PC - Server:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TalkServer {
public static void main (String [] args) {
ServerSocket server = null;
try {
server = new ServerSocket (4700);
/ / Create a ServerSocket listens for client requests on port 4700

Socket socket = server.accept ();
/ / Use the accept () blocks waiting for client requests, a customer
/ / Request comes a Socket object is generated, and continue execution

String line;
BufferedReader is = new BufferedReader (new InputStreamReader (
socket.getInputStream ()));

/ / Get the input stream from the Socket object, and construct the corresponding BufferedReader object

PrintWriter os = new PrintWriter (socket.getOutputStream ());
/ / Get the output stream from the Socket object and construct PrintWriter object

BufferedReader sin = new BufferedReader (new InputStreamReader (
System.in));
/ / Standard input device constructed by the system BufferedReader object

System.out.println ("Talk Server Client:" + is.readLine ());
/ / Print on the standard output from the client reads a string

line = sin.readLine ();
/ / Read from standard input string into a
while (! line.equals ("bye")) {
/ / If the string is "bye", then stop the loop
os.println (line);
/ / Output the string to the client
os.flush ();
/ / Flush the output stream, the Client immediately receive the string

System.out.println ("in Talke Server, Server:" + line);
/ / Print on the standard output in the system reads the string
System.out.println ("in Talke Server, Client:" + is.readLine ());
/ / Read a string from the Client, and print to standard output
line = sin.readLine ();
/ / From the system standard input a string
/ / Continue the loop
os.close (); / / close the output stream Socket
is.close (); / / close the input stream Socket
socket.close (); / / Close Socket
server.close (); / / close the ServerSocket
} Catch (IOException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}

}
}



-------------------------------------------------
You need to install the JDK and then be able to run under CMD
-------------------------------------------------
Specifically the next step now, JDK to install too, then CMD how a process? For example, my project files in the D drive · · · · ·
-------------------------------------------------
Enter CMD
D:
javac xxxx.xx (file name extension is correct, then it will not be displayed)
java xxxx (file name)
-------------------------------------------------

没有评论:

发表评论