本帖最后由 丘凤光 于 2013-4-18 10:17 编辑
早上练习TCP传输时,能连接上,运行也出结果,但是获取的ip地址怎么和我的网络ip不一致呢?求解.
运行结果:
代码:- /*
- 需求:TCP传输练习,客户端向服务端放松字符,服务端返回其大写
- */
- import java.io.*;
- import java.net.*;
- //服务端
- class Server3
- {
- public static void main(String[] args) throws Exception
- {
- ServerSocket ss=new ServerSocket(10000);
- String ip=ss.getInetAddress().getHostAddress();
- System.out.println(ip+"——————is connected successfully!");
- System.out.println(ss.getInetAddress());
- Socket s=ss.accept();
- //获取socket的输出流
- BufferedWriter bwOut=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- //获取socket的输入流
- BufferedReader brIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
- String line=null;
- while((line=brIn.readLine())!=null)
- {
- bwOut.write(line.toUpperCase());
- bwOut.newLine();
- bwOut.flush();
- }
- }
- }
- //客户端
- class Client3
- {
- public static void main(String[] args) throws Exception
- {
- Socket s=new Socket("192.168.1.106",10000);
- //键盘录入
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- //获取socket的输出流
- BufferedWriter bwOut=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- //获取socket的输入流
- BufferedReader brIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
- String line=null;
- while((line=br.readLine())!=null)
- {
- if("over".equals(line))
- break;
- bwOut.write(line);
- bwOut.newLine();
- bwOut.flush();
-
-
- System.out.println(brIn.readLine());
- }
- //关流、管资源
- s.close();
- br.close();
- }
- }
复制代码 |