I am writing a simple client server application for android in java and running client and server application in emulators. My server is Listening at 10.0.2.15:4444 and i redirected localhost:8080(127.0.0.1:8080 of development machine) to 10.0.2.15:4444 . Client is getting connected with server and sending server a message "Hello Server !\r\n" but my server is not reading this message .
**Server Side Code **
public void run()
{
try
{
server_socket = new ServerSocket(port_number);
handle.post(new Runnable()
{
public void run()
{
text.append("ServerSocket object has been created successfuly...\n");
text.append("Waiting for clients...\n");
}
});
while(true)
{
Socket client = server_socket.accept();
handle.post(new Runnable()
{
public void run()
{
text.append("Client has come...\n");
}
});
try
{
BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()) );
String line ;
text.append("writing data....\n");
text.append("yes m ready...\n");
line = input.readLine();
text.append("yo i have come..\n");
if(line == null)
{
text.append("Nothing has been receieved...\n");
}
else
{
text.append(line);
while((line = input.readLine()) != null)
{
text.append(line);
}
}
}
catch(Exception e)
{
text.append("problem in receving data...\n");
}
}
}
catch(Exception e)
{
text.setText("Error in running thread.\n");
}
}
**Client Side Code **
public void run()
{
boolean connect;
try
{
Socket server = new Socket(ip_address,port_number);
text.append("connection has been made...\n");
connect = true;
try
{
text.append("Preparing to send data...\n");
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(server.getOutputStream()));
output.write("Hello server.!\r\n");
output.flush();
text.append("data has been sent...\n");
}
catch(Exception e)
{
text.append("Unable to send data...\n");
}
}
catch(Exception e)
{
text.append("Unable to connect with server...\n");
}
}
My client application is sending data and showing Data has been sent...(i wrote this line for doing debugging) and my server is not reading data and showing writing data... . I have tried these threads
buffered reader not receiving data from socket
Sockets, BufferedReader.readline() - why the stream is not ready?
BufferedReader.readline() return nothing in result in android
BufferedReader.readline() return nothing in result in android
but nothing is working for me . When i used BufferedReader.ready() then also it was blocking at BurreferedReader.ready() (input.ready()) . Can any one tell me what is wrong with my code ?