Back to project page Common-Library.
The source code is released under:
Apache License
If you think the Android project Common-Library listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.morgan.library.snippet; // w w w.j ava 2 s .com import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; /** * Socket???????? * * @author Morgan.Ji * */ public class SocketClient { static Socket client; public SocketClient(String site, int port) { try { client = new Socket(site, port); System.out.println("Client is created! site:" + site + " port:" + port); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public String sendMsg(String msg) { try { BufferedReader in = new BufferedReader(new InputStreamReader( client.getInputStream())); PrintWriter out = new PrintWriter(client.getOutputStream()); out.println(msg); out.flush(); return in.readLine(); } catch (IOException e) { e.printStackTrace(); } return ""; } public void closeSocket() { try { client.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { } }