- class TranClient{
- public static void main(String[] args)throws Exception{
- Socket s=new Socket("对方IP地址",端口号);
- BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
- BufferedWriter bufwout=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- BufferedReader bufrin=new BufferedReader(new InputStreamReader(s.getInputStream()));
- String line=null;
- while((line=bufr.readLine())!=null){
- if("over".equals(line))
- break;
- bufwout.write(line);
- bufwout.newLine();
- bufwout.flush();
- String str=bufrin.readLine();
- System.out.println("Server :"+str);
- }
- bufr.close();
- s.close();
- }
- }
- class TranServer{
- public static void main(String[] args)throws Exception{
- ServerSocket ss=new ServerSocket(10005);
- Socket s=ss.accept();
- String ip=s.getInetAddress().getHostAddress();
- System.out.println(ip+"--connected");
- BufferedReader bufrin=new BufferedReader(new InputStreamReader(s.getInputStream()));
- BufferedWriter bufwout=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- String line=null;
- while((line=bufrin.readLine())!=null){
- System.out.println(line);
- bufwout.write(line.toUpperCase());
- bufwout.newLine();
- bufwout.flush();
- }
- s.close();
- ss.close();
- }
- }
复制代码 |