java 板


LINE

Server(出問題那行用紅色標出了) import java.net.*; import java.io.*; import java.lang.Thread; public class Server { public static void main(String args[]) throws java.io.IOException { while(true){ ServerThread thread = new ServerThread(); thread.start(); } } } class ServerThread extends Thread { public void run() { try{ ServerSocket sv_socket = new ServerSocket(12345); //宣告一個ServerSocket使用Port 12346 Socket cl_socket = new Socket(); //宣告一個Socket 建立起和Server的Connection System.out.println("等待客戶端連線....."); while(true) { //接收部分 cl_socket = sv_socket.accept(); //ServerSocket接受接收 System.out.println("客戶端從:"+cl_socket.getInetAddress()+":"+cl_socket.getPort()+"連線"); InputStream input = cl_socket.getInputStream(); //接收Client傳送內容 sv_socket.close(); int port_length = input.read(); //read Client Port length byte[] port_byte = new byte[port_length]; for(int i = 0 ; i < port_byte.length ; i++) { port_byte[i] = (byte)input.read(); //read Client Port } int client_port = Integer.parseInt(new String(port_byte)); int get_length = input.read(); //read 算式長度 byte[] byte_temp = new byte[get_length]; System.out.println(client_port); for(int i = 0 ; i < byte_temp.length ; i++) { byte_temp[i] = (byte)input.read(); //read Client送出之算式 } String str = new String(byte_temp); System.out.print(str); //運算部分 char str_array[] = str.toCharArray(); int i = 0 , ans = 0, num1 = 0, num2; boolean bool = false; while(i < str_array.length) { //運算算式 switch(str_array[i]) { case '+': { System.out.print("get"); String num[] = str.split("\\+"); num1 = Integer.parseInt(num[0]); num2 = Integer.parseInt(num[1]); ans = num1+num2; System.out.println("ans:"+ans); bool = true; i = str_array.length; break; } //省略其他算式判斷 default: { i++; break; } } } if(bool = true) { //送出運算結果部分 byte int_byte[]; String int_str; int_str = String.valueOf(ans); int_byte = int_str.getBytes(); System.out.println("Client Port:"+client_port); try{ Socket send_socket = new Socket(cl_socket.getInetAddress(), client_port); //建立socket連接Client System.out.println("Connected?"+send_socket.isConnected()); OutputStream ops = send_socket.getOutputStream(); ops.write(int_byte); //送出答案 send_socket.close(); } catch(BindException e){ System.out.println(e.toString()); System.out.println(e.getMessage()); } catch(ConnectException e){ System.out.println(e.toString()); System.out.println(e.getMessage()); } catch(Exception e){ System.out.println(e.toString()); System.out.println(e.getMessage()); } bool = false; } } } catch(Exception e){} } } Client部分 import java.net.*; import java.io.*; import java.util.Scanner; public class Client { public static void main(String arg[]) throws UnknownHostException { try { Scanner input = new Scanner(System.in); //建立Scanner做為鍵盤輸入 ServerSocket sv_socket = new ServerSocket(0); //宣告一個ServerSocket使用電腦空閒的Port System.out.print("請輸入Server IP:"); String address; address = input.nextLine(); Socket socket = new Socket(address, 12345); //宣告一個Socket 建立起和Server的Connection System.out.println(socket.getLocalAddress()); int port = sv_socket.getLocalPort(); //取得ServerSocket所使用的空閒Port System.out.println("port:"+port); String str_port = Integer.toString(port); //將數字Port型態轉換為String byte[] port_byte = str_port.getBytes(); //將Port型態轉換為Byte OutputStream ops = socket.getOutputStream(); //new OutputStream ops.write(port_byte.length); //將Port byte的長度送出 ops.write(port_byte); //輸出Port的byte串流 System.out.print("請輸入算式:"); String str; str = input.nextLine(); byte[] byte_temp = str.getBytes(); //將輸入字串轉成byte ops.write((byte)str.length()); //送出算式字串的byte長度 ops.write(byte_temp); //輸出算式byte串流 socket = sv_socket.accept(); //ServerSocket接受接收 InputStream input_stream = socket.getInputStream(); byte[] get_byte = new byte[100]; int get_byte_long = input_stream.read(get_byte); //接收byte串流(Server回覆內容) byte[] byte_ans = new byte[get_byte_long]; for(int i = 0 ; i < get_byte_long ; i++) { byte_ans[i] = get_byte[i]; } str = new String(byte_ans); //將接收的byte轉回字串輸出 System.out.println("運算結果為:"+str); } catch(IOException e) { } } } -- ★小天使 嗚嗚嗚...壞人請你饒我一命 To 小天使: 饒命可以...但是要...嘿嘿嘿嘿嘿 To 小天使: XDDDDDDDDDDDDD ★小天使 要幹嘛><? To 小天使: 要..........給我P幣.....XDDDDDD --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.131.66.40
1F:→ tkcn:這樣貼 code 很難看! 這份 code 架構有問題,TCP 也有誤解 11/22 16:53
2F:→ tkcn:官方tutorial:http://tinyurl.com/2beqz8e 特別是文末sample 11/22 16:55
3F:→ a73126:了解~感謝這份文件....這部分算式自己摸索API寫的所以.... 11/22 17:25
4F:推 qrtt1:if(a = b) 的型式能過 compiler 嗎@@? 11/23 09:26
5F:→ tkcn:boolean 可以過,不過一定是 true 11/23 09:50







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:BabyMother站內搜尋

TOP