import java.io.*;
import java.net.*;
import java.util.*;
public class ClientA
{
public static void main(String[] args)
{
try
{
// Create socket & address, and connect
Socket s = new Socket();
InetSocketAddress address = new InetSocketAddress("www.google.com",80);
s.connect(address);
System.out.println("Connected: " + s); // just for our own info
try
{
// Send request to server & close socket for output
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("GET / HTTP/1.0\n");
s.shutdownOutput();
// Read response and echo to terminal
Scanner in = new Scanner(s.getInputStream());
while(in.hasNextLine())
System.out.println(in.nextLine());
}
finally { s.close(); }
}
catch (IOException e) { e.printStackTrace(); }
}
}
oregano, using port 10001.
import java.io.*;
import java.net.*;
import java.util.*;
public class ServerB
{
public static void main(String[] args)
{
try
{
ServerSocket s = new ServerSocket(10001);
Socket next = null;
try
{
// Accept connection request & set up input/output streams
next = s.accept();
Scanner in = new Scanner(next.getInputStream());
PrintWriter out = new PrintWriter(next.getOutputStream(),true);
// Read line and return capitalized (terminate with BYE)
while(in.hasNextLine())
{
String line = in.nextLine();
if (line.equals("BYE")) {
System.out.println("Terminating: " + next);
break;
}
out.println(line.toUpperCase());
}
}
finally { s.close(); if (next != null) next.close(); }
}
catch (IOException e) { e.printStackTrace(); }
}
} |
import java.io.*;
import java.net.*;
import java.util.*;
public class ClientB
{
public static void main(String[] args)
{
try
{
// Create socket & address
Socket s = new Socket();
InetSocketAddress address =
new InetSocketAddress("chipotle.cs.usna.edu",10001);
// Connect
s.connect(address);
System.out.println("Connected: " + s);
try
{
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("the rain in spain falls mainly on the plain\nBYE");
s.shutdownOutput();
Scanner in = new Scanner(s.getInputStream());
while(in.hasNextLine())
System.out.println(in.nextLine());
}
finally { s.close(); }
}
catch (IOException e) { e.printStackTrace(); }
}
}
|
import java.io.*;
import java.net.*;
import java.util.*;
public class ServerB
{
public static void main(String[] args)
{
try
{
// Create server socket & deal with incoming connection requests
ServerSocket s = new ServerSocket(10001);
try {
while(true)
{
Socket next = s.accept();
Thread t = new Thread(new ServeOne(next));
t.start();
}
}
finally { s.close(); }
}
catch (IOException e) { e.printStackTrace(); }
}
static class ServeOne implements Runnable
{
Socket s;
public ServeOne(Socket sk) { s = sk; }
public void run()
{
try
{
try
{
// Set up I/O for the socket
Scanner in = new Scanner(s.getInputStream());
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
// Read line & print capitalized (or exit with BYE)
while(in.hasNextLine())
{
String line = in.nextLine();
if (line.equals("BYE")) {
System.out.println("Terminating: " + s);
break;
}
out.println(line.toUpperCase());
}
}
finally { s.close(); }
}
catch(IOException e) { e.printStackTrace(); }
}
}
} |
import java.io.*;
import java.net.*;
import java.util.*;
public class ClientB
{
public static void main(String[] args)
{
try
{
// Create socket & address
Socket s = new Socket();
InetSocketAddress address =
new InetSocketAddress("chipotle.cs.usna.edu",10001);
// Connect
s.connect(address);
System.out.println("Connected: " + s);
try
{
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
out.println("the rain in spain falls mainly on the plain\nBYE");
s.shutdownOutput();
Scanner in = new Scanner(s.getInputStream());
while(in.hasNextLine())
System.out.println(in.nextLine());
}
finally { s.close(); }
}
catch (IOException e) { e.printStackTrace(); }
}
}
|