- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class Server
- {
- private ServerSocket ss;
- private Socket socket;
- private BufferedReader in;
- private PrintWriter out;
- public Server()
- {
- try
- {
- ss = new ServerSocket(10000);
- System.out.println("The server is waiting your input...");
- while(true)
- {
- socket = ss.accept();
- in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- out = new PrintWriter(socket.getOutputStream(), true);
- String line = in.readLine();
- System.out.println("you input is : " + line);
- //out.println("you input is :" + line);
- out.close();
- in.close();
- socket.close();
- if(line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("exit"))
- break;
- }
- ss.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args)
- {
- new Server();
- }
- }
客户端 java的
- import java.io.*;
- import java.net.*;
- public class Client
- {
- Socket socket;
- BufferedReader in;
- PrintWriter out;
- public Client()
- {
- try
- {
- socket = new Socket("127.0.0.1", 10000);
- in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- out = new PrintWriter(socket.getOutputStream(), true);
- BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
- out.println(line.readLine());
- line.close();
- out.close();
- in.close();
- socket.close();
- } catch (IOException e) {
- }
- }
- public static void main(String[] args)
- {
- new Client();
- }
- }
转载自: