日期:2014-05-20 浏览次数:20770 次
import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args)throws Exception { ServerSocket server = new ServerSocket(1331); Socket socket = server.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String fromClient; while((fromClient = in.readLine()) != null){ System.out.print("message form client:"+fromClient+"\n"); } } }
import java.io.IOException; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class Client { public static void main(String[] args) throws UnknownHostException, IOException { Socket socket = new Socket("127.0.0.1",1331); OutputStream out = socket.getOutputStream(); Scanner sin = new Scanner(System.in); while(sin.hasNextLine()){ out.write(sin.nextLine().getBytes()); out.flush(); } out.close(); socket.close(); } }