ServerSocket not accepting Connection - java

I have the following code in JAVA to create Server Socket to listen for incoming connections :
public class ConnectOracle {
public static void main(String[] args) {
String driver = "oracle.jdbc.driver.OracleDriver"; //
String serverName = "10.11.201.84";
String portNumber = "1521";
String db = "XE";
String url = "jdbc:oracle:thin:#" + serverName + ":" + portNumber + ":"
+ db; // connectOracle is the data
// source name
String user = "ORAP"; // username of oracle database
String pwd = "ORAP"; // password of oracle database
Connection con = null;
ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
try {
Class.forName(driver);// for loading the jdbc driver
System.out.println("JDBC Driver loaded");
con = DriverManager.getConnection(url, user, pwd);// for
// establishing
// connection
// with database
Statement stmt = (Statement) con.createStatement();
serverSocket = new ServerSocket(8888);
System.out.println("Listening :8888");
while (true) {
try {
socket = serverSocket.accept();
System.out.println("Connection Created");
dataInputStream = new DataInputStream(
socket.getInputStream());
dataOutputStream = new DataOutputStream(
socket.getOutputStream());
System.out.println("ip: " + socket.getInetAddress());
// System.out.println("message: " +
// dataInputStream.readUTF());
ResultSet res=stmt.executeQuery("select * from food_category");
while(res.next()){
System.out.println(res.getString(1));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
In my Android program I am trying to create a connection with this JAVA program . The code is as follows :
class ConnectToOracle extends AsyncTask<String, String, String> {
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
#Override
protected void onPreExecute() {
super.onPreExecute();
;
}
// Download Music File from Internet
#Override
protected String doInBackground(String... f_url) {
try {
socket = new Socket("10.11.201.84", 8888);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF(textOut.getText().toString());
textIn.setText(dataInputStream.readUTF());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
// Once Music File is downloaded
#Override
protected void onPostExecute(String file_url) {
// Dismiss the dialog after the Music file was downloaded
}
}
But the connection is not established . How can I establish connection ? Any advice is of great help .

Related

Can't user getOutputStream and getInputStream in a method [duplicate]

This question already has an answer here:
java.net.SocketException socket is closed
(1 answer)
Closed 5 years ago.
I have a application to connect 2 device using socket. In Client, i try send and receive data. When i send data from Client, the Server can receive data and reply a other message. But when i open InputStream to catch receive from server, it show notify (IOException: java.net.SocketException: Socket is closed).
Please help me. ##
CODE IN CLIENT:
Socket socket = null;
String mes = message;
try {
socket = new Socket(dstAddress, dstPort);
/*Example send data to server*/
ops = socket.getOutputStream();
ps = new PrintStream(ops);
ps.print(mes);
ps.close();
/*End of example*/
/*Receive data from server*/
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
/*End of Receive data from server*/
Log.e("Receive From Server:", response);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
AND THIS CODE OF SERVER
private class SocketServerThread extends Thread {
int count = 0;
#Override
public void run() {
try {
serverSocket = new ServerSocket(socketServerPORT);
while (true) {
Socket socket = serverSocket.accept();
count++;
/*example get input String*/
try {
isr = new InputStreamReader(socket.getInputStream());
br = new BufferedReader(isr);
request = br.readLine();
Log.e("Mess-H.a", request);
}catch (Exception e){
Log.e("Can't receive data:", e.getMessage() );
}
message += "#" + count + " from "
+ socket.getInetAddress() + ":"
+ socket.getPort() + request + "\n";
/*End of example*/
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
activity.msg.setText(message);
}
});
SocketServerReplyThread socketServerReplyThread = new SocketServerReplyThread(
socket, count);
socketServerReplyThread.run();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class SocketServerReplyThread extends Thread {
private Socket hostThreadSocket;
int cnt;
SocketServerReplyThread(Socket socket, int c) {
hostThreadSocket = socket;
cnt = c;
}
#Override
public void run() {
OutputStream outputStream;
String msgReply = "Hello from Server, you are #" + cnt;
try {
outputStream = hostThreadSocket.getOutputStream();
PrintStream printStream = new PrintStream(outputStream);
printStream.print(msgReply);
printStream.close();
message += "replayed: " + msgReply + "\n";
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
activity.msg.setText(message);
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
message += "Something wrong! " + e.toString() + "\n";
}
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
activity.msg.setText(message);
}
});
}
}
You closed the socket and then continued to use it.
ps.close();
Here. Remove.

Unexpected End Of Stream error in reading xml data with InputStream of HttpsURLConnection

public class MainActivity extends Activity
{
Button submit;
EditText textBox;
String xmlRequest = "<Request><Merchant_Number>111111111111</Merchant_Number><Terminal_ID>001</Terminal_ID><Action_Code>05</Action_Code><Trans_Type>N</Trans_Type><POS_Entry_Mode>S</POS_Entry_Mode><Track_Data2>71019520xxxxxx=</Track_Data2></Request>";
String domain = "www.somedomain.com/gateway.php";
String domainForSocket = "https://" + domain;
URL a;
String sucess = "sucess";
HttpsURLConnection urlConnection;
String header;
String writeData; // data which will be sent to server
byte[] outputInBytes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button) findViewById(R.id.button1);
textBox = (EditText) findViewById(R.id.editText1);
writeData = xmlRequest;
textBox.setText(writeData);
// creating thread to run on background for network connection
final Thread mThread = new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
a = new URL(domainForSocket);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
outputInBytes = xmlRequest.getBytes("UTF-8");
urlConnection = (HttpsURLConnection) a.openConnection();
urlConnection
.setFixedLengthStreamingMode(outputInBytes.length);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Content-Length", ""
+ outputInBytes.length);
urlConnection.setRequestProperty("Accept-Encoding", "");
urlConnection.setRequestProperty("Content-Language",
"en-US");
urlConnection.setUseCaches(false);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
// urlConnection.setDoInput(true);
urlConnection.setInstanceFollowRedirects(false);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Log.v(sucess, "connectio open");
try {
OutputStream out = urlConnection.getOutputStream();
out.write(outputInBytes, 0, outputInBytes.length);
out.close();
// Send request
Log.v(sucess, "output sent");
} catch (Exception e) {
e.printStackTrace();
Log.e("Output Error", "error in sending data to server");
}
try {
InputStream a = urlConnection.getInputStream();
InputStreamReader read = new InputStreamReader(a);
BufferedReader br = new BufferedReader(read);
String data = "";
// Log.v("code execute", "i am here");
StringBuilder total = new StringBuilder();
StringBuffer ab = new StringBuffer();
Log.v("result", "if you dont see then no data");
while ((data = br.readLine()) != null) {
total.append(data);
Log.v("data", data.toString());
}
a.close(); // closing input string
} catch (Exception e) {
e.printStackTrace();
Log.e("Data Receiving Error",
"Error in receiving dat from server");
}
finally {
// urlConnection.disconnect();
try {
// mSocket.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("Socket close Error", "cant close socket");
}
}
// sending output
}
});
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mThread.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
Above is my complete source code for XML HTTP Request. I getting XML response from the server. But I am getting the below error in logcat whenever I send request to the server. Can anyone help me on this issue.
Any help will be appreaciated.
Here is the log:
02-17 11:09:33.684: W/System.err(6446): java.net.ProtocolException: unexpected end of stream
02-17 11:09:33.689: W/System.err(6446): at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:445)
02-17 11:09:33.690: W/System.err(6446): at com.android.okio.RealBufferedSource$1.read(RealBufferedSource.java:168)
I found the solution.
I was not opening socket with android's inbuilt SSL socket library
javax.net.SocketFactory fact=SSLSocketFactory.getDefault();
socket=fact.createSocket(addr, port);
This two line code wasted my four precious days :(

Convert input stream from Bluetooth to string

I'm making a app which can send a character ("B") to activate microcontroller then accept the result data from it. The data will be numbers 0-100. However I'm stuck in converting stream data to string.
I've tried the codes in comment but the process ended up being looping forever.
Here is my codes :
public Bluetooth(startover so, Handler handler, ProgressDialog pd) {
// TODO Auto-generated constructor stub
this.handler=handler;
activity=so;
this.pd=pd;
}
#Override
public void run() {
// TODO Auto-generated method stub
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.enable();
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
try
{
btSocket =device.createRfcommSocketToServiceRecord(MY_UUID);
//Toast.makeText(this, "Bluetooth_socket is created", Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Log.e(TAG, "ON RESUME: Socket creation failed.", e);
}
try
{
btSocket.connect();
Log.e(TAG, "ON RESUME: BT connection established, datatransfer link open.");
//Toast.makeText(this, "Bluetooth_socket is connected", Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
try
{
btSocket.close();
}
catch (IOException e2)
{
Log.e(TAG, "ON RESUME: Unable to close socket during connection failure", e2);
}
}
message = "B";
sendMessage(message);
InputStream mInput=null;
try {
mInput = btSocket.getInputStream();
System.out.println("Input Open");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(!Thread.currentThread().isInterrupted() && mBluetoothAdapter.isEnabled()) {
try {
int bytesAvailable = mInput.available();
if(bytesAvailable > 0) {
System.out.println("Pesan Diterima");
BufferedReader r = new BufferedReader(new InputStreamReader(mInput));
StringBuilder total = new StringBuilder(bytesAvailable);
String line;
//BACA LEBIH DARI 1 KARAKTER
//while ((line = r.readLine()) != null) {
// total.append(line);
//}
final String message=total.toString();
handler.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
pd.dismiss();
System.out.println("Turn "+message);
activity.startActivity(new Intent(activity.getApplicationContext(),result.class).putExtra("data", message));
}
});
break;
}
}
catch (IOException ex) {
}
}
super.run();
}
private void sendMessage (String message) {
byte[] send = message.getBytes();
try {
OutputStream OutStream = btSocket.getOutputStream();
OutStream.write(send);
System.out.println("Send "+message);
}
catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
}

ObjectOutputStream method writeObject hangs on android

I write some client-server communication.
My server:
public class Server {
public synchronized static void sendPacket(Packet packet,
ObjectOutputStream server) {
try {
server.writeObject(packet);
server.flush();
} catch (IOException e) {
Log.d(TAG, "Error while sending a packet. Output stream is unaviable.");
}
}
public synchronized static Packet readPacket(ObjectInputStream sourceStream) {
Packet recivedPacket = null;
try {
recivedPacket = (Packet) sourceStream.readObject();
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return recivedPacket;
}
/** Register user on the server */
private User registerUser(Socket socket) {
ClientUserLoginPacket newUserPacket = null;
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
try {
Log.i(TAG, "Opening output stream...");
oos = new ObjectOutputStream(socket.getOutputStream());
if (oos != null)
Log.d(TAG, "Output stream opened");
Log.i(TAG, "Opening input stream...");
ois = new ObjectInputStream(socket.getInputStream());
if (ois != null)
Log.d(TAG, "Input stream opened");
} catch (StreamCorruptedException e1) {
Log.e(TAG, "Error while opening stream");
} catch (IOException e1) {
e1.printStackTrace();
}
// First packet MUST be register request
try {
Log.d(TAG, "Waiting for login packet from client...");
newUserPacket = (ClientUserLoginPacket) readPacket(ois);
Log.d(TAG, "Login packet from recived...");
} catch (Exception e) {
Log.e(TAG, "Can't recive login packet.");
}
User newUserInstance = null;
// TODO check if exists. or to map in the future
if (newUserPacket != null) {
newUserInstance = new User(socket, ois, oos, newUserPacket.nick);
users.add(newUserInstance);
Log.d(TAG, "User " + newUserPacket.nick + " registered.");
Server.sendPacket(new ServerLoginAcceptedPacket(), oos);
Log.d(TAG, "User accept confirmation sent.");
}
return newUserInstance;
}
#Override
public void run() {
Log.i(TAG, "Starting server...");
ServerSocket server;
try {
server = new ServerSocket(PORT);
Log.i(TAG, "Server started.");
server.setSoTimeout(0);
while (true) {
Log.i(TAG, "Waiting for players...");
final Socket socket = server.accept();
Log.i(TAG, "New player connected.");
new Thread(new Runnable() {
#Override
public void run() {
Log.i(TAG, "Try to register new player.");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
User user = registerUser(socket);
while (true) {
Log.i(TAG, "Waiting for packets from " + user.nick+"...");
Packet packet = readPacket(user.ois);
Log.i(TAG, "Packet from " + user.nick + " recived.");
if (packet instanceof ...) {
...
}
}
}
}).start();
}
} catch (IOException e) {
Log.i(TAG, "Port is busy.");
}
}
private class User {
public Socket connection;
public ObjectInputStream ois;
public ObjectOutputStream oos;
public String nick;
public boolean inGame;
public User(Socket socket, ObjectInputStream ois,
ObjectOutputStream oos, String nick) {
this.connection = socket;
this.ois = ois;
this.oos = oos;
this.nick = nick;
}
// ...
}
My client:
public class Client {
callbackHandler = new Thread(new Runnable() {
#Override
public void run() {
while (true) {
Log.e(TAG, "Waiting for incomeing packets...");
Packet packet = (Packet) Server.readPacket(serverInput);
Log.e(TAG, "Packet recived.");
if (packet instanceof ServerLoginAcceptedPacket) {
Log.e(TAG, "Recived packet is "
+ packet.getClass().toString());
Intent intent = new Intent(MyActivity.this,
MainMenuActivity.class);
MyActivity.this.startActivity(intent);
}
}
}
});
public void connectToServer() {
SocketAddress sockaddr = new InetSocketAddress(mEditTextIp.getText()
.toString(), Server.PORT);
server = new Socket();
try {
server.setSoTimeout(1000);
Log.d(TAG, "Connecting to server.");
server.connect(sockaddr, Server.PORT);
Log.d(TAG, "Connected to server.");
} catch (IOException e) {
Log.e(TAG, "Can't connect to server.");
server = null;
}
if (server != null)
try {
server.setSoTimeout(0);
Log.d(TAG, "Opening output stream...");
serverOutput = new ObjectOutputStream(server.getOutputStream());
if (serverOutput != null)
Log.d(TAG, "Output stream opened");
else
Log.e(TAG, "Error while opening output stream");
} catch (IOException e) {
Log.e(TAG, "Server socket probably closed");
}
}
public void requestLogin() {
new Thread(new Runnable() {
#Override
public void run() {
Log.e(TAG, "Sending login packet...");
Server.sendPacket(new ClientUserLoginPacket(mEditTextLogin
.getText().toString(), ""), serverOutput); // TODO send
// pass and
// email
Log.e(TAG, "Login packet send");
}
}).start();
}
public void authenticate(View v) {
if (server == null)
connectToServer();
if (server != null) {
requestLogin();
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
serverInput = new ObjectInputStream(server.getInputStream());
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (serverInput != null) {
Log.e(TAG, "Start reciving callbacks...");
callbackHandler.start();
} else {
Log.d(TAG, "Can't open input stream to server.");
}
}
}
public void runServer(View v) {
new Thread(new Server()).start();
Toast.makeText(this, "Server running...", 1000).show();
}
}
Where runServer() and authenticate() functions are triggered with button.
Problem is that after server recive ClientLoginPacket, all subsequent sentPacket functions hangs on oos.writeObject().
I think the order of reading/writing from/to streams may be wrong.
What should be correct order of opening streams and writing objects to them?
Do I have to write something to ObjectOutputStream before opening ObjectInputStream?
After few hours I found that keywords synchronized before my methods readPacket() and sendPacket() were problem. ;)

Java sockets server help

Hi there im very new to java programing and am really stuck getting a socket server and client working how i want.
The problem im having is...
server:
Im looking at the output from the client and once client prints "TIME" the server will return a message eg "the time is...". The server does this but not straight away it seems to send it on the second time you send a message from the client.
Is this becuase the client is not connected all the time maybe ?
Im pretty sure this method is wrong can anyone give me some advice.
Any help would be great .
Luke
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args){
ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
try {
serverSocket = new ServerSocket(8888);
System.out.println("Listening :8888");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
try {
socket = serverSocket.accept();
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
dataOutputStream = new DataOutputStream(socket.getOutputStream());
System.out.println("ip: " + socket.getInetAddress());
System.out.println("message: " + dataInputStream.readUTF());
dataOutputStream.writeUTF("Hello!");
try{
String line = in.readLine();
if (line.contains("TIME")){
dataOutputStream.writeUTF("TIME IS....."); // ITS HERE THE PROBLEM MAY BE ?
{
} catch (IOException e){
System.out.println("Read failed");
System.exit(1);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if( socket!= null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( dataInputStream!= null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if( dataOutputStream!= null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
The Android Client
UPDATE , i think the problem is with the client only being connected when you send a message. How do i have a reading loop in here that wont affect when i send data out from the client.
package com.exercise.AndroidClient;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidClient extends Activity {
EditText textOut;
TextView textIn;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textOut = (EditText)findViewById(R.id.textout);
Button buttonSend = (Button)findViewById(R.id.send);
textIn = (TextView)findViewById(R.id.textin);
buttonSend.setOnClickListener(buttonSendOnClickListener);
}
Button.OnClickListener buttonSendOnClickListener
= new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
socket = new Socket("192.168.1.101", 8888);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF(textOut.getText().toString());
textIn.setText(dataInputStream.readUTF());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}};
}
After the dataOutputStream.writeUTF() line, write dataOutputStream.flush();. That will send all the data through.
It seems you’re reading from the stream to output the message.
System.out.println("message: " + dataInputStream.readUTF());
So the command has been removed from the stream when you try to run
String line = in.readLine();
You should try to read the command from the stream into a variable before outputting the message and then check if that string contains "TIME".
Also why use two different methods to read from the stream? You could just use the BufferedReader and discard DataInputStream. You could as well use PrintWriter instead of DataOutputStream.
Something like this:
out = new PrintWriter( socket.getOutputStream(), true );
and then:
out.println( "Time is..." );
Hope this helps.

Categories