A simple java socket client

This is part two of my example of communicating between android and a pc over usb.
This shows a simple java client based on this oracle tutorial and I use it to test communication between android and the PC.

Here’s the code:

package com.rga.vzw;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;

public class EchoClient {
	public static void main(String[] args) throws IOException {

		System.out.println("EchoClient.main()");

		Socket echoSocket = null;
		PrintStream out = null;
		BufferedReader in = null;

		try {
			echoSocket = new Socket("localhost", 38300);
			out = new PrintStream(echoSocket.getOutputStream());
			in = new BufferedReader(new InputStreamReader(
					echoSocket.getInputStream()));
		} catch (UnknownHostException e) {
			System.err.println("Don't know about host: localhost.");
			System.exit(1);
		} catch (IOException e) {
			System.err.println("Couldn't get I/O for "
					+ "the connection to: localhost.");
			System.exit(1);
		}

		BufferedReader stdIn = new BufferedReader(new InputStreamReader(
				System.in));
		// String userInput;
		System.out.println("connected!!");
		int counter = 0;

		// TODO monitor
		while (true) {
			counter++;
			// out.println(counter);
			if (counter % 1000 == 0) {
				out.println("update" + new Date().getSeconds());
				counter = 1;
				System.out.println("echo: " + in.readLine());
			}
		}
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *