I want to make my android app open socket to my windows console app and they communicate with each other. The socket is opened and data is sent and received in windows app, but my android app does not receive the answer which sent by windows. I watch the packets in my android and I saw the packets are coming but I do not know why my app do not receive it!
windows app server class:
class Server
{
private TcpListener tcpListener;
private Thread listenThread;
public Server()
{
Console.WriteLine("\nStarting server...");
this.tcpListener = new TcpListener(IPAddress.Any, 1234);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
private void ListenForClients()
{
Console.WriteLine("\nWaiting for clients to connect...");
this.tcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
//create a thread to handle communication with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
Console.WriteLine("\nIncoming from client...");
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
try
{
while (true)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
//a socket error has occured
break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
break;
}
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
Console.WriteLine("\nReceived: \n\n" + encoder.GetString(message, 0, bytesRead));
//By FMR
string response = "random responsive: " + new Random().Next(1000).ToString() + "\n";//"\r\n";
//writeData(clientStream, response);
byte[] msg = System.Text.Encoding.ASCII.GetBytes(response);
// Send back a response.
clientStream.Write(msg, 0, msg.Length);
clientStream.Flush();
Console.WriteLine("\nResponed ..." + response);
}
}
catch (Exception ex)
{
Console.WriteLine("\nException while: " + ex.Message);
}
tcpClient.Close();
}
}
my android thread:
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
Socket socket = null;
ServerSocket serverSocket = null;
Boolean bRun = true;
try {
socket = new Socket(ip, port);
if(outputStream == null) {
outputStream = new DataOutputStream(socket.getOutputStream());
}
// become server
serverSocket = new ServerSocket(port);
Log.i(G.TAG, "before serverSocket.accept");
socket = serverSocket.accept();
Log.i(G.TAG, "response recieve: ");
inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch (Exception e) {
try {
serverSocket.close();
} catch (IOException e1) {
Log.e(G.TAG, "serverSocket.close() e: " + e1.getMessage());
}
try {
socket.close();
} catch (IOException e1) {
Log.e(G.TAG, "socket.close() e: " + e1.getMessage());
}
}
Log.i(G.TAG, "after start recieve: ");
while (bRun) {
try {
Log.i(G.TAG, "while start: ");
String message = inputStream.readLine();
Log.i(G.TAG, "response message: " + message);
if (message != null) {
setListMessage(false, message);
}
}
catch (IOException e) {
bRun = false;
Log.e(G.TAG, "while bRun e: " + e.getMessage());
}
}
}
});
thread.start();
// in another function, my message is sent successfully from android and receive in windows
I found the problem, this line
socket = serverSocket.accept();
made the problem when I comment the line, the android app received the response!
Does anybody know why?
Related
I am done with connecting the Bluetooth to a paired device. After establishing the connection I want to move to the next activity, where the data to be sent are given by the user. so the main thing is, how can I send a string to an already connected Bluetooth device(from previous activity). Need help. I searched a lot. Couldn't find it.
Thanks in advance
BluetoothConnectingActivity
private Runnable serverListener = new Runnable() {
public void run() {
try //opening of BT connection
{
//problematic with older phones... HELP: Change server/client orientation...
//but solves: BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
Log.i("TrackingFlow", "Server socket: new way used...");
socket = (BluetoothSocket) remoteDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(remoteDevice, 1);
socket.connect();
CONNECTION_ENSTABLISHED = true; //protect from failing
} catch (Exception e) //obsolete way how to open BT
{
try {
Log.e("TrackingFlow", "Server socket: old way used...");
BluetoothServerSocket tmpsocket = adapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
socket = tmpsocket.accept();
CONNECTION_ENSTABLISHED = true; //protect from failing
Log.i("TrackingFlow", "Listening...");
} catch (Exception ie) {
Log.e(TAG, "Socket's accept method failed", ie);
ie.printStackTrace();
}
}
Log.i(TAG, "Server is ready for listening...");
runOnUiThread(new Runnable() {
#Override
public void run() { //Show message on UIThread
listItems.clear(); //remove chat history
listItems.add(0, String.format(" Server opened! Waiting for clients..."));
listAdapter.notifyDataSetChanged();
}
});
try //reading part
{
is = socket.getInputStream();
os = socket.getOutputStream();
new Thread(writter).start();
int bufferSize = 1024;
int bytesRead = -1;
byte[] buffer = new byte[bufferSize];
while (CONTINUE_READ_WRITE) //Keep reading the messages while connection is open...
{
final StringBuilder sb = new StringBuilder();
bytesRead = is.read(buffer);
if (bytesRead != -1) {
String result = "";
while ((bytesRead == bufferSize) && (buffer[bufferSize - 1] != 0)) {
result = result + new String(buffer, 0, bytesRead - 1);
bytesRead = is.read(buffer);
}
result = result + new String(buffer, 0, bytesRead - 1);
sb.append(result);
}
Log.e("TrackingFlow", "Read: " + sb.toString());
runOnUiThread(new Runnable() {
#Override
public void run() { //Show message on UIThread
Toast.makeText(ConnectToBluetoothActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
listItems.add(0, String.format("< %s", sb.toString())); //showing in history
listAdapter.notifyDataSetChanged();
}
});
}
} catch (IOException e) {
Log.e(TAG, "Server not connected...");
e.printStackTrace();
}
}
};
StringSendingActivity
public void sendString(String convertedString) { //send converted string
byte[] b = convertedString.getBytes();
try {
Toast.makeText(this, "Sending" + convertedString, Toast.LENGTH_SHORT).show();
os.write(b);
// list.add(0, "> " + et.getText().toString()); //chat history
list.add(0, et.getText().toString()); //chat history
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.message_textview, R.id.textview, list);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
et.setText(""); //remove text after sending
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Not sent", Toast.LENGTH_SHORT).show(); //usually problem server-client decision
}
}
Is there any good option to send broadcast message on UDP port while receiving data from other devices on the same port?
I have this currently:
First I initialize 1 DiagramSocket for both reading and sending:
private void startSocket(){
try {
if(socket == null) {
socket = new DatagramSocket(3040);
socket.setBroadcast(true);
socket.setReuseAddress(true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
In first Thread I have reading/receiving for current socket:
public class Server implements Runnable {
public Server(iBroadcastResponse iBResponse){
BroadcastClass.this.iBResponse = iBResponse;
}
#Override
public void run() {
startSocket();
while (DataHandler.isEmergencyMode.get() ) {
try {
byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
if(socket == null)
return;
socket.receive(packet);
String command = stringFromPacket(packet);
Log.d(TAG, command);
addToBroadcastCalls(command);
if(iBResponse != null)
iBResponse.onResponse();
} catch (Exception e) {
Log.e(TAG, "Server: Error!\n");
}
}
Log.e(TAG, "Stop!");
}
String stringFromPacket(DatagramPacket packet) {
return new String(packet.getData(), 0, packet.getLength());
}
}
If I run this, it works normally, reads receiving data. But if I try to send data in another thread:
socketThread = new Thread(){
#Override
public void run() {
startSocket();
String message = "Message";
byte[] buf = message.getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length);
try {
if(!socket.isConnected())
socket.connect(InetAddress.getByName("255.255.255.255"), 3040);
if (socket != null) {
socket.send(packet);
}
Log.d(TAG, "Send: " +message);
} catch (Exception e) {
e.printStackTrace();
}
}
};
socketThread.start();
When .send() is called it stops receiving data from other devices so now it only send message but stops receiving it from other devices.
Is there any good solution to this problem?
This was the solution
https://stackoverflow.com/a/25520279/1088975
I only changed
System.log...
To
Log.w(TAG, "Message...");
And I changed IP to be always "255.255.255.255" instead of
getBroadcastAddress()
which returns broadcast IP for current WIFI, which didn't work on my android devices
I'm new to Android, somewhat new to socket programming. I have two devices, running Android 5.1, connected with WiFi direct (not sure if that's relevant). I have a service where the server listens for a request on a socket, then returns a reply back to the client.
Likewise the client code sends a request and listens for the reply from the server. The server is sending the response, but the client never gets the message and the socket times out.
Server test code:
while (true) {
try {
Log.i(TAG, "test waiting for a request");
mServer = new ServerSocket(PORT);
Socket socket = mServer.accept(); //Block to receive message //
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.i(TAG, "Message received! " + in.readLine());
String msg = "This is my reply.";
OutputStream outputStream = socket.getOutputStream();
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println(msg);
out.flush();
out.close();
} catch (SocketException e) {
Log.e(TAG, "Socket Accept Interrupted", e);
} catch (IOException e) {
Log.e(TAG, "Socket Failure", e);
} finally {
if (mServer != null && mServer.isBound()) {
try {
mServer.close();
} catch (IOException ioException) {
Log.e(TAG, "Failed to close socket trying to recover from SocketException", ioException);
}
}
}
}
Client test code:
Socket socket = null;
SocketAddress addr = new InetSocketAddress(host, PORT);
int socketTOms = 5000;
try {
socket = new Socket(host, PORT);
socket.setKeepAlive(false);
String syncReq = "Request to server.";
//Send Request//
OutputStream outputStream = socket.getOutputStream();
outputStream.write(syncReq.getBytes());
socket.setSoTimeout(socketTOms);
//Rcv reply//
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.i(TAG, "Message received! " + in.readLine());
} catch (SocketTimeoutException e) {
Log.e(TAG, "Timeout while reading from socket: timeout=" + socketTOms);
} catch (Exception e) {
Log.e(TAG, "Exception", e);
} finally {
if (socket != null && socket.isConnected()) {
try {
socket.close();
} catch (IOException e) {
Log.e(TAG, "Exception while closing socket", e);
}
}
}
I'm running the server and client on two different devices through Android Studio and can see in the logs that the server receives the request and sends the reply, but the client always throws SocketTimeoutException. I saw else where that socket.setKeepAlive(false) would fix the problem, but it doesn't seem to have any effect.
Seems simple enough, but I can't see what I'm missing here.
May be try this line of code before infinite loop mServer = new ServerSocket(PORT);
Did you try to create thread in sever side app. This makes the process to run in parallel so that while server is waiting for request the application does not gets hang. First of all try this code for localhost . To find Inetaddress just use InetAddress.getLocalHost(). Then run this. For communication with different devices there is service provided that is called (NSD) (Network Service Discovary).
But if you want to run this way I have written a code for you.
Server side code
TextView textView;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
connect();
}
}
);
}
public void connect()
{
MyServer myServer= new MyServer();
myServer.setEventListener(this);
myServer.startListening();
}
#Override
public void Display(String message) {
textView.setText("Client - "+ message);
}
}
Client side code
TextView textView;
Button button;
Thread mThread;
Socket clientSocket;
Button sendBtn;
public String userText1;
ObjectOutputStream output;
EditText editText;
Object userText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textView);
button=(Button)findViewById(R.id.button);
sendBtn=(Button)findViewById(R.id.sendBtn);
editText=(EditText)findViewById(R.id.editText);
sendBtn.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
userText=editText.getText().toString();
start();
}
}
);
public void start()
{
mThread= new Thread(new Runnable() {
#Override
public void run() {
try {
clientSocket = new Socket("127.0.0.1", 2001);
Log.v("binaya", "client socket created");
output = new ObjectOutputStream(clientSocket.getOutputStream());
output.writeObject(userText);
Message serverObj = Message.obtain();
ObjectInputStream input = new ObjectInputStream(clientSocket.getInputStream());
String strMsg = input.readObject().toString();
serverObj.obj = strMsg;
mHandler.sendMessage(serverObj);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
mThread.start();
}
Handler mHandler= new Handler()
{
#Override
public void handleMessage(Message msg) {
msgDisplay(msg.obj.toString());
}
};
private void msgDisplay(String msg) {
textView.setText("Server - " + msg);
}
We have used handler because we cannot touch user interface from inside runnable in this case.
Thanks
Figured this out ....
On the client side I was using outputStream.write(...) to send the request to the server as in:
String syncReq = "Request to server.";
OutputStream outputStream = socket.getOutputStream();
outputStream.write(syncReq.getBytes());
But reading it on the server with BufferedReader.readLine():
Socket socket = mServer.accept(); //Block to receive message //
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.i(TAG, "Message received! " + in.readLine());
My problem was that outputStream.write(...) does not append a '\n' at the end of the String, but in.readLine() on the server expects it. Therefore the server was blocking while waiting for '\n'; which in turn caused the client socket to timeout.
I have a client/server socket program for android working through Direct WIFI connection.It includes several Request and Response between client and server phone. My code works perfectly for the first time but in second time client gets "Connection refused" and server "socket closed" error message.in third time it works again and in fourth time raise that errors and ....
Could anyone help me about this issue?
this is server code:
new Thread(){
public void run(){
ServerSocket welcomeSocket = null;
try {
//Create a socket and listen to that
try {
if(welcomeSocket == null)
welcomeSocket = new ServerSocket(COMMUNICATION_PORT);
}catch(Exception qwe){
Log.d("Info", "qwe:" + qwe.getMessage());
}
int i = 0;
while (i == 0) {
try {
//Waiting for client connection
socket = welcomeSocket.accept();
}catch(Exception qwe2){
Log.d("Info", "qwe2:" + qwe2.getMessage());
}
//i++;
new Thread(){
public void run(){
try {
InputStream is;
DataInputStream dIn;
OutputStream os;
DataOutputStream dOut;
//socket.setSoTimeout(SOCKET_TIMEOUT);
log("Connection accepted");
//Create an input stream for receiving data from server
is = socket.getInputStream();
dIn = new DataInputStream(is);
//Get an output stream for sending data to server phone
os = socket.getOutputStream();
dOut = new DataOutputStream(os);
boolean done = false;
byte messageType;
String message;
int serverReplyType = 0;
String serverReply = "NOP";
while (!done) {
messageType = dIn.readByte();
message = dIn.readUTF();
Log.d("Info", "Received: " + messageType);
Log.d("Info", "Received: " + message);
switch (messageType) {
//----------------------------------------
//Comparing Job ID and Location ID of client phone and server phone
case JOB_ID_CHECK_REQ:
String[] tokens = message.split("#");
serverReplyType = JOB_ID_CHECK_RESP;
if (Integer.parseInt(tokens[0]) == currentJob.getJobId() && Integer.parseInt(tokens[1]) == currentJob.getJobLocationId()) {
serverReply = "ACCEPTED";
log("Syncing data verified");
} else {
serverReply = "ID MISMATCH";
done = true;
log("It is not possible to sync none equal Jobs.");
log("Please select same Job in both phone.");
}
break;
//----------------------------------------
//Generating JSON string including Racks data
case RACK_LIST_REQ:
log("Sending Racks' data");
serverReplyType = RACK_LIST_RESP;
serverReply = racks.size() > 0 ? (new Gson()).toJson(racks) : "NOP";
break;
//----------------------------------------
//Inserting new Racks to local Db
case NEW_RACK_FOR_INSERT:
log("Receiving new Racks' data for insert");
if (!message.equals("NOP")) {
List<Rack> diffRacks = new Gson().fromJson(message, new TypeToken<List<Rack>>() {
}.getType());
rackDataSource rds = new rackDataSource(CrossPhoneSync.this);
rds.open();
rds.crossPhoneSync_InsertDiff(diffRacks);
rds.close();
}
serverReplyType = DONE;
serverReply = "NOP";
done = true;
break;
//----------------------------------------
case DONE:
done = true;
log("Done");
break;
}
Log.d("Info", "Send: " + serverReplyType);
Log.d("Info", "Send: " + serverReply);
dOut.writeByte(serverReplyType);
dOut.writeUTF(serverReply);
dOut.flush();
}
log("Operation completed!");
} catch (Exception e) {
log("Error: " + e.getMessage());
Log.d("Info", "ServerPhoneError:" + e.getMessage());
e.printStackTrace();
} finally {
//Close stream and socket
//closeSocket(true);
Disconnect(null);
}
log("Done!");
}
}.start();
}
}catch(Exception qw){
Log.d("Info", "Eq1:" + qw.getMessage());
}finally {
try {
if (welcomeSocket != null) {
welcomeSocket.close();
}
}catch (Exception e) {
Log.d("Info", "Close_ServerSocket#: " + e.getMessage());
}
}
}
}.start();
and client code:
Socket socket2 = null;
try {
InputStream is;
DataInputStream dIn;
OutputStream os;
DataOutputStream dOut;
//Create a socket for connecting to server phone
socket2 = new Socket(host, COMMUNICATION_PORT);
//socket.setSoTimeout(SOCKET_TIMEOUT);
//Get an output stream for sending data to server phone
os = socket2.getOutputStream();
dOut = new DataOutputStream(os);
//Create an input stream for receiving data from server
is = socket2.getInputStream();
dIn = new DataInputStream(is);
//Send first message
log("Sending verification data...");
int clientMessageType = JOB_ID_CHECK_REQ;
String clientMessageToSend = currentJob.getJobId() + "#" + currentJob.getJobLocationId();
dOut.writeByte(clientMessageType);
dOut.writeUTF(clientMessageToSend);
dOut.flush();
byte messageType;
String message;
boolean done = false;
while(!done) {
messageType = dIn.readByte();
message = dIn.readUTF();
Log.d("Info", "" + messageType);
Log.d("Info", message);
switch(messageType)
{
//----------------------------------------
//Sending current Job ID and Location ID to server phone for equality checking
case JOB_ID_CHECK_RESP:
if(message.equals("ACCEPTED")){
log("Data verified");
clientMessageType = RACK_LIST_REQ;
clientMessageToSend = "NOP";
log("Requesting for other phone's Racks data");
}else{
done = true;
log("It is not possible to sync none equal Jobs.");
log("Please select same Job in both phone.");
}
break;
//----------------------------------------
//Comparing...
case RACK_LIST_RESP:
log("Comparing Racks' data of two phones");
List<Rack> serverPhoneRacks;
if(!message.equals("NOP"))
serverPhoneRacks = new Gson().fromJson(message, new TypeToken<List<Rack>>() { }.getType());
else
serverPhoneRacks = new ArrayList<>();
rackDataSource rds = new rackDataSource(CrossPhoneSync.this);
rds.open();
List<Rack> diffRacks = rds.crossPhoneSync_Compare(racks,serverPhoneRacks);
rds.close();
clientMessageType = NEW_RACK_FOR_INSERT;
clientMessageToSend = diffRacks == null ? "NOP" : (new Gson()).toJson(diffRacks);
log("Sending new Racks' data for inserting on the other phone");
break;
//----------------------------------------
case DONE:
done = true;
log("Done!");
break;
}
if(!done) {
dOut.writeByte(clientMessageType);
dOut.writeUTF(clientMessageToSend);
dOut.flush();
}
}
log("Operation completed!");
}catch (Exception e) {
log("Error:" + e.getMessage());
e.printStackTrace();
Log.d("Info", " ClientPhoneError:" + e.getMessage());
}finally {
//Close stream and socket
//closeSocket(false);
try {
if (socket2 != null) {
//socket.shutdownInput();
//socket.shutdownOutput();
socket2.close();
}
}catch (Exception e) {
Log.d("Info", "Close_Socket_Error5: " + e.getMessage());
}
}
log("Finished!");
i've created an android application in which, android application act as the client, and server resides in the desktop application.
suppose there are 10 android application runs the same at a time on 10 different android tablets, when one updation received from one tablet, the desktop application sends the updation to all other remaining tablets. how could the server knows how many clients are connected and how to send the message to all the clients
what i plan is to run a server in all android so that when one updation received from one tablet, the desktop application sends the updation to all other remaining tablets.
can anyone please tell me some suggestion regarding this.
Client side
private int SERVER_PORT = 9999;
class Client implements Runnable {
private Socket client;
private PrintWriter out;
private Scanner in;
#Override
public void run() {
try {
client = new Socket("localhost", SERVER_PORT);
Log.d("Client", "Connected to server at port " + SERVER_PORT);
out = new PrintWriter(client.getOutputStream());
in = new Scanner(client.getInputStream());
String line;
while ((line = in.nextLine()) != null) {
Log.d("Client", "Server says: " + line);
if (line.equals("Hello client")) {
out.println("Reply");
out.flush();
}
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Server class
class ServerThread implements Runnable {
private ServerSocket server;
#Override
public void run() {
try {
server = new ServerSocket(SERVER_PORT);
Log.d("Server", "Start the server at port " + SERVER_PORT
+ " and waiting for clients...");
while (true) {
Socket socket = server.accept();
Log.d("Server",
"Accept socket connection: "
+ socket.getLocalAddress());
new Thread(new ClientHandler(socket)).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class ClientHandler implements Runnable {
private Socket clientSocket;
private PrintWriter out;
private Scanner in;
public ClientHandler(Socket clietSocket) {
this.clientSocket = clietSocket;
}
#Override
public void run() {
try {
out = new PrintWriter(clientSocket.getOutputStream());
in = new Scanner(clientSocket.getInputStream());
String line;
Log.d("ClientHandlerThread", "Start communication with : "
+ clientSocket.getLocalAddress());
out.println("Hello client");
out.flush();
while ((line = in.nextLine()) != null) {
Log.d("ClientHandlerThread", "Client says: " + line);
if (line.equals("Reply")){
out.print("Server replies");
out.flush();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
You could save the Sockets in a List and send a message through all the OutputStreams in that List:
Socket socket = Server.accept();
sockets.add(socket); //sockets is an ArrayList<Socket>
public void sendMessageToEveryone(String msg) {
for(Socket s : sockets) {
s.getOutputStream().write(msg.getBytes());
s.getOutputStream().flush();
}
You could use the Google Cloud Messaging service.
Take a look at this page:
https://developer.android.com/google/gcm/index.html