Reputation: 11
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();
}
}
}
}};
}
Upvotes: 1
Views: 1212
Reputation: 237
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.
Upvotes: 0
Reputation: 10948
After the dataOutputStream.writeUTF()
line, write dataOutputStream.flush();
. That will send all the data through.
Upvotes: 3