java网络编程(TCP)-客户端

public class Client {
public Client() {
super();
}

public static void main(String[] args) {
//建立客户端连接
try {
Socket s = new Socket("localhost", 9999);
PrintStream ps = new PrintStream(s.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
for (int i = 0; i < 10; i++) {
//写数据
ps.println("哈哈哈!");
//读数据
String line = br.readLine();
System.out.println(line);
}

} catch (UnknownHostException e) {
e.printStackTrace();
throw new RuntimeException("端口已经被占用了!");
} catch (IOException e) {
e.printStackTrace();
}
}
}

猜你喜欢

转载自www.cnblogs.com/lyq1025/p/10857619.html