3 Replies - 921 Views - Last Post: 10 October 2010 - 10:25 AM Rate Topic: -----

#1 Nikhil_07n   User is offline

  • The cheese stands alone..
  • member icon

Reputation: 54
  • View blog
  • Posts: 2,490
  • Joined: 09-January 09

Help with first client-server program

Posted 10 October 2010 - 09:50 AM

I'm having some strange problem while trying out my first client-server program..

Here's the part of my code for client class which I think is causing trouble:

			Socket s = new Socket(IP, 5000);
			InputStreamReader sr = new InputStreamReader(s.getInputStream());
			BufferedReader bf = new BufferedReader(sr);
			String x = bf.readLine();
			if (x=="relp") {
				return 1;
			} else {
				return 3;
			}


Server class :
				Socket chkSock = mainSocket.accept();
				PrintWriter sendConf = new PrintWriter(chkSock.getOutputStream());
				sendConf.println("relp");
				sendConf.flush();


The problem is that x=="relp" always returns false & the method returns the value 3. I've even checked the value stored in variable 'x'.. it's relp only.

And strangely enough, if I make use of integer in place of String, everything works fine.

Working code:

			Socket s = new Socket(IP, 5000);
			InputStreamReader sr = new InputStreamReader(s.getInputStream());
			BufferedReader bf = new BufferedReader(sr);
			int x = Integer.parseInt(bf.readLine());
			if (x==69) {
				return 1;
			} else {
				return 3;
			}



				Socket chkSock = mainSocket.accept();
				PrintWriter sendConf = new PrintWriter(chkSock.getOutputStream());
				sendConf.println(69);
				sendConf.flush();


I'm completely lost here. RELP!!

Is This A Good Question/Topic? 0
  • +

Replies To: Help with first client-server program

#2 b0ng01   User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Help with first client-server program

Posted 10 October 2010 - 09:58 AM

don't use == on a string use .equals()
http://download.orac...va.lang.Object)
Was This Post Helpful? 1
  • +
  • -

#3 Nikhil_07n   User is offline

  • The cheese stands alone..
  • member icon

Reputation: 54
  • View blog
  • Posts: 2,490
  • Joined: 09-January 09

Re: Help with first client-server program

Posted 10 October 2010 - 10:11 AM

I thought they were one and the same thing. Thanks. :)
Was This Post Helpful? 0
  • +
  • -

#4 b0ng01   User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Help with first client-server program

Posted 10 October 2010 - 10:25 AM

In java .equals checks the objects contents whereas == checks to see if the objects instance are the same.

To see it in code it would be:

package newpractice;

import java.util.Scanner;

public class Main
{

public static void main(String[] args)
{

    String n = "ha";
    String v = "ha";

    String r = new String("roger");
    String s = new String("roger");

if(n==v){
    System.out.println("they are equal for n and v");
}
    if(r==s){
        System.out.println("They are equal for r and s");
    }

    if(r.equals(s)){
        System.out.println("They are equal");
    }
}

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1