Results 1 to 14 of 14

Thread: Two way socet

  1. #1
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Two way socet

    Is there an elegant solution for two way socket?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: Two way socet

    Quote Originally Posted by Fubarable View Post
    Yes.
    Ok.Show it.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Two way socet

    Quote Originally Posted by gyijhbk View Post
    Ok.Show it.
    The tutorials have much on use of sockets including using it for two-way communication. I'd be happy to show more and discuss more if you would kindly provide more detail regarding your specific problem.

  5. #5
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: Two way socet

    Quote Originally Posted by Fubarable View Post
    The tutorials have much on use of sockets including using it for two-way communication. I'd be happy to show more and discuss more if you would kindly provide more detail regarding your specific problem.
    Well I want the server to send data and then the client to process and send a reply to the server.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Two way socet

    Quote Originally Posted by gyijhbk View Post
    Well I want the server to send data and then the client to process and send a reply to the server.
    Very good, then there are many many tutorials written on this which Google can help you find. If you get stuck after reading a tutorial or two and have a specific question, please feel free to come on back and we'll be glad to try to help.

  7. #7
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: Two way socet

    Quote Originally Posted by Fubarable View Post
    Very good, then there are many many tutorials written on this which Google can help you find. If you get stuck after reading a tutorial or two and have a specific question, please feel free to come on back and we'll be glad to try to help.
    Ok.Here is the problem.When I try to send a string like this:
    Java Code:
     String sentence;
    		        	  String modifiedSentence;
    		        	  BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
    		        	  Socket clientSocket = new Socket("localhost", 6789);
    		        	  DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    					  outToServer.writeBytes("Ima wryzka");
    		        	  BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    		        	  sentence = inFromUser.readLine();
    		        	  outToServer.writeBytes(sentence + '\n');
    		        	  modifiedSentence = inFromServer.readLine();
    I recive this:Ima wryzkanull

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Two way socet

    You're sending primitive bytes in the output and reading in text -- don't do that. Send out text with a PrintWriter or similar object, and read in text with your BufferedReader; make it consistent.

  9. #9
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: Two way socet

    Quote Originally Posted by Fubarable View Post
    You're sending primitive bytes in the output and reading in text -- don't do that. Send out text with a PrintWriter or similar object, and read in text with your BufferedReader; make it consistent.
    Ok.I use this example :Reading from and Writing to a Socket (The Java™ Tutorials > Custom Networking > All About Sockets)
    And i use PrintWriter .

  10. #10
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: Two way socet

    Well, now i have the sent message, but it is different. When I try to compare it with the same string appears to be different. Why?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Two way socet

    Quote Originally Posted by gyijhbk View Post
    Well, now i have the sent message, but it is different. When I try to compare it with the same string appears to be different. Why?
    I've no idea.

    Re-read your statement above, but do as if you were me and have no knowledge of your code, what you're trying to do, or what "different" means, and you'll see how no one can answer this question. Please read How to Ask Smart Questions to find some great tips on how to ask questions that can be answered.

  12. #12
    gyijhbk is offline Member
    Join Date
    Feb 2012
    Posts
    61
    Rep Power
    0

    Default Re: Two way socet

    Quote Originally Posted by Fubarable View Post
    I've no idea.

    Re-read your statement above, but do as if you were me and have no knowledge of your code, what you're trying to do, or what "different" means, and you'll see how no one can answer this question. Please read How to Ask Smart Questions to find some great tips on how to ask questions that can be answered.
    Here is the code
    Java Code:
    	
    		       try { 
    		        	//Thread.sleep(10000);
    		        	  String sentence;
    		        	  String modifiedSentence;
    		        	  BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
    		        	  Socket clientSocket = new Socket("localhost", 6789);
    		        	  DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    		        	  BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    		        	  sentence = "aaa";
    		        	  outToServer.writeBytes(sentence + '\n');
    		        	  modifiedSentence = inFromServer.readLine();
    		        	  System.out.println("FROM SERVER: " + modifiedSentence);
    		        	  if( modifiedSentence == "22")
    					  {
    					  
    					  }
    					  clientSocket.close();
    					  
    		        } catch (UnknownHostException e) {
    		            System.err.println("Don't know about host: taranis.");
    		            System.exit(1);
    		        } catch (IOException e) {
    		            System.err.println("Couldn't get I/O for "
    		                               + "the connection to: taranis.");
    		            System.exit(1);
    		        }
    Java Code:
     try{
    				String clientSentence;
    	         String capitalizedSentence;
    	         ServerSocket welcomeSocket = new ServerSocket(6789);
    	
    	         while(true)
    	         {
    		
    	            Socket connectionSocket = welcomeSocket.accept();
    	            BufferedReader inFromClient =
                    new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
    	            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
    	            clientSentence = inFromClient.readLine();
    	            System.out.println("Received:" + clientSentence);
    				if ( clientSentence == "aaa")
    				{
    				
    				outToClient.writeBytes("22");
    				}
    	            capitalizedSentence = clientSentence.toUpperCase() + '\n';
    	            outToClient.writeBytes(capitalizedSentence);
    	         }
    			}
    		
    			catch (IOException e)
    			{}
    Last edited by gyijhbk; 06-16-2012 at 05:42 PM.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Two way socet

    Thank you for the code, but you've yet to clarify your question. Again, please click on and read the link from my previous post as the suggestions in that link are very helpful. I know as they've helped me when I've asked questions here and elsewhere.

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: Two way socet

    Quote Originally Posted by gyijhbk View Post
    Here is the code
    Java Code:
    	
    				if ( clientSentence == "aaa")
    Ahem ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

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