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灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP