Results 1 to 4 of 4

Thread: socket listning

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2004
    Location
    sweden
    Posts
    176

    socket listning

    Hello folks,
    I made this "function" to listen for data from a visualbasic app.
    Code:
    public static void ListenConnection(int port)
     try
         {
         ServerSocket ss=new ServerSocket(port);
         Socket sa=ss.accept();
         System.out.println("Client Accepted");
     }catch(Exception e){System.out.println(e);}
    I got a problem printing out the data is just received, do i need to make some sort of a declare of the socket?
    Code:
    //  BufferedReader br=new BufferedReader(new InputStreamReader(sa.getInputStream()));
    //  System.out.println(br.readLine());
    Thanks,
    Nait

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: socket listning

    What do you mean a problem? Are you recieving an error? How are you sending data?(with a printwriter?)

    The only thing I can think of is you are not reading everything, except for one line:

    Code:
    String inputLine;
    while ((inputLine = br.readLine()) != null)
    {
       System.out.println(inputLine + "\n");
    }
    Maybe that's it?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2004
    Location
    sweden
    Posts
    176

    Re: socket listning

    Hi again,
    All i want is to get all received data printed in the java-consol.
    But if i use "catch(Exception" it will only print out "java.lang.NullPointerException" in the consol.

    I don't know much about java, but must i close the socket each time it accepted new data?
    Code:
    private Socket sa;
    
    public static void ListenConnection(int port)
    {
     try
         {
         ServerSocket ss=new ServerSocket(port);
         Socket sa=ss.accept();
         System.out.println("Client Accepted");
    //BufferedReader sa=new BufferedReader(new InputStreamReader(sb.getInputStream()));
     }catch(Exception e){System.out.println(e);}}
    Code:
    try
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(sa.getInputStream()));
    String inputLine;
    while ((inputLine =br.readLine()) != null)
    {
       System.out.println(inputLine + "\n");
    }
     }catch(Exception e){System.out.println(e);}
    Thanks,
    Naitsabes
    Last edited by naitsabes85; Oct 31st, 2005 at 10:45 AM.

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: socket listning

    Where is the code that sends the data? You've got code listening and taking in what was sent, but I need to see how you send the data. As for closing it, you don't need to close it, but you can try to flush() the stream which is probably a good idea.

Posting Permissions

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



Click Here to Expand Forum to Full Width