[問題] 簡單ClientSocket 建立的問題,懇求板ꐠ…
最近在練習簡單的Socket的程式,Server端已經寫好,但Client端的都照著書上打,
卻還是無法建立ClientSocket,看了好久,還是不知道原因,懇求版大大幫忙解答
簡短程式碼如下:
-----------------------------------------------------------------------------
import java.io.*;
import java.net.*;
public class ClientSocketTest {
Socket cs;
int port;
ClientSocketTest(String host){
port = 0;
try{
System.out.println("未建立");
cs = new Socket(InetAddress.getByName(host), port);
System.out.println("已建立");
}
catch(IOException ex){
System.out.println("error!!");
}
}
public void clientInfo(){
try{
InetAddress addr = cs.getLocalAddress().getLocalHost();
System.out.println("Client Information: ");
System.out.println("Local Host: "+cs.getLocalAddress().getLocalHost());
System.out.println("Host name : "+addr.getHostName());
System.out.println("IP address: "+addr.getAddress());
System.out.println("Peot : "+cs.getLocalPort());
System.out.println("============================");
}
catch(IOException ex){}
}
}
public class JavaInternetTest {
public static void main(String[] args) {
ClientSocketTest cst = new ClientSocketTest("localhost");
//cst.clientInfo();
}
}
-----------------------------------------------------------------------------
印出結果如下
未建立
error!!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.128.18.102
※ 編輯: baconcsie 來自: 140.128.18.102 (04/08 04:20)
推
04/08 08:35, , 1F
04/08 08:35, 1F
→
04/08 09:38, , 2F
04/08 09:38, 2F
推
04/08 10:05, , 3F
04/08 10:05, 3F
→
04/08 10:09, , 4F
04/08 10:09, 4F
→
04/08 10:17, , 5F
04/08 10:17, 5F
→
04/08 13:09, , 6F
04/08 13:09, 6F
→
04/08 13:10, , 7F
04/08 13:10, 7F
→
04/08 13:35, , 8F
04/08 13:35, 8F
→
04/08 13:36, , 9F
04/08 13:36, 9F
→
04/08 13:53, , 10F
04/08 13:53, 10F
→
04/08 15:56, , 11F
04/08 15:56, 11F
→
04/08 15:59, , 12F
04/08 15:59, 12F
→
04/08 16:01, , 13F
04/08 16:01, 13F
→
04/08 16:03, , 14F
04/08 16:03, 14F
→
04/08 16:04, , 15F
04/08 16:04, 15F
→
04/08 16:07, , 16F
04/08 16:07, 16F
→
04/08 16:09, , 17F
04/08 16:09, 17F
→
04/08 16:13, , 18F
04/08 16:13, 18F
→
04/08 16:19, , 19F
04/08 16:19, 19F
→
04/08 20:32, , 20F
04/08 20:32, 20F