public class TcpClient2 {
public static void main(String[] args) throws Exception {
Socket s = new Socket("127.0.0.1", 10013);
OutputStream out = s.getOutputStream();
out.write("服务端你好!".getBytes());
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
System.out.println(new String(buf, 0, len));
}
s.close();
}
}
class TcpServer2 {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10013);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + ".....connected");
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
System.out.println(new String(buf, 0, len));
OutputStream out = s.getOutputStream();
out.write("客户端你好!".getBytes());
out.flush();