CS-465 / testEcho / Server / src / echo / EchoServerThread.java
EchoServerThread.java
Raw
package echo;


import java.io.*;
import java.net.Socket;

// objects of this class work on one request
class EchoServerThread implements Runnable {

    Socket client;
    
    DataInputStream fromClient;
    DataOutputStream toClient;
    
    int charFromClient;
    int state = 0;

    // output streams to destination web server and requesting client
    EchoServerThread(Socket client) {
        this.client = client;
    }

    @Override
    public void run() {
        
        // first get the streams
        try {
            this.fromClient = new DataInputStream (client.getInputStream ());
            this.toClient   = new DataOutputStream(client.getOutputStream());
        } catch (IOException e) {
        }

        // now talk to the client
        while (true) {
            try {
                charFromClient = fromClient.readByte();
            } catch (IOException e) {
            }

            try {
                toClient.writeByte(charFromClient);
            } catch (IOException e) {
            }
            
            if(charFromClient == 'q')
            {
                break;
            }
        }

        try {
            client.close();
        } catch (IOException e) {
        }
    }
}