Question

Can you please write this java code in Scala. import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import...

Can you please write this java code in Scala.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

public class SimpleSocketClientExample
{
    public static void main( String[] args )
    {
        if( args.length < 2 )
        {
            System.out.println( "Usage: SimpleSocketClientExample <server> <path>" );
            System.exit( 0 );
        }
        String server = args[ 0 ];
        String path = args[ 1 ];

        System.out.println( "Loading contents of URL: " + server );

        try
        {
            // Connect to the server
            Socket socket = new Socket( server, 80 );

            // Create input and output streams to read from and write to the server
            PrintStream out = new PrintStream( socket.getOutputStream() );
            BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );

            // Follow the HTTP protocol of GET <path> HTTP/1.0 followed by an empty line
            out.println( "GET " + path + " HTTP/1.0" );
            out.println();

            // Read data from the server until we finish reading the document
            String line = in.readLine();
            while( line != null )
            {
                System.out.println( line );
                line = in.readLine();
            }

            // Close our streams
            in.close();
            out.close();
            socket.close();
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.BufferedReader

import java.io.InputStreamReader

import java.io.PrintStream

import java.net.Socket

//remove if not needed
import scala.collection.JavaConversions._

object SimpleSocketClientExample {

def main(args: Array[String]): Unit = {
    if (args.length < 2) {
      println("Usage: SimpleSocketClientExample <server> <path>")
      System.exit(0)
    }
    val server: String = args(0)
    val path: String = args(1)
    println("Loading contents of URL: " + server)
    try {
// Connect to the server
      val socket: Socket = new Socket(server, 80)
// Create input and output streams to read from and write to the server
      val out: PrintStream = new PrintStream(socket.getOutputStream)
      val in: BufferedReader = new BufferedReader(
        new InputStreamReader(socket.getInputStream))
// Follow the HTTP protocol of GET <path> HTTP/1.0 followed by an empty line
      out.println("GET " + path + " HTTP/1.0")
      out.println()
// Read data from the server until we finish reading the document
      var line: String = in.readLine()
      while (line != null) {
        println(line)
        line = in.readLine()
      }
// Close our streams
      in.close()
      out.close()
      socket.close()
    } catch {
      case e: Exception => e.printStackTrace()

    }
}

}

Add a comment
Know the answer?
Add Answer to:
Can you please write this java code in Scala. import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT