I wrote this program back in May that makes use of networking. It's a smallish program that is comprised of 2 parts, a client and a server. The server just receives information from a single client. The client is just a form in which you input an address. If you open the client without opening the server first, it doesn't work. It just says "NO CONNECTION TO SERVER" when you try to add a record. Which is what it's supposed to do.
Anyway, for some reason, every time I open the server program, I get a BindException. I have this handled by a try-catch block, so it throws an error message of my own design "PORT ALREADY IN USE. EXITING." then it exits.
I arbitrarily chose port 10,000 to handle the transmission of data, and port 9,000 to handle task IDs (I had to use a separate socket so that the server would know what to do with the information it was given). I chose these because as far as I know they aren't reserved by any regular TCP/IP protocols. Also, this program worked fine 5 months ago. What's the deal? Here's the code for the server portion if you think you can find anything relevant in there.
public class P_AddressServer { //Text area to log activity. private JTextArea jtaServerLog = new JTextArea(); //Socket for listening for objects. private Socket listeningSocket; //Integer input stream from client to get task ID. private DataInputStream getTaskID; //Object input and ouput streams from and to client. private ObjectInputStream inputFromClient; private ObjectOutputStream outputToClient; //Integer to store flags indicating what the client wants. private int userTask; //ID of current record. private int currentRecord = 0; //Linked list to store the address objects. LinkedList<Address> mailingList = new LinkedList<Address>(); P_AddressServer() { //Make the JFrame. makeWindow(); //Sockets for listening for addresses and the task ID numbers. ServerSocket serverSocket = null; ServerSocket serverTaskSocket = null; try { //Open socket to client. Listen on port 10000 serverSocket = new ServerSocket(10000); //Open socket to listen for the task ID. serverTaskSocket = new ServerSocket(9000); //Wait for input. while(true) { //Begin listening. listeningSocket = serverSocket.accept(); Socket taskListeningSocket = serverTaskSocket.accept(); //Input stream for receiving the task's ID. getTaskID = new DataInputStream(taskListeningSocket.getInputStream()); //Get the task ID. userTask = getTaskID.readInt(); //Process server task based on user's task choice. if(userTask == Address.ADD) serverSideAdd(); else serverSideSingleItem(userTask); } } catch (BindException e) { JOptionPane.showMessageDialog(null, "PORT ALREADY IN USE. EXITING."); System.exit(0); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { serverSocket.close(); serverTaskSocket.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } //Method to construct the GUI frame. private void makeWindow() { //Create main frame. JFrame logWindow = new JFrame("Server activity log."); //Lock out user editing of server log. jtaServerLog.setEditable(false); //Log date and time of startup. jtaServerLog.append("SERVER STARTED AT: "); jtaServerLog.append(new Date().toString()); //Add elements to JFrame. logWindow.add(new JScrollPane(jtaServerLog)); //Set attributes of frame. logWindow.setVisible(true); logWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); logWindow.setSize(600,350); } //Method to add addresses to linked list and server log. private void serverSideAdd() throws IOException { //Create input and output streams. inputFromClient = new ObjectInputStream(listeningSocket.getInputStream()); try { //Current record is last item in list. currentRecord = mailingList.size(); Address newAddress = (Address)inputFromClient.readObject(); //Identify if this is the first address, and set the flag if it is. if(mailingList.isEmpty()) newAddress.isFirst = true; //Log address information to the server. addStuffToLog(newAddress.name); addStuffToLog(newAddress.street); addStuffToLog(newAddress.city); addStuffToLog(newAddress.state); addStuffToLog(newAddress.zip); //Add address from client to the linked list. mailingList.add(newAddress); //Move pointer of the last item. mailingList.get(currentRecord).isLast = true; //Next to last item is no longer the last item. //Disregard if this is the first item (size = 1, currentRecord = 0). if(mailingList.size() > 1) mailingList.get(currentRecord - 1).isLast = false; } catch(ClassNotFoundException e) { JOptionPane.showMessageDialog(null, "ADDRESS CLASS NOT FOUND. EXITING."); System.exit(0); } finally { //Close the input stream from the client. Reopen when needed next time. inputFromClient.close(); } } //End of serverSideAdd method. //Method to send single address from the list to the client. private void serverSideSingleItem(int userTask) throws IOException { //Open data stream to send record information to client. outputToClient = new ObjectOutputStream(listeningSocket.getOutputStream()); switch(userTask) { //Current record is the first record. case Address.FIRST: currentRecord = 0; break; //Current record is one more than the previous. case Address.NEXT: currentRecord++; break; //Current record is one less than the previous. case Address.PREVIOUS: currentRecord--; break; //Current record is one less than the size of the whole list case Address.LAST: currentRecord = mailingList.size() - 1; break; //Just quit if somehow an integer other than one of our 4 constants got in. default: System.exit(0); break; } /* Check current record. If it was record 0 when user entered this method, and user hit the Previous button, set it back to 0 by incrementing. If it was the last record and user hit Next, set it to the ID of the last by decrementing. Set the respective flags to indicate if they are the first or last. In this way, hitting previous on the first record will show the first record and hitting next on the last record will show the last record. */ if(currentRecord == -1) currentRecord++; if(currentRecord == mailingList.size()) currentRecord--; //Send current record. outputToClient.writeObject(mailingList.get(currentRecord)); } //Method to add strings to the server log. private void addStuffToLog(String addThis) { jtaServerLog.append("\n" +addThis); } //Program entry point. Simply creates new server object. public static void main(String[] args) { new P_AddressServer(); } }