Java Socket Programming Standard example (multithreaded)

Source: Internet
Author: User

Link Address: http://blog.csdn.net/benweizhu/article/details/6615542

Server-side (server) non-threading

[Java]View PlainCopy
  1. Package com.zeph.serverclient;
  2. Import Java.io.BufferedReader;
  3. Import java.io.IOException;
  4. Import Java.io.InputStreamReader;
  5. Import Java.io.PrintWriter;
  6. Import Java.net.ServerSocket;
  7. Import Java.net.Socket;
  8. Public class MyServer {
  9. public static void Main (string[] args) throws IOException {
  10. ServerSocket Server = new ServerSocket (5678);
  11. Socket client = Server.accept ();
  12. BufferedReader in = new BufferedReader (new InputStreamReader (
  13. Client.getinputstream ()));
  14. PrintWriter out = new PrintWriter (Client.getoutputstream ());
  15. While (true) {
  16. String str = in.readline ();
  17. System.out.println (str);
  18. Out.println ("has receive ....");
  19. Out.flush ();
  20. if (str.equals ("End"))
  21. Break ;
  22. }
  23. Client.close ();
  24. }
  25. }


Clients (client)

[Java]View PlainCopy
  1. Package com.zeph.serverclient;
  2. Import Java.io.BufferedReader;
  3. Import Java.io.InputStreamReader;
  4. Import Java.io.PrintWriter;
  5. Import java.net.InetAddress;
  6. Import Java.net.Socket;
  7. Public class MyClient {
  8. static Socket server;
  9. public static void Main (string[] args) throws Exception {
  10. Server = new Socket (Inetaddress.getlocalhost (), 5678);
  11. BufferedReader in = new BufferedReader (new InputStreamReader (
  12. Server.getinputstream ()));
  13. PrintWriter out = new PrintWriter (Server.getoutputstream ());
  14. BufferedReader wt = new BufferedReader (new InputStreamReader (system.in));
  15. While (true) {
  16. String str = wt.readline ();
  17. Out.println (str);
  18. Out.flush ();
  19. if (str.equals ("End")) {
  20. Break ;
  21. }
  22. System.out.println (In.readline ());
  23. }
  24. Server.close ();
  25. }
  26. }

Server-side (server) multithreading

[Java]View PlainCopy
  1. Package com.zeph.multiclient;
  2. Import Java.io.BufferedReader;
  3. Import java.io.IOException;
  4. Import Java.io.InputStreamReader;
  5. Import Java.io.PrintWriter;
  6. Import Java.net.ServerSocket;
  7. Import Java.net.Socket;
  8. Public class Multiclient extends Thread {
  9. private Socket client;
  10. Public multiclient (Socket c) {
  11. this.client = c;
  12. }
  13. public Void Run () {
  14. try {
  15. BufferedReader in = new BufferedReader (new InputStreamReader (
  16. Client.getinputstream ()));
  17. PrintWriter out = new PrintWriter (Client.getoutputstream ());
  18. //Mutil User but can ' t parallel
  19. While (true) {
  20. String str = in.readline ();
  21. System.out.println (str);
  22. Out.println ("has receive ....");
  23. Out.flush ();
  24. if (str.equals ("End"))
  25. Break ;
  26. }
  27. Client.close ();
  28. } catch (IOException ex) {
  29. } finally {
  30. }
  31. }
  32. public static void Main (string[] args) throws IOException {
  33. ServerSocket Server = new ServerSocket (5678);
  34. While (true) {
  35. //Transfer location Change single user or Multi user
  36. multiclient mc = New Multiclient (Server.accept ());
  37. Mc.start ();
  38. }
  39. }
  40. }


Test this code can be directly run on the DOS console, so the effect is clearer!

Basic server steps:

1. Specify the port instantiation of a Seversocket

2. Call the Accept () method of serversocket to cause blocking while waiting for the connection

3. Get the stream at the bottom of the socket for read and write operations

4. Encapsulating data into a stream

5. Read and write the socket

6. Close the Open stream

Basic client steps:

1. Instantiate the socket via IP address and port, request a connection to the server

2. Get the stream on the socket to read and write

3. Encapsulating the flow into the Bufferedreader/printwriter instance

4. Read and write the socket

5. Close the Open stream

Java Socket Programming Standard example (multithreaded)

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.