ToOtherUserThread.java 810 Bytes
Newer Older
Michele Fiori's avatar
Michele Fiori committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
package chat;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class ToOtherUserThread extends Thread {
    private Socket s;
    private Queue q;

    public ToOtherUserThread(Socket s, Queue q) {
        this.s = s;
        this.q = q;
    }

    public void run() {
        DataOutputStream outToServer = null;
        try {
            outToServer = new DataOutputStream(s.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        String newMsg;
        while(true) {
            newMsg = q.take();
            try {
                outToServer.writeBytes(newMsg + '\n');
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}