Code provided by Sun's education.
//Server
import java.net.*;
import java.io.*;
public class SimpleServer {
public static void main(String args[]) {
ServerSocket s = null;
Socket s1;
String sendString = "Hello Net World!";
int slength = sendString.length();
OutputStream s1out;
DataOutputStream dos;
// Register your service on port 5432
try {
s = new ServerSocket(5432);
} catch (IOException e) {
// ignore
}
// Run the listen/accept loop forever
while (true) {
try {
// Wait here and listen for a connection
s1=s.accept();
// Get a communication stream associated with
// the socket
s1out = s1.getOutputStream();
dos = new DataOutputStream (s1out);
// Send your string!
// (UTF provides machine independence)
dos.writeUTF(sendString);
// Close the connection, but not the server socket
dos.close();
s1out.close();
s1.close();
} catch (IOException e) {
// ignore
}
}
}
}
//client
import java.net.*;
import java.io.*;
public class SimpleClient {
public static void main(String args[]) throws IOException {
int c;
Socket s1;
InputStream s1In;
DataInputStream dis;
// Open your connection to a server, at port 5432
// localhost used here
s1 = new Socket("127.0.0.1",5432);
// Get an input file handle from the socket and
// read the input
s1In = s1.getInputStream();
dis = new DataInputStream(s1In);
String st = new String (dis.readUTF());
System.out.println(st);
// When done, just close the connection and exit
dis.close();
s1In.close();
s1.close();
}
}
import java.io.*;
import java.net.*;
import java.util.*;
public class UdpServer{
//This method retrieves the current time on the server
public byte[] getTime(){
Date d= new Date();
return d.toString().getBytes();
}
// Main server loop.
public void go() throws IOException {
DatagramSocket datagramSocket;
// Datagram packet from the client
DatagramPacket inDataPacket;
// Datagram packet to the client
DatagramPacket outDataPacket;
// Client return address
InetAddress clientAddress;
// Client return port
int clientPort;
// Incoming data buffer. Ignored.
byte[] msg= new byte[10];
// Stores retrieved time
byte[] time;
// Allocate a socket to man port 8000 for requests.
datagramSocket = new DatagramSocket(8000);
System.out.println("UDP server active on port 8000");
// Loop forever
while(true) {
// Set up receiver packet. Data will be ignored.
inDataPacket = new DatagramPacket(msg, msg.length);
// Get the message.
datagramSocket.receive(inDataPacket);
// Retrieve return address information, including
// InetAddress and port from the datagram packet
// just recieved.
clientAddress = inDataPacket.getAddress();
clientPort = inDataPacket.getPort();
// Get the current time.
time = getTime();
//set up a datagram to be sent to the client using the
//current time, the client address and port
outDataPacket = new DatagramPacket(time, time.length, clientAddress, clientPort);
//finally send the packet
datagramSocket.send(outDataPacket);
}
}
public static void main(String args[]) {
UdpServer udpServer = new UdpServer();
try {
udpServer.go();
} catch (IOException e) {
System.out.println("IOException occured with socket.");
System.out.println (e);
System.exit(1);
}
}
}
//client
import java.io.*;
import java.net.*;
public class UdpClient {
public void go() throws IOException, UnknownHostException {
DatagramSocket datagramSocket;
// Datagram packet to the server
DatagramPacket outDataPacket;
// Datagram packet from the server
DatagramPacket inDataPacket;
// Server host address
InetAddress serverAddress;
// Buffer space.
byte[] msg = new byte[100];
// Received message in String form.
String receivedMsg;
// Allocate a socket by which messages are sent
// and received.
datagramSocket = new DatagramSocket();
// Server is running on this same machine for this
// example.
// This method can throw an UnknownHostException.
serverAddress = InetAddress.getLocalHost();
// Set up a datagram request to be sent to the server.
// Send to port 8000.
outDataPacket = new DatagramPacket(msg, 1, serverAddress, 8000);
// Make the request to the server.
datagramSocket.send(outDataPacket);
// Set up a datagram packet to receive
// server's response.
inDataPacket = new DatagramPacket(msg, msg.length);
// Receive the time data from the server
datagramSocket.receive(inDataPacket);
// Print the data received from the server
receivedMsg = new String(
inDataPacket.getData(), 0, inDataPacket.getLength());
System.out.println(receivedMsg);
//close the socket
datagramSocket.close();
}
public static void main(String args[]) {
UdpClient udpClient = new UdpClient();
try {
udpClient.go();
} catch (Exception e) {
System.out.println ("Exception occured with socket.");
System.out.println (e);
System.exit(1);
}
}
}