1

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();
}
}
}
}};
}
asked Jun 5, 2011 at 13:59

2 Answers 2

3

After the dataOutputStream.writeUTF() line, write dataOutputStream.flush();. That will send all the data through.

answered Jun 5, 2011 at 14:06
Sign up to request clarification or add additional context in comments.

Comments

0

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.

answered Jun 5, 2011 at 15:05

4 Comments

Thanks so much for taking the time to reply . This does help tho it is not what im after . It seems that my client is connected only when it wants to send data hence it wont recieve the data after the client has sent "time". I think i need the client to be looping waiting for incomming data. Will this not work as i get feeling sockets cant transmit and recieve at the same time ? cheers again
Yes you have to loop the client as well. You send something to the server an immediately read from the stream. This will wait for incoming data. Good luck ;)
Ive now put up my basic client code , can you have a quick look and maybe suggest how i can adapt this code to have a reading loop in there and still be able to write data out. Thanks again for your time.
I would put the whole try..catch block inside a while(true) loop.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.