Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problems with Sockets

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems with Sockets

    I'm having some trouble with sockets not communicating properly. I wrote out some example code to try to target the problem but it's only confused me further.

    I wrote two example classes, TestServer and TestClient.

    My TestServer contains:
    BufferedReader in = new BufferedReader(new
    InputStreamReader(ns.getInputStream()));
    while (true) {
    System.out.println((in.readLine()));
    }
    and my TestClient contains:
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    while (true) {
    out.write("ss\n");
    }
    then it works fine, constantly outputting ss.

    But if TestClient is as follows:
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    while (true) {
    String ss = input.readLine();
    out.write(ss+"\n");
    }
    Then the Server doesn't receive anything.

    I have no idea what's going on and would greatly appreciate any help.


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with Sockets

    I've had limited success using flush(). Messages now get through, but I still lose information, particularly when several messages are queued in a row.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems with Sockets

    Good news is: I seem to have fixed the problem.
    Bad news is: I'm not really sure how.

Similar Threads

  1. Threads, Sockets & jFrame
    By Rakshasas in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 11th, 2011, 07:28 PM
  2. Java Sockets, Bytes / Objects
    By AdamD in forum Java Networking
    Replies: 1
    Last Post: January 31st, 2011, 03:25 PM
  3. Help Needed - Strings from sockets?
    By nate0927 in forum Java Networking
    Replies: 1
    Last Post: January 24th, 2011, 09:06 AM
  4. Java sockets help
    By ma05k1 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 27th, 2010, 06:52 AM
  5. Replies: 1
    Last Post: December 2nd, 2009, 04:01 AM