Socket usage Related

Source: Internet
Author: User
Learned in a small exercise program




  1. Package com.sdut.edu.tools;
  2. Import Java.io.BufferedWriter;
  3. Import java.io.IOException;
  4. Import Java.io.InputStream;
  5. Import Java.io.OutputStreamWriter;
  6. Import Java.io.PrintWriter;
  7. Import java.net.InetSocketAddress;
  8. Import Java.net.Socket;
  9. Import java.net.SocketAddress;
  10. Import java.net.SocketException;
  11. Import Java.util.Scanner;
  12. Import Android.app.ActivityManager;
  13. Import Android.app.AlertDialog;
  14. Import Android.content.Context;
  15. Import Android.content.DialogInterface;
  16. Import Android.util.Log;
  17. public class ConnectServer {
  18. public string ConnectServer (String str) throws Socketexception,ioexception
  19. {//Get the response string from the server
  20. TODO auto-generated Method Stub
  21. String result = null;
  22. StringBuffer tempstr=new StringBuffer ();//result of final return
  23. try{
  24. int c=0;
  25. Create a socket object based on the server's IP and port number to establish a connection to the server
  26. Socket socket=new socket ();
  27. How to construct the socket,
  28. Socket.connect (New Inetsocketaddress ("192.168.0.189", 4444), 4000);
  29. Receives the data amount object obtained from the server
  30. if (socket.getremotesocketaddress () ==null)
  31. // {
  32. //
  33. // }
  34. Socket.connect (REMOTEADDR);
  35. BufferedReader in=new BufferedReader (New InputStreamReader (Socket.getinputstream (), "Utf-8"), 10000);
  36. InputStream Is=socket.getinputstream (); transcoding problems with receiving data
  37. Scanner in=new Scanner (IS);
  38. Creates a PrintWriter object from the established socket and sends the information to the server through this object.
  39. PrintWriter out=new PrintWriter (New BufferedWriter (New OutputStreamWriter (Socket.getoutputstream (), "Utf-8"), true) ;
  40. Send Message
  41. Out.println (str);
  42. LOG.D ("listadapter_getvediourl_isconnected", socket.isconnected () + "");
  43. To determine the connection timeout
  44. if (socket.isconnected ())//To determine the client-server connection, receive the response string
  45. {
  46. while (In.hasnextline ())
  47. {
  48. Result=in.nextline ();
  49. Tempstr.append (result);
  50. System.out.println (TEMPSTR);
  51. }
  52. }
  53. Else
  54. {
  55. SYSTEM.OUT.PRINTLN ("Connection service timeout");
  56. }
  57. Out.close ();
  58. In.close ();
  59. Socket.close ();//After completion. Close socket
  60. }catch (SocketException e)
  61. // {
  62. Return "-1";
  63. System.out.println ("Connection failed");
  64. E.printstacktrace ();
  65. }catch (IOException e) {
  66. Return "-2";
  67. // }
  68. return tempstr.tostring ();
  69. }
  70. }
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.