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 9 of 9

Thread: server/client communication problem

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default server/client communication problem

    I am confused with my project.

    It is a server/client system which uses java nio.

    The requiremet is that the server keep sending 10 messages to the client. Then the client read messages. But the cilent can read incomplete message which causes exception.

    I have a message size at the beginning of a message. How to let the client read the number of bytes specified in the msgSize.


    how to solve this problem? Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: server/client communication problem

    What classes and methods are you using?

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: server/client communication problem

    For read:
                 n = socket.read(rbuf);
    	     if (((n==0) || (n==-1))) {
    		logger.logInfo("t=" + t + " count=" + count + " and break."); //5-21-11
    		break;
    	     }

    For write:
                  n = socket.write(buffer);
    	      if(n == 0) { 							
    		buffer.clear();
    		break;//Feb14					
    	      }							
    	      count += n;					
    	      if(buffer.hasRemaining()){					
    		buffer.compact();				
    	      }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: server/client communication problem

    How to let the client read the number of bytes specified in the msgSize.
    What class is socket?
    Your code appears to try read rbuf.size number of bytes. Why not read the header and get the message size at the beginning of a message before reading the rest.

  5. #5
    Junior Member
    Join Date
    Jul 2010
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: server/client communication problem

    Thank you. But the SocketChannel class just provide method to read a chunk, instead of byte by byte. Is there any way to read the header and get message size first. Then I can read the required amount of bytes. Thanks.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: server/client communication problem

    n = socket.read(rbuf)
    What class is the variable socket that you show in your code sample?

  7. #7
    Junior Member
    Join Date
    Jul 2010
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: server/client communication problem

    socket is an instance of SocketChannel.

    If I add an interval between messages, the problem is not there. I just feel that adding an interval may not be a good solution. Is there other ways?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: server/client communication problem

    Sorry, I haven't worked with that.

  9. #9
    Junior Member
    Join Date
    Jul 2010
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: server/client communication problem

    thank you.

Similar Threads

  1. Replies: 0
    Last Post: February 24th, 2011, 06:31 AM
  2. Replies: 0
    Last Post: January 22nd, 2011, 11:52 AM
  3. client server communication
    By Brt93yoda in forum Java Theory & Questions
    Replies: 4
    Last Post: September 2nd, 2010, 04:49 PM
  4. [SOLVED] client-server-router problem
    By KrisTheSavage in forum Java Networking
    Replies: 2
    Last Post: August 23rd, 2010, 05:49 AM
  5. Partial messages problem with Java non-blocking socket communication
    By perl0101 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 28th, 2010, 03:38 PM