package it.cast.tcp;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/*
*服务端接受从键盘上输入的数据
*为什么服务端接受不到信息呢?
*/
public class Server2 {
public static void main(String[] args) throws IOException {
// 创建ServerSocket对象
ServerSocket ss = new ServerSocket(10001);
Socket s = ss.accept();
while(true){
// 获取客户端套接字对象
// 获取输入流
InputStream in = s.getInputStream();
byte[] bys = new byte[1024];
int len = in.read(bys);
System.out.println(new String(bys, 0, len));
}
}
}
public class Client2 {
public static void main(String[] args) throws IOException {
// 创建客户端套接字
Socket s = new Socket("192.168.1.145", 10001);
// 从键盘录入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while ((line = br.readLine()) != null) {
if ("over".equals(line)) {
break;
}
// 与服务端建立连接,并获取输出流
OutputStream out = s.getOutputStream();
// 发送数据
out.write(line.getBytes());
}
}
}
|