import java.awt.*;
import java.io.*;
import java.net.*;
public class URLAccessTest {
static void displayInfo(URLConnection u)
throws IOException
{
URL url = u.getURL();
println(url.toExternalForm() + ": ");
println("Type: ", u.getContentType());
println("Length: ", u.getContentLength());
println("Modified: ",
new Date(u.getLastModified()));
println("Expires: ", u.getExpiration());
println("Encoding: ",
u.getContentEncoding());
DataInputStream in = u.getInputStream();
String line;
while ((line = in.readLine()) != null)
println(line);
}
public static void main(String[] args)
throws IOException
{
URL url = new URL(argv[0], argv[1]);
try
displayInfo(url.openConnection());
catch (Exception E) {}
}
}
import java.awt.*;
import java.io.*;
import java.net.*;
class P {
static final int PORT = 2346;
static final String HOST = "ics.uci.edu";
}
public class SimpleServer {
public static void main(String[] args)
{
Server serv = new Server(P.PORT);
serv.out.println("Send me a string:");
try {
String data = serv.in.readLine();
println("Client says: " + data);
}
catch (IOException e) {}
}
}
class Server {
Socket chan;
DataInputStream in;
PrintStream out;
ServerSocket server;
Server(int port)
{
try {
server = new ServerSocket(port);
chan = server.accept();
out = new PrintStream(chan.getOutputStream());
in = new DataInputStream(chan.getInputStream());
}
catch (IOException e) {}
}
}
import java.awt.*;
import java.io.*;
import java.net.*;
public class SimpleClient {
public static void main(String[] args)
{
Client s = new Client(P.HOST, P.PORT);
String msg;
try {
msg = s.in.readLine();
println("Server says: " + msg);
}
catch (IOException e) {}
s.out.println("Recieved: " + msg);
}
}
class Client {
DataInputStream in;
PrintStream out;
Socket s;
public Client(String host, int port)
{
try {
s = new Socket(host, port);
out = new PrintStream(s.getOutputStream());
in = new DataInputStream(s.getInputStream());
}
catch (IOException e) {System.out.println(e);}
}
}
class DP {
static final int PORT = 2345;
}
public class Sender {
public static void main(String args[])
throws Exception
{
InetAddress addr = InetAddress.getByName(args[0]);
int msglen = args[1].length();
byte[] msg = new byte[msglen];
args[1].getBytes(0, msglen, msg, 0);
DatagramPacket packet =
new DatagramPacket(msg, msglen, addr, DP.PORT);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
}
}
public class Receiver {
byte[] buffer = new byte[1024];
DatagramPacket p = new DatagramPacket(buffer, buffer.length);
DatagramSocket socket = new DatagramSocket(DP.PORT);
while (true)
{
socket.receive(packet);
String s = new String(buffer, 0, 0, packet.getLength());
System.out.println("Received from "
+ packet.getAddress().getHostName()
+ packet.getPort());
System.out.println(s);
}
}