Click here to Skip to main content
13,051,803 members (59,387 online)
Rate this:
Please Sign up or sign in to vote.
this is an example server given by my professor. i am to write a simple web browser to accept a socket and display data received. getting exception opening socket. using socket 8080. any ideas would be very helpful.
public class server {
     * @param args the command line arguments
    public static void main(String[] args) {
        // TODO code application logic here
    // read arguments
	int portNum = java.lang.Integer.parseInt(args[0]); // the second argument is
                                                // a port number.
     // The server just opens a ServerSocket using the specified port number,
     // and then goes into an infinite loop.
     // try connecting to the server
     ServerSocket s = null;
     try {
         s = new ServerSocket(portNum);
     } catch (Exception ex0 ) {
       System.out.println("Exception opening socket");
       System.exit(1); // exit with error.
       // the server waits (using an accept call) for a client to connect.
       Socket clientConnection = null;
       try {
          clientConnection = s.accept();
       } catch (Exception ex1) {
          System.out.println("Exception with accept");
          System.exit(1); // exit with error.
       System.out.println("Client connected to Server");
       // the socket provides input and output streams we can treat 
       // like any other input and output stream.
       DataInputStream netInput = null; 
       DataOutputStream netOutput = null; 
       try {
          netInput = new DataInputStream(clientConnection.getInputStream());
          netOutput = new DataOutputStream(clientConnection.getOutputStream());
       } catch ( ex1) {
         System.out.println(" Unable to create input or output stream for network connection.");
         System.exit(1); // exit with error.
        int input; // input from network
       try {
          do {
             input =;
          } while(input!=-1);
       } catch (Exception ex3) {
         System.out.println("IO Exception" );
       System.out.println("Client Disconnect"); 
    } // end of main.
Posted 26-Apr-12 7:46am
SAKryukov 26-Apr-12 13:50pm
You say you need to write a Web browser, but this is rather some kind of a server. To start with, what exactly you are writing?
And what is the problem? Right now, this is not a question.
lewax00 26-Apr-12 13:56pm
Try printing out the message (i.e. ex0.getMessage() in this case) given by the exception and see what it tells you. As the program is written it throws out all useful information given by the exceptions.
SAKryukov 26-Apr-12 14:04pm
Reason for my vote of 1
I don't know who I vote 1 for: a professor or a student.

This code cannot be called a Web (HTTP) server, as it does not implement HTTP protocol, so the client of it cannot be called a Web browser.

Also, it's the server which "accepts a socket". A client would only connect to it. It could be something else in more general case, but then one cannot talk in terms of client-server model, not only about "Web client" or "Web server".

Therefore, the whole question is a big mess.

The problem with this server code is that it accept only one client; and then the whole thread is busy with working with the network stream. Such servers are written with at least two threads to separate this activities. Only then the thread can accept clients and work with available clients at the same time. I do understand that for learning purposes some initial code could be simplified, but this is different situation -- the code is provided by a professor.

I cannot understand why trying to teach students on such bad code. It is bad: exception handling, hard-coded immediate string constants, unsound terminology.

I could not trust such professor. If it was a student's code, I would understand, but teaching... No.

SAKryukov 26-Apr-12 14:05pm
Very bad thing for a professor. Please see my comment below.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Ok thanks. Does anyone have any suggestions? I do not have to worry about the client side of the browser. I only need to implement the get function. The program must accept a port number and be able to display web data such as text, image data, and directory info. I'm really not knowing where to start especially since I'm not able to open any port. Maybe I'm not inputing right port number. Does anyone know how to find out what port number a computer is using say for a browser such as firefox for testing purposes? Any suggestions what so ever would be useful thanks.
lewax00 26-Apr-12 14:54pm
As I stated above, we can't help you figure out WHY the exception is being thrown without more information. Exceptions generally carry with them information about why they were thrown, you just need to get that information. You don't even need to modify the code, use the debugger if you know how. If not, just change

System.out.println("Exception opening socket");


System.out.println("Exception opening socket: " + ex0.getMessage());

then come back with the more detailed error information.
ryanplawrence 26-Apr-12 14:59pm
Ok thanks. Will do

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 26 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100