views:

52

answers:

0

What does this error mean?

java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind

My assignment is to create a client-side app that will exchange info w/ the server class that was supplied. We're supposed to use localhost.

Here is the Server class...

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import javax.swing.ImageIcon;

public class Server{
    public static final int PORT_NUMBER = 8989;

    public static void main(String[] args){
        File imageDir = new File("images");
        String[] files = imageDir.list(new FilenameFilter(){
            @Override
            public boolean accept(File dir, String name){
                return name.toLowerCase().endsWith(".jpg") || name.toLowerCase().endsWith(".jpeg") || name.toLowerCase().endsWith("gif") || name.toLowerCase().endsWith("png")
                    || name.toLowerCase().endsWith("bmp");
            }
        });
        ServerSocket server = null;
        Socket client = null;
        try{
            server = new ServerSocket(PORT_NUMBER);
            server.setSoTimeout(5 * 60 * 1000); //5 minutes in milliseconds
            client = server.accept();
            ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
            out.writeObject(files);
            out.flush();
            Scanner in = new Scanner(client.getInputStream());
            String fileName;
            while (in.hasNextLine() && !(fileName = in.nextLine()).isEmpty()){
                File image = new File(imageDir, fileName);
                if (image.exists()){
                    ImageIcon icon = new ImageIcon(image.toURI().toURL());
                    out.writeObject(icon);
                    out.flush();
                } else{
                    out.writeObject(new ImageIcon());
                    out.flush();
                }
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally{          
            if (client != null) try{
                client.close();
            } catch (IOException e){
                e.printStackTrace();
            }
            if (server != null) try{
                server.close();
            } catch (IOException e){
                e.printStackTrace();
            }
        }
    }   
}

and here is the client class I have done...

import java.awt.BorderLayout;
import java.awt.Label;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


public class Client{

    static final int port = 8989;
    JFrame frame;
    String serverName;
    boolean running;
    JList filesList;
    PrintWriter print;
    ObjectInputStream objIn;

    public Client() throws UnknownHostException, IOException, ClassNotFoundException{
        frame = new JFrame();
        frame.setBounds(100,100,750,500);
        frame.setVisible(true);
        serverName = JOptionPane.showInputDialog(frame, "Enter a server name, please.");

        running = true;

        while(running){
        Socket sock = new Socket(serverName,port);
        OutputStream os = sock.getOutputStream();
        print = new PrintWriter(os);
        InputStream input = sock.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(input));

        objIn = new ObjectInputStream(input);

        String[] array = (String[])objIn.readObject();

        filesList = new JList(array);
        frame.add(filesList,BorderLayout.WEST);
        filesList.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e){
                String file = (String)filesList.getSelectedValue();
                print.write(file);
                print.flush();

                JLabel image = new JLabel();
                try {
                    image.setIcon((Icon)objIn.readObject());
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                frame.add(image,BorderLayout.CENTER);
                frame.repaint();
            }
        });
    }
    }

    public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException{
        Client c = new Client();
    }
}

Could anyone help me out with this error?? I'm not sure if its something I'm doing wrong or if its an error with the server.

Thanks.