create two socket connection in one thread - android

In my application i want to share file between a server and client, for that purpose i want to create two socket connection in one thread.
For example at server we have thread "T" and it has opened two socket like "1234" and socket no "3456" now on the client thread "T2" i will try to connect with these sockets.
So how i should implement it.
server:
thread
{
wait at socket "1234"
wait at socket "3456"
}
CLIENT SIDE:
Thread
{
connect with socket no "1234"
connect with socket no "3456"
}

if you have well implemented protocol(ie. each socket will send the data in a definite order), yeah one thread should work. but you can do this using only one socket connection too. what is the main point of creating two sockets? and what is the relation with android?
in addition; you definitely have to elaborate your question, otherwise it seems the question will be closed soon.

Related

RXKOTLIN/RXJAVA: Communication between the socket using Observables

I am a newbie to RXKotlin/RXJava.
I am developing the background service in Android.
In my service, I have
Bluetooth socket
TCP Socket
Whenever the data is available on the Bluetooth socket, read and write to the TCP socket. And whenever data is received in the TCP socket, write to the Bluetooth socket.
Can someone help me:
how to achieve this using Observables?
how to exchange the socket id information?
how to exchange the data?
Thanks
Please try using RxSubjects (https://blog.mindorks.com/understanding-rxjava-subject-publish-replay-behavior-and-async-subject-224d663d452f)
Let me take PublishSubject as an example here.
//a publish subject which publishes int values
public PublishSubject<Integer> source = PublishSubject.create();
source.onNext(1);
source.onNext(2);
So above lines of code goes in Bluetooth socket class.
Now in TCP socket class, using the source, you can observe here.
source
.subscribe(
{
//result
},
{
//error
}
)
Thats it.
Please make sure, the subscription happens before Bluetooth socket starts publishing data.

Queuing Bluetooth Connection Request and accept then concurrently

In my Android application I can accept connection request sent from BT device(SPP profile). those BT devices sends connection request periodically and application accept it.
But now my problem is, I can pair with multiple device but wants to communicate with paired devices periodically. so i want clarification on this front.
If application communicate with one device and at same time another device sends connection request then can i accept this connection request through my App using BluetoothServerSocket? How?
Bluetooth Server can server up to 7 different bluetooth clients, you need to create bluetooth server socket in a separate thread and every time a client connects , send that client to a new thread , and return to listening state.
you can use the following pseudocode
BluetoothServerSocket serverSocket = BluetoothAdapter.listenUsingRfcommWithServiceRecord();
while(running){
BluetoothSocket client = serverSocket.accept(); //blocks untel a client is connected
sendClientToHisThread(client);
}
private void sendClientToHisThread(final BluetoothSocket socket){
Thread thread = new Thread(new Runnable(){
#Override
public void run(){
// communicate with client
socket.close();
}
});
thread.start();
}
I think you can follow the line on the BluetoothChat example, having a thread listening for incoming connections but, in your case, as a connection is established you do not close the server socket.

Dialog for incoming bluetooth request

In my application two devices are connected via bluetooth. In the background runs an own thread for the bluetooth connection. (Just like the example )
When one device wants to connect to another device i want a request dialog to be displayed on the second device.
So I guess that i have to modify the AcceptThread. The AcceptThread has to inform my mainThread (for example with a Handler).
In the AcceptThread I find this code:
// This is a blocking call and will only return on a
// successful connection or an exception
socket = mmServerSocket.accept();
Now here is my problem: this "blocking call" runs the whole time. How and when shall I inform my mainThread that another device wants to connect?
Definitely afterwards.
What you want is the result of the blocking call - of the .accept().
That is socket in your code.
Here is a quote from the Android BluetoothServerSocket documentation:
Then call accept() to listen for incoming connection requests. This
call will block until a connection is established, at which point, it
will return a BluetoothSocket to manage the connection. Once the
BluetoothSocket is acquired, it's a good idea to call close() on the
BluetoothServerSocket when it's no longer needed for accepting
connections. Closing the BluetoothServerSocket will not close the
returned BluetoothSocket.
So don't forget to do:
mmServerSocket.close()
After you receive a socket correctly - a BluetoothSocket actually -, you can choose what to do with it following the user's choice:
Should he go ahead, you just create the AsyncTask that reads from the socket until the AsyncTask is cancelled or an Exception occurs(on Bluetooth disconnect probably).
Should he decline, just cancel the socket
If you receive an Exception during the blocking call I would return to the main menu only a toast, saying something failed. But you can do a dialog (like retry?)

Try to open the socket connection which is alreay opened hangs the android system.How to solve it?

I am very new to android. I am developing an application in android 2.2 that sends the some bytes to server via socket connection.
In server side they used simple java coding for server socket implementation. The sending data in socket connection is success.
But my problem is when send the data in socket connection then I close the connection in client side (that is in mobile side). But in server side it does not closed.
Then again the client try to send data to server via socket. So the client side (in android mobile) application is hanged.
socket=new Socket(this.ipAddress,this.port_number);
The above coding snippet hangs the client system. How to show the error screen in the client side without hanging client application.
All are welcome to give your ideas
I got the solution,The following are the steps,codings,etc.
while(//condition)
{
socket=new Socket(this.ipAddress,this.port_number);
//Opening input and output streams
//Transfer the data
//After data transfer connectin is closed.
//While loop runs untill some condition
}

A problem when creating the socket

I've a android app which try to connect to server. But there is a problem when I create a socket. the application hang when it try implement the following line:
Socket socket= null;
socket = new Socket("192.168.0.15", 6565);
note that I have added this permission:
<uses-permission android:name="android.permission.INTERNET" />
also I used the debug but it hang when it reach to creating socket line and I've got this detail about the error:
java.net.SocketTimeoutException: Socket is not connected id=829731319824
and I am sure that port is not bound.
I appreciate your time.
Your connection probably cannot be established or is taking some time to be established.
Operations on java.net.Socket are blocking that's why your application appears to be hung. To avoid "hanging" the UI, you should probably try to establish the connection using another thread so your UI thread won't block and will remain responsive.
Obviously your connection cannot be established and the problem lies not within your code but the network setup. To avoid your app to hang, make sure you catch the SocketTimeoutException to be able to show an error message to the user along with some buttons to, for instance, try again or cancel the connection.

Categories

Resources