java.net.Socket Incompatible Types

Discussion in 'Software' started by EGONick, Jun 11, 2008.

  1. EGONick

    EGONick Private E-2

    Had this problem for a few days now and It's really getting to me. Tried searching everywhere for it and just not been able to find a solution or even what the problem is.

    These are the errors I'm getting but I assume the top 2 errors are caused by the bottom because I got the bottom error first and the same objects are used.
    Code:
    ./User.java:72: cannot resolve symbol
    symbol  : method getInputStream ()
    location: class Socket
                                    in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                                                                                        ^
    ./User.java:73: cannot resolve symbol
    symbol  : method getOutputStream ()
    location: class Socket
                                    out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
                                                                                          ^
    ./Socket.java:38: incompatible types
    found   : java.net.Socket
    required: Socket
                                                    Socket server = listener.accept();
                                                                                   ^
    3 errors
    Here is my class if it helps:

    Code:
    import java.io.*;
    import java.net.*;
    import java.security.*;
    import java.util.*;
    import java.text.*;
    import java.lang.*;
    
    class Socket
    {
    	static int port = 9392;
    	String socket_status = "Awaiting connection.";
    	ServerSocket socket_listener;
    	
    	public Socket()
    	{
    		try
    		{
                ServerSocket listener = new ServerSocket(port);
    
    			System.out.println("Socket Connection");
    			System.out.println("----------------------------");
    			System.out.println("Port: " + port);
    			System.out.println("Status: " + socket_status);
    			System.out.println(" ");
    			System.out.println(" ");
    
                int i = 0;
                
                try
                {
                    while(true)
                    {	
    					if(i > 1)
    					{
    						Socket server = listener.accept();
    						
    						new User(server).start();
    					}
        
                        i++;
                    }
                }
                catch(SocketException e)
                {
                    System.out.println(e);
                }
            }
            catch(SocketTimeoutException e)
            {
                System.out.println("Socket timedout: " + e);
                e.printStackTrace();    
            }
            catch (IOException ioe)
            {
                System.out.println("IOException on socket listen: " + ioe);
                ioe.printStackTrace();
            }		
        }	
    }
    This is based on my previous server and only slightly modified and it works fine so I just can't figure out the problem.

    Any help would be appreciated.

    Thanks,
    Nick.
     
  2. PC-XT

    PC-XT Master Sergeant

    It looks like Java is getting confused about which Socket you are referring to in
    Socket server = listener.accept();
    You could try renaming your class to socket (all lower case) or mySocket or something like that to avoid confusion between the imported java.net.Socket and your class.
    Alternatively, you could avoid importing java.net.* and refer to java.net components using their full java.net paths like java.net.Socket
     
  3. PC-XT

    PC-XT Master Sergeant

    Actually, you could probably just change that line to
    java.net.Socket server = listener.accept();
    if this is your intention. You wouldn't need to delete the import java.net.*; line unless you still had problems.

    (Sorry to double post)
     
    Last edited: Jun 16, 2008

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds