Java TCP-based socket programming exercises

Source: Internet
Author: User
Tags readline sin

Environment: Notpad + + 6.0 + JDK 6.0.31

Issue: Use sockets to write client-server programs that enable client-server interaction calculations. The customer sends the length of the triangle 3 sides to the server, and the server returns the area of the computed triangle to the customer.

WORKAROUND: Use Java socket programming. The client sends the data, and the server receives the data and handles it accordingly.

Code:

Tcpserver.java:

[Java]View PlainCopy
  1. Import Java.io.BufferedReader;
  2. Import Java.io.InputStreamReader;
  3. Import Java.net.ServerSocket;
  4. Import Java.net.Socket;
  5. Public class TCPServer {
  6. public static final int NUM = 3;
  7. public static void Main (String args[]) {
  8. TCPServer tcpserver = new TCPServer ();
  9. try {
  10. ServerSocket server = null;
  11. try {
  12. Server = new ServerSocket (4700);
  13. } catch (Exception e) {
  14. E.printstacktrace ();
  15. }
  16. Socket socket = null;
  17. try {
  18. Socket = Server.accept ();
  19. } catch (Exception e) {
  20. E.printstacktrace ();
  21. }
  22. String line = "";
  23. double a[] = new double[num];
  24. BufferedReader is = new BufferedReader (new InputStreamReader
  25. (Socket.getinputstream ()));
  26. System.out.println ("Three sides of the triangle received from the client:");
  27. For (int i = 0; i < a.length; i++) {
  28. line = Is.readline ();
  29. A[i] = double.parsedouble (line);
  30. }
  31. System.out.println ("The area of the triangle is:" + Tcpserver.countarea (a));
  32. Is.close (); //Turn off read stream
  33. Socket.close (); //Close socket
  34. Server.close (); //Close ServerSocket
  35. } catch (Exception e) {
  36. E.printstacktrace ();
  37. }
  38. }
  39. //method for calculating triangular area
  40. public Double Countarea (double a[]) {
  41. Double area = 0.0;
  42. Double temp = 0.0;
  43. double aside = 0.0;
  44. double bside = 0.0;
  45. double cside = 0.0;
  46. aside = a[0];
  47. Bside = a[1];
  48. Cside = a[2];
  49. temp = a[0] + a[1] + a[2];
  50. temp = temp/ 2;
  51. Area = MATH.SQRT (temp * (temp-aside) * (temp-bside) * (temp-cside));
  52. return area;
  53. }
  54. }


Tcpclient.java

[Java]View PlainCopy
  1. Import Java.io.BufferedReader;
  2. Import Java.io.InputStreamReader;
  3. Import Java.io.PrintWriter;
  4. Import Java.net.Socket;
  5. Public class TcpClient {
  6. public static final int NUM = 3;
  7. public static void Main (String args[]) {
  8. try {
  9. Socket socket = new socket ("127.0.0.1", 4700);
  10. BufferedReader sin = new BufferedReader (new InputStreamReader (system.in));
  11. PrintWriter OS = new PrintWriter (Socket.getoutputstream ());
  12. System.out.println ("Three sides from the client receive triangle sent to the server:");
  13. System.out.println ("Please enter 3 sides:");
  14. String ReadLine = "";
  15. For (int i = 0; i < NUM; i++) {
  16. Os.flush ();
  17. ReadLine = Sin.readline ();
  18. Os.println (ReadLine);
  19. }
  20. Os.close ();
  21. Socket.close ();
  22. } catch (Exception e) {
  23. E.printstacktrace ();
  24. }
  25. }
  26. }


Run:

The client program (Tcpclient.java) and the server-side program (TCPSERVER.JAVA) are compiled separately, and then open two CMD consoles, first run the server, then run the client, enter the data on the client, and then you can see the results on the server.

Operating effect:

Service side:

Client:

Download resources: www.makeru.com.cn/?t=12

Java TCP-based socket programming exercises

Related Article

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.