日期:2014-05-20 浏览次数:20672 次
import java.io.*; import java.net.*; public class Server { public static void main(String[] args) throws IOException{ ServerSocket server=new ServerSocket(5678); Socket client=server.accept(); BufferedReader in= new BufferedReader(new InputStreamReader(client.getInputStream())); DataOutputStream out=new DataOutputStream(client.getOutputStream()); while(true){ String str="Your input is:"+in.readLine(); System.out.println(str); if(str.equals("end")) { break; } byte[] b=str.getBytes(); out.write(b,0,b.length); out.flush(); } in.close(); out.close(); client.close(); } }
import java.net.*; import java.io.*; public class Client{ static Socket server; public static void main(String[] args)throws Exception{ server=new Socket(InetAddress.getLocalHost(),5678); DataInputStream in = new DataInputStream(server.getInputStream()); PrintWriter out=new PrintWriter(server.getOutputStream()); BufferedReader wt=new BufferedReader(new InputStreamReader(System.in)); while(true){ String str=wt.readLine(); out.println(str); out.flush(); if(str.equals("end")){ break; } byte[] b = new byte[in.available()]; for(int i = 0;i < b.length;i++){ b[i] = (byte)in.read(); } String s = new String(b); System.out.println(s); } in.close(); out.close(); wt.close(); server.close(); } }