Server
package tcp.http;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket tcpServerSocket = new ServerSocket(8080);
Scanner scanner = new Scanner(System.in);
while (true) {
Socket clientSocket = tcpServerSocket.accept();
InetAddress clientAddress = clientSocket.getInetAddress();
int clientPort = clientSocket.getPort();
System.out.printf("有客户端连接上来 %s:%d%n",
clientAddress.getHostAddress(), clientPort);
// 获取 字节流
InputStream is = clientSocket.getInputStream();
// 字节流转换为字符流
InputStreamReader isReader = new InputStreamReader(is, "UTF-8");
// 字符流转换缓冲字符流
BufferedReader reader = new BufferedReader(isReader);
// 获取输出字节流
OutputStream os = clientSocket.getOutputStream();
PrintStream out = new PrintStream(os, true, "UTF-8");
String line = reader.readLine();
System.out.println("好友说: " + line);
System.out.print("请回复> ");
String response = scanner.nextLine();
out.println(response);
clientSocket.close();
}
}
}
Client
package tcp.http;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
Socket tcpClientSocket = new Socket();
byte[] ipv4 = { (byte)192, (byte)168, (byte)0, (byte)107};
InetAddress serverAddress = InetAddress.getByAddress(ipv4);
SocketAddress serverSocketAddress
= new InetSocketAddress(serverAddress, 8080);
tcpClientSocket.connect(serverSocketAddress);
while (true) {
System.out.print("请输出> ");
String request = scanner.nextLine();
// 通过字节流直接写入请求
OutputStream os = tcpClientSocket.getOutputStream();
PrintStream out = new PrintStream(os, true, "UTF-8");
out.println(request);
out.flush();
// 通过字节流,直接读取数据
InputStream is = tcpClientSocket.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "UTF-8")
);
String response = reader.readLine();
System.out.println(response);
}
}
}