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

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

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

來源:程序員人生   發布時間:2014-12-08 08:53:48 閱讀次數:3518次

       最近在寫這個計算機網絡,然后要求我們自己去寫1個服務器,1開始1頭霧水!不過后來1查資料想通了,哈哈~然后自己就啪啪啪敲出來了。好啦,閑言少敘,直接看代碼!

Client.java

package exercise01; import java.io.*; import java.net.*; public class Client { private String hostname; private int port; Socket socket = null; public Client(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 static void main(String[] argv){ //create an object for the current class Client Client client = new Client("localhost",8181); try{ //trying to establish a connection to the server client.connect(); //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()); } } }

Server.java

package exercise01; import java.net.*; import java.io.*; public class Server { private ServerSocket serverSocket; private int port; public Server(int port){ this.port = port; } public void startServer() throws IOException{ System.out.println("Starting the socket server at the port: " + port); serverSocket = new ServerSocket(port); //Listen the clients. Block until one connects System.out.println("Waiting for clients..."); Socket client = serverSocket.accept(); //A client has connected and send the welcome message sendMessage(client); } //send the display message private void sendMessage(Socket client) throws IOException { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream())); writer.write("Hello. You are connected to a Simple Socket Server."); writer.flush(); writer.close(); } /** * Create a server object and start the server * * @param argv */ public static void main(String[] argv){ int port = 8181; //start the server try{ Server server = new Server(port); server.startServer(); }catch(IOException ioe){ ioe.printStackTrace(); } } }

自己將包名改1下就能夠跑了。


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: jizz 在线观看免费 | 国产一区二区三区四区 | 中文字幕乱码无限2021丫丫 | 成人性生活视频 | 成人免费看黄页网址大全 | 精品久久一区二区三区 | 另类 欧美 视频二区 | 国产欧美日韩第一页 | 亚洲色图欧美在线 | 无码免费一区二区三区免费播放 | 午夜美女影院 | 国产精品视频国产永久视频 | 日本动漫片b站免费观看 | 中文字幕乱偷乱码亚洲 | 亚洲欧美日韩网站 | 亚洲色中文字幕在线播放 | 亚洲欧美偷拍视频 | 亚洲精品国产男人的天堂 | 国产深夜福利在线观看网站 | 亚洲国产欧美一区 | 欧美精品在线一区 | 日本中文字幕在线观看视频 | 香蕉乱码成人久久天堂爱免费 | 蜜桃色永久入口 | 亚洲精品一区二区三区婷婷月 | 欧美在线精品一区二区三区 | 亚洲综合二区 | 欧美亚洲图片小说 | 久久久久久久久人体 | 亚洲一区二区精品推荐 | 亚洲国产一区二区三区最新 | 一区二区三区四区无限乱码在线观看 | 波多野结衣在线视频观看 | 日韩欧美一及在线播放 | 亚洲欧美精品一区 | 国产福利亚洲 | av在线影院 | www.操操| 天天做天天爱天天综合网 | 欧美精品久久久久久久免费观看 | 黄色a大片 |