Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Networking Web Browser , +
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.
       
     }
     
     while(true){
       // 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 (java.io.IOException 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 = netInput.read();
              netOutput.write(input);
              System.out.print((char)input);
          } while(input!=-1);
       } catch (Exception ex3) {
         System.out.println("IO Exception" );
       }
       System.out.println("Client Disconnect"); 
     }
    } // end of main.
}
Posted 26-Apr-12 8:46am
Comments
SAKryukov at 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.
--SA
lewax00 at 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 at 26-Apr-12 14:05pm
   
Very bad thing for a professor. Please see my comment below.
--SA
SAKryukov at 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.
 
--SA

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
lewax00 at 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");
 
to
 
System.out.println("Exception opening socket: " + ex0.getMessage());
 
then come back with the more detailed error information.
ryanplawrence at 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
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,192
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 26 Apr 2012
Copyright © CodeProject, 1999-2014
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