Installiert habe ich Java SDK 6.13
Code:
Code:
import java.*;
import java.lang.*;
import java.net.*;
import java.util.*;
import java.io.*;
import java.applet.*;
public class TcpIP
{
protected Socket s = null;
public DataInputStream dis = null;
protected DataOutputStream dos = null;
InetAddress reader_ip = null;
public TcpIP(int port ,Applet a)
{
try
{
reader_ip = InetAddress.getByName(a.getCodeBase().getHost());
}
catch (UnknownHostException e){}
if (reader_ip != null) Connect(reader_ip, port);
}
public void Connect(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 void send(byte[] temp, int len)
{
try {
dos.write(temp, 0, len);
dos.flush();
}
catch(Exception ex) {
System.out.println("Error sending data : " + ex.toString());
}
}
public synchronized void send(String given)
{
// W ARNING : 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);
}
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 int available()
{
int avail;
avail = 0;
try {
avail = dis.available();
} catch (IOException e) {}
return(avail);
}
}
Lesezeichen