public class TransClient {
/**
* @param args
* 练习:客户端输入字母数据,发送给服务端,服务端收到后显示在控制台,并将该数据转换成大写返回给客户端,直到客户端输入over,转换结束
* 思路:
* 1,需要现有socket端点,
* 2,客户端的数据源是键盘输入,目的是socket
* 3,需要将服务端发回的信息接收,源是socket
* 4,将数据显示再打印到控制台
* 5,在这些操作过程中的数据,都是文本数据,因此用字符流操作
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s=new Socket(InetAddress.getLocalHost().getHostAddress(),10004);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
while((line=br.readLine())!=null){
if("over".equals(line+"\r\n")){
break;
}
out.write(line);
out.flush();
System.out.println(in.readLine());
}
s.close();
}
}
public class TransServer {
/**
* @param args
* 练习:客户端输入字母数据,发送给服务端,服务端收到后显示在控制台,并将该数据转换成大写返回给客户端,直到客户端输入over,
* 转换结束
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10004);
Socket s = ss.accept();
String address = s.getInetAddress().getHostAddress();
System.out.println(address + "...connected");
BufferedReader brin = new BufferedReader(new InputStreamReader(
s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
String text = null;
while ((text = brin.readLine()) != null) {
text = text.toUpperCase();
out.write(text+"\r\n");
out.flush();
}
s.close();
ss.close();
}
TCP协议练习——转换
最新推荐文章于 2023-12-28 19:47:29 发布