本帖最后由 张俊生 于 2013-9-3 22:34 编辑
- import java.io.*;
- import java.net.*;
- class TcpClient
- {
- public static void main(String[] args) throws Exception
- {
- //建立Socket对象
- Socket s = new Socket("192.168.3.130",10005);
- //定义读取键盘数据的流对象
- BufferedReader bufr =
- new BufferedReader(new InputStreamReader(System.in));
- //定义目的,将数据写入到Socket输出流,发给服务端。
- BufferedWriter bufOut =
- new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- //定义一个Socket读取流,读取服务端返回的大写信息
- BufferedReader bufIn =
- new BufferedReader(new InputStreamReader(s.getInputStream()));
- String line = null;
- while ((line = bufr.readLine()) != null)
- {
- if ("over".equals(line))
- {
- break;
- }
- bufOut.write(line);
- bufOut.newLine();
- bufOut.flush();
-
- String str = bufIn.readLine();
- System.out.println("server: "+str);
- }
- bufr.close();
- s.close();
-
- }
- }
- class TransServer
- {
- 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 + ".....conneted");
- //读取socket读取流中的数据
- BufferedReader bufIn =
- new BufferedReader(new InputStreamReader(s.getInputStream()));
- //目的 socket 输出流,将大写数据写入到socket输出流,并发送给客户端
- BufferedWriter bufOut =
- new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- String line = null;
- while ((line = bufIn.readLine()) !=null)
- {
- System.out.println(line);
- line = myreverse(line);
- bufOut.write(line);
- bufOut.newLine();
- bufOut.flush();
- }
- s.close();
- ss.close();
- }
- public static String myreverse(String str){
-
- char[] arr = str.toCharArray();
- int low = 0;
- int hight = arr.length-1;
- for (int i =0;low<hight ;i++ )
- {
- char temp = arr[low];
- arr[low] = arr[hight];
- arr[hight] = temp;
- low++;
- hight--;
- }
- return new String(arr);
- }
- }
复制代码 |
|