多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 互聯網 > Java網絡編程之多線程Client-Server

Java網絡編程之多線程Client-Server

來源:程序員人生   發布時間:2014-12-13 08:40:43 閱讀次數:3182次

前面空話過了,現在就直接看代碼吧!

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(); } }


是否是給你帶來了1點靈感呢?


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲视频一区二区在线观看 | 亚洲天堂视频在线免费观看 | 亚洲日本在线观看网址 | 国产h肉在线视频免费观看 国产h视频 | 免费一级毛片在播放视频 | 欧美视频在线一区二区三区 | 天天在线天天综合网色 | 亚洲精品美女久久777777 | 一区二区三区亚洲 | 色成人亚洲 | 免费观看欧美一级毛片 | h网在线观看 | 日韩欧美印度一级毛片 | 五月天伊人| 欧美第5页 | 精品影视网站入口 | 欧美人乱大交xxxxx | 亚洲精品美女久久久久 | 国产一区亚洲欧美成人 | 手机午夜看片 | 狠狠干一区 | 欧美高清揄拍自拍视频网站 | 日本卡1卡2卡4卡免费 | 久久久高清日本道免费观看 | 中文字幕精品一区二区精品 | 久久永久免费视频 | 国产在线观看福利片 | 国产一区二区高清在线 | 日本三级日本三级日本三级极 | 亚洲综色 | 国产mv在线观看 | 视频在线亚洲 | 午夜视频在线看 | 黄大片日本一级在线a | free性日韩 | 美女网站免费看 | 午夜欧美成人久久久久久 | 欧美一区二区精品系列在线观看 | 日本免费三区 | 国产视频一区二区在线观看 | 亚洲国产成人久久精品影视 |