- import java.io.*;
- import java.util.*;
- import java.net.*;
- class TcpClient
- {
- public static void main(String[] args)throws Exception
- {
- Socket s = new Socket("192.168.118.1",10005);
- BufferedOutputStream bufw =
- new BufferedOutputStream(s.getOutputStream());
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int len=0;
- while((len=br.read())!=-1)
- {
- bufw.write(len);
- }
- s.shutdownOutput();
- BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String len1 = bufr.readLine();
- System.out.println(len1);
- s.close();
- bufw.close();
- bufr.close();
- }
- }
- class TcpServer
- {
- public static void main(String[] args)throws Exception
- {
- ServerSocket ss = new ServerSocket(10005);
- Socket s = ss.accept();
- ArrayList<String> list = new ArrayList<String>();
- BufferedInputStream fr = new BufferedInputStream(s.getInputStream());
- int len=0;
- String writeResult =null;
- byte[] buf = new byte[1024];
- while((len=fr.read(buf))!=-1)
- {
- String target = new String(buf,0,len);
- String[] temp = target.split("");
- for(int i=0;i<temp.length;i++)
- {
- list.add(temp[i]);
- }
- Collections.reverse(list);
- for(int j=0;j<list.size();j++)
- {
- writeResult +=list.get(j);
- }
- }
- s.shutdownInput();
- BufferedWriter bufw =
- new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- bufw.write(writeResult);
- bufw.flush();
- bufw.close();
- }
- }
复制代码 |
|