Java网络编程之多线程Client-Server
2014-12-04 02:37
381 查看
前面废话过了,现在就直接看代码吧!
ThreadedClient.java
是不是给你带来了一点灵感呢?
ThreadedClient.java
package exercise01; import java.io.*; import java.net.*; public class ThreadedClient { private String hostname; private int port; Socket socket = null; public ThreadedClient(String hostname, int port){ //constructor of the Client class this.hostname = hostname; this.port = port; } public void connect() throws UnknownHostException, IOException{ System.out.println("Attempting connect to "+ hostname +":"+port); socket = new Socket(hostname,port); System.out.println("Connection established!"); } public void readResponse() throws IOException{ String userInput; BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); System.out.println("Response from the server:"); while((userInput = reader.readLine() )!= null ){ System.out.println(userInput); } } public void askForTime() throws IOException{ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); writer.write("Time?"); writer.newLine(); writer.flush(); } public static void main(String[] argv){ //create an object for the current class Client ThreadedClient client = new ThreadedClient("localhost",8181); try{ //trying to establish a connection to the server client.connect(); //ask the server for time client.askForTime(); //if connection succeed, return the input contents client.readResponse(); }catch(UnknownHostException ukhe){ //if the host not found System.err.println("Host unknown! Connection can not be established!"); }catch(IOException ioe){ //if the server doesn't work System.err.println("Connection can not be established! The server may not be on! Check the error message! "+ioe.getMessage()); } } }ThreadedServer.java
package exercise01; import java.io.*; import java.net.*; import java.util.Date; public class ThreadedServer { private ServerSocket serverSocket; private int port; public ThreadedServer(int port){ this.port = port; } //get the thread started public void start() throws IOException{ System.out.println("Starting the server at the port: "+ port); serverSocket = new ServerSocket(port); //instantial an object client Socket client = null; //set the condition to be true for endless loops while(true){ System.out.println("Waiting for clients..."); client = serverSocket.accept(); System.out.println("The following client has connected: " + client.getInetAddress().getCanonicalHostName()); //A client has connected to the server Thread thread = new Thread(new SocketClientHandler(client)); thread.start(); } } /** * instantial an object of class ThreadedServer and start the server * * @param argv */ public static void main(String[] argv){ //set the default port number int port = 8181; try{ //initializing the socket server ThreadedServer threadedServer = new ThreadedServer(port); threadedServer.start(); }catch(IOException ioe){ ioe.printStackTrace(); } } } class SocketClientHandler implements Runnable{ private Socket client; public SocketClientHandler(Socket client){ this.client = client; } /** * get the thread started by the run method */ @Override public void run() { try{ System.out.println("Thread started with name: " + Thread.currentThread().getName()); readResponse(); }catch(IOException ioe){ ioe.printStackTrace(); }catch(InterruptedException ie){ ie.printStackTrace(); } } /** * read the response from the server * @throws IOException * @throws InterruptedException */ private void readResponse() throws IOException, InterruptedException { String userInput; BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream())); while(( userInput = reader.readLine())!= null){ if(userInput.equals("Time?")){ System.out.println("Request to send time! Sending time current."); sendTime(); break; } } } /** * send the current time to the server * @throws IOException * @throws InterruptedException */ private void sendTime() throws IOException, InterruptedException { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream())); writer.write(new Date().toString()); writer.flush(); writer.close(); } }
是不是给你带来了一点灵感呢?
相关文章推荐
- Java在Client/Server网络中的应用-Java基础-Java-编程开发
- Java 在Client/Server 网络中的应用-Java基础-Java-编程开发
- 一个java网络编程Socket的例子,实现Server与Client聊天
- 使用Java网络编程创建基于TCP协议的Server和Client
- JAVA 网络编程 Socket server client 编程
- 使用Java网络编程创建基于UDP协议的Server和Client
- java网络编程____httpclient多线程请求和线程池
- java网络编程(Client/Server)基础知识
- Java笔记(9)-网络编程、URL、InetAddress、套接字、Socket、ServerSocket、多线程、UDP、广播数据包、远程调用
- java.nio.*进行网络编程 serverSocketchanel socketchanel
- 【2013】实习收获3:python网络编程--client端与server端入门(server)
- Java网络编程精解之ServerSocket用法详解二1
- java 基TCP的多线程 网络编程
- java网络编程之Http多线程下载应用实例
- java网络编程之TCP/IP ——SocketServer与Socket
- 【2013】实习收获2:python的网络编程--client端与server端入门(client端)
- java网络编程、多线程
- Java网络编程基础 ServerSocket类使用
- Java在Client/Server网络中的应用
- Java 在Client/Server 网络中的应用