1

This is my Server Program. This works fine. It starts waiting for the client.

package jay.com;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class serv {
public static void main(String[] args) throws IOException{
    String recvsentence = null;
    String sendsentence = null;


    ServerSocket server=new ServerSocket(5000);

    System.out.println("waiting for client");

    Socket sock = server.accept();
    System.out.println("Client Connected with IP Address");

    DataInputStream dis = new DataInputStream(sock.getInputStream());

    DataOutputStream dos = new DataOutputStream(sock.getOutputStream());

    recvsentence = dis.readUTF();

    System.out.println("Got Data");

    sendsentence = recvsentence+"changed";

    System.out.println("Message Altered");

    dos.writeUTF(sendsentence);

    sock.close();

    server.close();

}

}

This is my Client Program. Socket is not creating here....

package jay.com;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Mypro1 extends Activity {
EditText myText,finaltext;
Button button;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myText=(EditText)findViewById(R.id.editText1);
    button=(Button)findViewById(R.id.button1);
    finaltext=(EditText)findViewById(R.id.editText2);


    button.setOnClickListener(new View.OnClickListener(){

        public void onClick(View arg0) {
            byte adr[] = new byte[4];
            adr[0]=(byte)(192);
            adr[1]=(byte)(168);
            adr[2]=(byte)(1);
            adr[3]=(byte)(141);
            String sendsentence=null;
            String recvsentence=null;
            InetAddress addr=null;      

            try {
                addr = InetAddress.getByName("10.0.2.2");
                //finaltext.setText(addr.toString());
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            }

            try {
                //finaltext.setText("hi");
                Socket con=new Socket(addr,5000);

                finaltext.setText("Socket Created");

                DataInputStream dis = new DataInputStream(con.getInputStream());
                DataOutputStream dos = new DataOutputStream(con.getOutputStream());

                sendsentence=myText.getText().toString();

                dos.writeUTF(sendsentence);

                System.out.println("waiting for server response");

                recvsentence = dis.readUTF();

                System.out.println("got response");

                finaltext.setText(recvsentence);

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                System.out.println("Problem while creating");
                e.printStackTrace();
            }

        }
    });
}

}

Can anyone tell me whats the problem and also i want to know how to run the program. what ip address should i give. whether i've to try to connect to 10.0.2.2 or to 192.168.1.141(my Ip address)....

2
  • You need to use ip address of the host Commented Mar 13, 2012 at 6:49
  • why socket is not creating at the client side even after changing ip address of the host??? Commented Mar 13, 2012 at 6:51

3 Answers 3

1

You can find out the IP address of the server by calling server.getInetAddress();

Therefore if you change the line after creating the ServerSocket you will see the current IP address of your server.

System.out.println("waiting for client on IP " + server.getInetAddress());
Sign up to request clarification or add additional context in comments.

Comments

0

If you run server client app on emulator, you have to redirect your port otherwise your emulator doesn't work as server - Go through it...

Comments

0

Sockets are not created on UI thread. They should be created on a separate thread or else would be blocked by the OS. Best approach is to use AysncTask but for a starter,

    new thread(new new Runnable() {
    public void run() {
        // create your socket here.
    }
}).start();

This approach can work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.