Re: [問題] 找不出原因的SocketException

看板java作者 (Jalen)時間15年前 (2010/11/22 16:41), 編輯推噓1(104)
留言5則, 3人參與, 最新討論串2/2 (看更多)
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

11/22 16:53, , 1F
這樣貼 code 很難看! 這份 code 架構有問題,TCP 也有誤解
11/22 16:53, 1F

11/22 16:55, , 2F
官方tutorial:http://tinyurl.com/2beqz8e 特別是文末sample
11/22 16:55, 2F

11/22 17:25, , 3F
了解~感謝這份文件....這部分算式自己摸索API寫的所以....
11/22 17:25, 3F

11/23 09:26, , 4F
if(a = b) 的型式能過 compiler 嗎@@?
11/23 09:26, 4F

11/23 09:50, , 5F
boolean 可以過,不過一定是 true
11/23 09:50, 5F
文章代碼(AID): #1CwYp3oJ (java)
文章代碼(AID): #1CwYp3oJ (java)