Alexis Heaton Alexis Heaton - 21 days ago 6
Android Question

Android SDK, Output Stream from client to server doesn't work

So I was just seeing if I could build a dead simple chat application, and im facing a problem... I have the server running on my pc and the client on my phone, Once the client has connected to my pc I can then go to my pc and write as many messages as I want and they will appear on my phone (client), but when I try to send a message from my phone it will only send one message then no more, any ideas?

Server:

static OutputStream out;
static InputStream in;
String name = "Server";

public static void main(String args[]){
System.out.println("Server On");

//Server
new Thread(new Runnable(){
@Override
public void run(){
new Main().start();
}}).start();
//Scanner for text input
new Main().scrnn();
}

public void scrnn(){
Scanner lol = new Scanner(System.in);
if(lol.hasNext()){
try {
out.write((name + ": " +lol.nextLine().toString()).getBytes());
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
scrnn();
}

public void start(){
try{
ServerSocket sok = new ServerSocket(42069);
while(true){
Socket sk = sok.accept();
in = sk.getInputStream();
out = sk.getOutputStream();
byte[] buffer = new byte[1024];
in.read(buffer);
String recievedData = new String(buffer);
System.out.println(recievedData);
out.write(recievedData.getBytes());
}
}catch(Exception e){
e.printStackTrace();
}
}


And the client:(android phone)

OutputStream out;
InputStream in;
Socket sk;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new Object[]{"Thanks for using Monsterchat"};

Button button = (Button)findViewById(R.id.buttonSend);
listView = (ListView) findViewById(R.id.listView);

new Thread(new Runnable() {
@Override
public void run() {
try {
sk = new Socket("10.0.0.9", 42069);
sk.setKeepAlive(true);

out = sk.getOutputStream();
in = sk.getInputStream();

//This will send fine but if you look at the button onclick it wont send that :(
out.write((name + " Joined the group!").getBytes());

//This is how i can send constant messages from the server though
listenForData();
}catch(Exception e){
e.printStackTrace();
}
}
}).start();

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try {
out.write("succ".getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
});
}

public void listenForData() throws Exception{
byte[] buffer = new byte[1024];
in.read(buffer);
final String recievedData = new String(buffer);
runOnUiThread(new Runnable() {
@Override
public void run() {
//Update listView with new message
buildList(recievedData);
}
});
//Repeat
listenForData();
}


Sorry there's a lot of code, and also nothing gets output in the logcat that looks wrong

Answer
    while(true){
        Socket sk = sok.accept();
        in = sk.getInputStream();
        out = sk.getOutputStream();
    Dr    byte[] buffer = new byte[1024];
        in.read(buffer);
        String recievedData = new String(buffer);
        System.out.println(recievedData);
        out.write(recievedData.getBytes());

That loop is wrong. After one received message the server drops sk and will go waiting for a new client.

You better try;

        Socket sk = sok.accept();
        in = sk.getInputStream();
        out = sk.getOutputStream();
        byte[] buffer = new byte[1024];
    while(true){
        int nread = in.read(buffer);
        String recievedData = new String(buffer, 0, nread);
        System.out.println(recievedData);
        out.write(recievedData.getBytes());