2013-02-14 77 views
2

我是新的套接字编程。如何连接到特定的IP和端口在android

我想在Android中连接到特定的IP(例如:184.267.345.65)&端口(例如:53),这怎么可能?

在此先感谢。

+0

的可能重复的[实施例:使用的AsyncTask的Android双向网络套接字(http://stackoverflow.com/questions/5135438/example-android-bi-directional-network-socket-using-asynctask) – SztupY 2013-02-15 11:20:28

回答

4
Socket s = new Socket("184.267.345.65", 53) 

参见hereherehere为完整的示例代码

-2
URLConnection connection = null; 
URL url = null; 
InputStream is = null; 
String mUrl="IP ADDRESS AND PORT NUMBER"; 

try { 
url = new URL(mUrl); 

    connection = url.openConnection(); 
    connection.setRequestProperty("Accept", "SPECIFY ACCEPT TYPE"); 
is = connection.getInputStream(); 
String data=convertStreamToString(is); 

} catch (Exception e) { 
    Log.i("Exception while reading inputstream From URL :"+mUrl, e.getMessage()); 
    e.printStackTrace(); 
} 
+1

这更适合通过HTTP请求连接到服务器**不是用于套接字通信 – iTech 2013-02-14 11:42:22

相关问题