Hello guys,
I start to learn&code some basic small client/server console applications in java -especially in android- and also I try to learn Threads, in fact, I managed to connect 2 different machine by uploading different projects to these machines, for example one project only consists of client and the other project consists of server application, now I want to upload one project to different two machines and connect these two machines. Firstly, I start to learn topic about Threads and take looking some basic code examples about Threads and now I want to combine with client server application but I can not control the behaviour of my -one- applicaton whether server or client, I do hundreds of searching with google but I do not exactly how I should start, to illustrate that here,

  • I want to connect machine A ^^ machine B
    -to connect them I upload ONE project
    -this ONE project must be act as both Server and Client
  • for acting I want to use Threads, one for Client; one for Server and maybe additional Thread to control server and client threads

    In general my observation is something like that,

    Thanks for your interests, pls advise to me

Recommended Answers

All 15 Replies

Are you working with Java SE on a PC or with another version of java on an Android system?

I have worked with Eclipse Java Standart edition with android plugin and two android emulators -4.1 API vers. 16- numbered 5554 and 5556, In fact I manage to connect them with using seperate projects one includes Server and second includes Client now I am working on one project to combine them.

for example I have button named "send" or "connect" --> clicking this button calls Client thread and at the same time innerrupts Server thread -this idea probably wrong-
and vice versa.

For testing client/server programs, I write a main() method that starts a thread for the server and one for the client. In the Thread's run() method I call the main() method for each. That executes both client and server in the same JVM. For example:

   final static int ThePort = 8880;

   public static void main(final String[] args) {
      Thread t1 = new Thread(new Runnable() {
         public void run() {
      Thread t2 = new Thread(new Runnable() {
         public void run() {

      // wait some
      try{Thread.sleep(5000);}catch(Exception x){}
      System.exit(0); // done

   }  // end main()

I am not using an IDE for this. I use the java command to execute the class with the above main() method.

NormR1, I am not sure that the port number 8880 is a typo? Shouldn't it be 8080 which is a very common port number?

I think you can choose any port you want for testing. There is a range of predefined ports that I think stops at about 4096, so any above that should work. 8080 is a popular port but not required.
I use a final variable throughout the code so there is no chance that I can code 8080 in one place and 8800 in another.

            ss = new ServerSocket(ThePort);

           socket = new Socket(InetAddress.getByName("localhost"), ThePort);

I've apologize for I had no internet access from Friday/Saturday, so I can not check my thread, thanks a lot for your posting, in one button action I try these codes,

   public void tusla(View v)
        Log.d("Main", "Is Interrupted : " + serve.isInterrupted());
        cli = new Thread(new Client());

serve is an object of Server.java class and clie is an object of Client.java now I will try to run with synchronized functions if I manage, thanks...

Why synchronised? Why interrupt the server - that means it won't be listening when you start the client. Just start the server then start the client in two different threads and let them both run.

I removed interrupted part, now I try to work with two different ports, to illustrate;

I have 2 android emulators one is 5554 and the other one is 5556
--> then I add two different txt files -location of sdcard- which keeps two different port numbers; 7880 for 5554 emulator and 7882 for 5556 emulator 5554's client thread should be connected by 5556's port number and 5556's client thread should be connected by 5554's port number, but it give me a lot of exceptions ,starting with "FATAL ..."

I don't understand why the multiple port numbers. YOur server should listen on a single port, and all clients connect to that port.

Because I want not only connect "from client to server" also connect "from server to client" both way communication via messages -exclude avaliable andorid services ^^ libraries i.e. SMSManager..., yes my server should listen one server spec. for 5554 emulator and again my server which is uploaded to 5556 also listen spec. port for 5556 emulator; but my idea may totaly wrong, I just try to seek some information on Internet.

Connect server to client seems a contradiction of the terms client and server.
Once a client is connected to a server they can both send info to each other.

Hello guys, I managed to overcome above issue by using DataoutputStream ^^ DatainputStream classes, for this step I want to send recorded audio data from 5554 android emulator to client emulator 5556, first of all from internet I find to code examples -how to record .wav file -and It works but problem is that here

File sdcard = Environment.getExternalStorageDirectory();
            File file = new File(sdcard, "rarOut.wav");
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream of = new BufferedOutputStream(fos);
            BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
            int i;
            while ((i = in.read()) != -1)
                Log.d("Server Activity:", "Receive data");

In my application I've already captured the spec. sound data and loaded to rarOut.wav file in sdcard before even it is low quality, now I have a file named rarOut.wav and I reinforced with server client application how I manage to stream audio file between two android emulators ?

thanks for posting , I should start a new thread

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.