Communicating with a server socket in Java

Drawer QR Code in Java Communicating with a server socket

Communicating with a server socket
QR Code JIS X 0510 Maker In Java
Using Barcode drawer for Android Control to generate, create QR Code JIS X 0510 image in Android applications.
www.OnBarcode.com
Barcode Generation In Java
Using Barcode drawer for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
is EXIT, it breaks the loops and exits D. If the input doesn t prompt an exit, the server echoes the input back to the client s OuputStream with a BufferedWriter. This example is a good, albeit intentionally basic, representation of what a server does. It handles input, usually in a separate thread, then responds to the client, based on the input. To try out this server before using Android, you can telnet to the specified port (after the server is running, of course) and type some input; if all is well, it will echo the output. To run the server, you need to invoke it locally with Java. The server has a main method, so it ll run on its own; start it from the command line or from your IDE. Be aware that when you connect to a server from the emulator (this one or any other), you need to connect to the IP address of the host you run the server process on, not the loopback (not 127.0.0.1). The emulator thinks of itself as 127.0.0.1, so use the nonloopback address of the server host when you attempt to connect from Android. (You can find out the IP address of the machine you re on from the command line by entering ifconfig on Linux or Mac and ipconfig on Windows.) The client portion of this example is where NetworkExplorer itself begins, with the callSocket method of the SimpleSocket Activity, shown in the next listing.
Paint USS Code 128 In Java
Using Barcode printer for Android Control to generate, create Code-128 image in Android applications.
www.OnBarcode.com
EAN-13 Creator In Java
Using Barcode creation for Android Control to generate, create GTIN - 13 image in Android applications.
www.OnBarcode.com
Listing 6.3 An Android client invoking a raw socket server resource, the echo server
Barcode Printer In Java
Using Barcode creator for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
PDF 417 Generator In Java
Using Barcode creation for Android Control to generate, create PDF-417 2d barcode image in Android applications.
www.OnBarcode.com
public class SimpleSocket extends Activity { . . . View variable declarations omitted for brevity @Override public void onCreate(final Bundle icicle) { super.onCreate(icicle); this.setContentView(R.layout.simple_socket); . . . View inflation omitted for brevity this.socketButton.setOnClickListener(new OnClickListener() { public void onClick(final View v) { socketOutput.setText(""); String output = callSocket( ipAddress.getText().toString(), Use callSocket port.getText().toString(), method socketInput.getText().toString()); socketOutput.setText(output); } }); } private String callSocket(String ip, String port, String socketData) { Socket socket = null; BufferedWriter writer = null; BufferedReader reader = null; Create String output = null; client try { Socket socket = new Socket(ip, Integer.parseInt(port)); writer = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream()));
Draw UPCA In Java
Using Barcode generator for Android Control to generate, create GS1 - 12 image in Android applications.
www.OnBarcode.com
Paint USPS Intelligent Mail In Java
Using Barcode creator for Android Control to generate, create USPS Intelligent Mail image in Android applications.
www.OnBarcode.com
reader = new BufferedReader(
Make QR Code 2d Barcode In None
Using Barcode creator for Online Control to generate, create QR Code ISO/IEC18004 image in Online applications.
www.OnBarcode.com
Quick Response Code Generation In None
Using Barcode creator for Office Word Control to generate, create Quick Response Code image in Office Word applications.
www.OnBarcode.com
Networking and web services
Scan PDF 417 In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Quick Response Code Maker In None
Using Barcode drawer for Office Excel Control to generate, create QR image in Office Excel applications.
www.OnBarcode.com
new InputStreamReader( socket.getInputStream())); String input = socketData; writer.write(input + "\n", 0, input.length() + 1); writer.flush(); output = reader.readLine();
Code39 Recognizer In VB.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Reading Barcode In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Write to socket
Creating EAN13 In Objective-C
Using Barcode maker for iPad Control to generate, create EAN13 image in iPad applications.
www.OnBarcode.com
GTIN - 128 Creation In Visual Studio .NET
Using Barcode creation for ASP.NET Control to generate, create UCC.EAN - 128 image in ASP.NET applications.
www.OnBarcode.com
output this.socketOutput.setText(output); // send EXIT and close writer.write("EXIT\n", 0, 5); writer.flush(); . . . catches and reader, writer, and socket closes omitted for brevity . . . onCreate omitted for brevity return output; }
Making Barcode In None
Using Barcode encoder for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
Recognizing European Article Number 13 In Visual Basic .NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Get socket
Draw GTIN - 12 In None
Using Barcode generation for Office Excel Control to generate, create UPC A image in Microsoft Excel applications.
www.OnBarcode.com
Recognize GS1 - 12 In Visual Basic .NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
In this listing, we use the onCreate method to call a private helper callSocket method B and set the output to a TextView. Within the callSocket method, we create a Socket to represent the client side of our connection C, and we establish a writer for the input and a reader for the output. With the housekeeping taken care of, we then write to the socket D, which communicates with the server, and get the output value to return E. A socket is probably the lowest-level networking usage in Android you ll encounter. Using a raw socket, though abstracted a great deal, still leaves many of the details up to you, especially the server-side details of threading and queuing. Although you might run up against situations in which you either have to use a raw socket (the server side is already built) or you elect to use one for one reason or another, higherlevel solutions such as leveraging HTTP usually have decided advantages.
Working with HTTP
As we discussed in the previous section, you can use a raw socket to transfer IP data to and from a server with Android. This approach is an important one to be aware of so that you know you have that option and understand a bit about the underlying details. Nevertheless, you might want to avoid this technique when possible, and instead take advantage of existing server products to send your data. The most common way to do this is to use a web server and leverage HTTP. Now we re going to take a look at making HTTP requests from an Android client and sending them to an HTTP server. We ll let the HTTP server handle all the socket details, and we ll focus on our client Android application. The HTTP protocol itself is fairly involved. If you re unfamiliar with it or want the complete details, information is readily available via Requests for Comments (RFCs) (such as for version 1.1: http://www.w3.org/Protocols/rfc2616/rfc2616.html). The short story is that the protocol is stateless and involves several different methods that allow users to make requests to servers, and those servers return responses. The entire web is, of course, based on HTTP. Beyond the most basic concepts, there are ways to pass data into and out of requests and responses and to authenticate with servers.
Copyright © OnBarcode.com . All rights reserved.