本次学习聊天器的客户端代码,有了上一次的基础,这一次的学习会变得很快。
下面是完整代码:
import java.io.*;
import java.net.*;
public class SingleTalkClient {
public static void main(String[] args) throws IOException
{
Socket client = null;
PrintWriter out = null;
BufferedReader in = null;
try {
client = new Socket("127.0.0.1", 4444);
out = new PrintWriter(client.getOutputStream(), true); //auto flush
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: 127.0.0.1.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: 127.0.0.1."