目前我使用以下代码与服务器进行交互
public String connectToserverforincomingmsgs(String phonurl, String phno)
throws IOException {
URL url = new URL(phonurl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
// Allow Outputs
con.setDoOutput(true);
con.connect();
BufferedWriter writer = null;
writer = new BufferedWriter(new OutputStreamWriter(
con.getOutputStream(), "UTF-8"));
// give server your all parameters and values (replace param1 with you
// param1 name and value with your one's)
writer.write("sender_no=" + phno);
writer.flush();
String responseString = "";
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
responseString = responseString.concat(line);
}
con.disconnect();
return responseString;
}
我怎么能做tcp连接。现在我什么都不知道。我是android和java的新手,所以任何关于tcp连接的示例代码都会受到赞赏
答案 0 :(得分:3)
要创建TCP连接,您需要使用套接字:
Socket socket = new Socket(host_name_or_ip_address, port_no);
使用socket.getOutputStream()
使用socket.getInputStream()
答案 1 :(得分:0)
只需用Socket替换HttpURLConnection即可。它的工作原理基本相同