Results 1 to 2 of 2
  1. #1
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

    Default threading in server connection

    I am getting Exception. "connection refused" while trying to connect to my server. I implemented threading in my server and looks like something wrong. I am new to threading and not sure if i did right. These are my codes.. plzz some take a look.


    Java Code:
    public class server implements Runnable{
    
           ServerSocket server = null;
            Socket socket = null;
            ObjectInputStream in = null;
            ObjectOutputStream out = null;
    
        public static void main(String args[]) {
    
    
           ServerSocket server = null;
    
            try {
                //create socket
                server = new ServerSocket(5000, 100);
    
                System.out.println("Waiting for connection");
            }catch (IOException ex) {
                    Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
                }
    
           server myServer = new server();
           Thread myThread = new Thread(myServer);
           myThread.start();
    
        }
    
        public void run(){
              boolean exit = false;
                while (!exit) {
                try {
                    //accept nxt connection request
                    socket = server.accept();
                    System.out.println("Connection received from " + socket.getInetAddress().getHostName());
                    //get input and output streams
                    out = new ObjectOutputStream(socket.getOutputStream());
                    in = new ObjectInputStream(socket.getInputStream());
                    System.out.println("Connection successfull");
                    //reading input string
                    String inputStr = (String) in.readObject();
                    System.out.println("client> " + inputStr);
                    if (!inputStr.equals("exit")) {
                        if (inputStr.equals("john")) {
                            out.writeObject("Granted");
                        } else {
                            out.writeObject("Denied");
                        }
                    } else {
                        exit = true;
                        out.writeObject("exiting");
                    }
                    //close connection
                    in.close();
                    out.close();
                    socket.close();
                } catch (IOException ex) {
                    Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception e) {
                System.err.println("Exception: " + e.getMessage());
                e.printStackTrace();
            } finally {
                try {
                    server.close();
                } catch (IOException ex) {
                    Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
    
        }
        }
    }
    Last edited by niteangell21; 10-14-2010 at 11:33 PM.

  2. #2
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

Similar Threads

  1. Server Connection Code
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 7
    Last Post: 05-28-2010, 03:26 AM
  2. sql server connection problem
    By m.ali in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 02-22-2010, 07:09 PM
  3. Remote SQL Server Connection
    By saso1310 in forum JDBC
    Replies: 2
    Last Post: 05-15-2009, 08:39 AM
  4. Connection the sql server
    By Preethi in forum New To Java
    Replies: 7
    Last Post: 06-26-2008, 04:05 PM
  5. Sql server connection problem
    By tanvirtonu in forum JDBC
    Replies: 1
    Last Post: 03-31-2008, 01:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •