日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

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

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

來源:程序員人生   發(fā)布時間:2014-12-08 08:53:48 閱讀次數(shù):3521次

       最近在寫這個計算機網絡,然后要求我們自己去寫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下就能夠跑了。


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产精品久久久久久久久久久久冷 | 精品日韩一区二区三区 | 午夜久久av| com毛片 | 亚洲成人高清在线 | 天堂网在线最新版www中文网 | 成人一级片 | 国产精品久久久久久久9999 | 亚洲一区二区视频在线 | 玖玖综合九九在线看 | 国产一区二区高清 | 日本精品在线 | 亚洲国产高清在线 | 久久久久久久久一区二区 | 国产精品一二三四区 | 99re6热只有精品免费观看 | 久久欧美| 黄色黄色黄色 | 毛片免费观看视频 | 99一区二区 | 欧美一区二区三区在线免费观看 | 国产精品久久久久久久久免费高清 | 国产精品一区二区三区av | a视频免费 | 91精品久久久久久久久久入口 | 日韩欧美国产综合 | 色诱亚洲精品久久久久久 | 国产精品久久久久久久久久小说 | 亚洲免费一区二区 | 麻豆av福利av久久av | 色一情一乱一伦一区二区三区 | 欧美一级精品 | 国产精品色一区二区三区 | 99热久久是国产免费66 | 精品美女久久久久久免费 | 日本免费高清一区 | 精品国产乱码久久久久久丨区2区 | 免费av网站在线看 | 国产成年人免费视频 | 中文字幕不卡在线播放 | 国产网红女主播免费视频 |