Java Reference
In-Depth Information
Example
This example creates (a) a server process that transmits a fi xed graphics fi le to any
client that makes contact and (b) a client process that makes contact with the server
and accepts the fi le that is transmitted.
Firstly, the server code…
import java.io.*;
import java.net.*;
import javax.swing.*;
public class ImageServer
{
private static ServerSocket serverSocket;
private static fi nal int PORT = 1234;
public static void main(String[] args)
{
System.out.println("Opening port…\n");
try
{
serverSocket = new ServerSocket(PORT);
}
catch(IOException ioEx)
{
System.out.println(
"Unable to attach to port!");
System.exit(1);
}
do
{
try
{
Socket connection = serverSocket.accept();
//Step 1…
ObjectOutputStream outStream =
new ObjectOutputStream(
connection.getOutputStream());
//Step 2…
outStream.writeObject(
new ImageIcon("beesting.jpg"));
//To play safe, fl ush the output buffer…
outStream.fl ush();
}
catch(IOException ioEx)
{
Search WWH ::




Custom Search