/Users/mw17785/Projects/gamefinder/gamefinder/trunk/src/example/Client.java |
package example;
import cs101.comm.Announcer;
import cs101.comm.GameInfo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
public class Client {
Socket sock;
public Client() {
try {
GameInfo gi = Announcer.find("SocketTest");
if (gi == null) {
System.out.println("Cancelled");
return;
}
System.out.println("Found "+gi.getDescription()+" at "+
gi.getHostName()+" on port "+gi.getPort());
sock = new Socket(gi.getHostName(), gi.getPort());
OutputStream os = sock.getOutputStream();
InputStream is = sock.getInputStream();
PrintStream ps = new PrintStream(os);
BufferedReader br =
new BufferedReader(new InputStreamReader(is));
ps.print("Hello there\r\n");
String line = br.readLine();
System.out.println(line);
sock.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(String[] args) {
new Client();
}
}