import java.*;
import java.lang.*;
import java.net.*;
import java.util.*;
import java.io.*;/*
* This class opens a TCP connection, and allows reading and writing of byte arrays.
*/
public class tcpip
{
protected Socket s = null;
public DataInputStream dis = null;
protected DataOutputStream dos = null;
public tcpip(InetAddress ipa, int port)
{
Socket s1 = null;
try {// Open the socket
s1 = new Socket(ipa.getHostAddress(), port);
}
catch (IOException e) {
System.out.println("Error opening socket");
return;
}
s = s1;
try {// Create an input stream
dis = new DataInputStream(
new BufferedInputStream(s.getInputStream()));
}
catch (Exception ex) {
System.out.println("Error creating input stream");
}
try {// Create an output stream
dos = new DataOutputStream(
new BufferedOutputStream(s.getOutputStream()));
}
catch (Exception ex) {
System.out.println("Error creating output stream");
}
}
public synchronized void disconnect()
{
if (s != null) {
try {
s.close();
}
catch (IOException e){ }
}
}
public synchronized void send(byte[] temp)
{
try {
dos.write(temp, 0, temp.length);
dos.flush();
}
catch(Exception ex) {
System.out.println("Error sending data : " + ex.toString());
}
}
public synchronized byte[] receive()
{
byte[] retval = new byte[0];
try {
while(dis.available() == 0);/* Wait for data */
}
catch (IOException e){ }
try {
retval = new byte[dis.available()];
} catch (IOException e){ }
try {
dis.read(retval);
} catch (IOException e){ }
return(retval);
}
public synchronized void send(String given)
{// WARNING: this routine may not properly convert Strings to bytes
int length = given.length();
byte[] retvalue = new byte[length];
char[] c = new char[length];
given.getChars(0, length, c, 0);
for (int i = 0; i < length; i++) {
retvalue[i] = (byte)c[i];
}
send(retvalue);
}
}
次に tcpip クラスを使う実際のアプレットを作成します.
次のコードをファイル "Test.Java"として保存してください.
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.net.*;
import java.io.*;
import java.lang.*;
import java.text.*;
import java.util.*;
public class Test extends Applet
{
static private boolean isapplet = true;
static private InetAddress arg_ip = null;
static private int arg_port = 0;
public tcpip gtp = null;;
InetAddress reader_ip = null;
int port = 10001;
public void init()
{
gtp = null;
reader_ip = null;
port = 10001;
}
public void start()
{
add(new Label("TCP/IP connection test"));
/* Either get the IP address from the HTTP server if we're
an applet, or from the commandline (if passed). */
if (isapplet) {
try {
reader_ip = InetAddress.getByName(getCodeBase().getHost());
}
catch (UnknownHostException e){ }
}
else {
reader_ip = arg_ip;
if (arg_port != 0) {
port = arg_port;
}
}/* Open a socket to the device server's serial port */
if (reader_ip != null) {
if (gtp == null) {
gtp = new tcpip(reader_ip, port);
if (gtp.s == null) {
add(new Label("Connection FAILED!"));
return;
}
}
}
if (gtp == null) {
add(new Label("Not Connected"));
return;
}
add(new Label("Connected"));/* ADD YOUR CODE HERE *
/* You may now perform IO with the Device Server via
* gtp.send(byte[] data_out);
* byte[] data_in = gtp.receive();
*/
}
public void destroy()
{
if (gtp != null)
gtp.disconnect();
gtp = null;
}
public void stop() {
}
public static void main(String[] args) {
Frame frame = new Frame("TCP/IP Test");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
if (args.length > 0) {
try{
arg_ip = InetAddress.getByName(args[0]);
}
catch (UnknownHostException e){}
if (args.length > 1) {
try {
arg_port = Integer.valueOf(args[1]).intValue();
}
catch (NumberFormatException e) {}
}
}
Test ap = new Test();
frame.add(ap);
ap.init();
isapplet = false;
ap.start();
frame.pack();
frame.show();
}
}
次に,start()ルーチンにある/* ADD YOUR CODE HERE */ にあなたのアプリケーションを加えます(上記の例では,デバイスサーバへの接続を作りますが,データの転送は行いません). シリアルデバイスへのデータの転送には gtp.send() を,シリアルデバイスからのデータの読み込みには gtp.receive() を使います.
文法:
gtp.send(byte[] array)
byte[] array = gtp.receive()
ここで,Javaのソースコードをクラスファイルにコンパイルします.
C:> Javac tcpip.Java
C:> Javac Test.Java
Javaアプリケーションをテストします.
C:> Java Test xxx.xxx.xxx.xxx 10001 |