本帖最后由 梁航斌 于 2013-4-7 17:05 编辑
- import java.net.*;
- import java.io.*;
- //客户端
- class Client
- {
- public static void main(String []args) throws Exception{
- Socket s = new Socket(InetAddress.getByName("192.168.1.4"),10000);
- //控制台输入
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- //获取输入输出流
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- BufferedReader sbr = new BufferedReader(new InputStreamReader(s.getInputStream()));
- //发送和接收
- String sline = null;
- String line = null;
- while((line = br.readLine())!=null){
- bw.write(line);
- bw.newLine();
- bw.flush();
- sline = sbr.readLine();
- System.out.println(sline);
- if(line.equals("over"))
- break;
- }
- }
- }
复制代码- import java.net.*;
- import java.io.*;
- //服务端
- class Server
- {
- public static void main(String []args) throws Exception{
-
- ServerSocket ss = new ServerSocket(10000);
- Socket s = ss.accept();
- //连接反馈
- String ip = s.getInetAddress().getHostAddress();
- System.out.println(ip+"....连接成功");
- //获取客户端的输入输出流
- BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
- //接收和发送
- String line = null;
- while((line = br.readLine())!=null){
- System.out.println(line);
- bw.write("server: "+line.toUpperCase());
- bw.newLine();
- bw.flush();
- }
- s.close();
- ss.close();
- }
- }
复制代码 |
|