Socket programming server and client (multiple clients can connect to the same port of a server at the same time)

Source: Internet
Author: User
Tags getstream

Server code

[C-sharp]
View plaincopy
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. Using system. net;
  5. Using system. net. Sockets;
  6. Using system. Threading;
  7. Namespace TCP Communication
  8. {
  9. Class Program
  10. {
  11. Static void main (string [] ARGs)
  12. {
  13. Try
  14. {
  15. // Convert an IP address to an instance
  16. IPaddress IPA = IPaddress. parse ("127.0.0.1 ");
  17. // Listening port 8001
  18. Tcplistener mylsn = new tcplistener (IPA, 8001 );
  19. // Enable the listener
  20. Mylsn. Start ();
  21. // Output successful listening Information
  22. Console. writeline ("starting service in 8001, waiting for connection ");
  23. // Wait for processing the Access Connection Request
  24. While (true)
  25. {
  26. Socket mysock = mylsn. acceptsocket ();
  27. Console. writeline ("connected, connected from" + mysock. remoteendpoint );
  28. Work W = new work ();
  29. W. mysock = mysock;
  30. W. mylsn = mylsn;
  31. Thread t = new thread (New threadstart (W. Main ));
  32. T. Start ();
  33. }
  34. }
  35. Catch
  36. {}
  37. Finally
  38. {}
  39. }
  40. }
  41. Public class work
  42. {
  43. Public socket mysock;
  44. Public tcplistener mylsn;
  45. Public void main ()
  46. {
  47. // Receives client messages
  48. Byte [] DATA = new byte [100];
  49. Mysock. Receive (data );
  50. String RT = system. Text. utf8encoding. utf8.getstring (data );
  51. Console. writeline (RT );
  52. // Send a message to the client
  53. Mysock. Send (utf8encoding. utf8.getbytes ("send back to client "));
  54. // Release resources
  55. Mysock. Close ();
  56. Mylsn. Stop ();
  57. }
  58. }
  59. }

 

Client code

[C-sharp]
View plaincopy
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. Using system. IO;
  5. Using system. net;
  6. Using system. net. Sockets;
  7. Namespace TCP communication Client
  8. {
  9. Public class class1
  10. {
  11. Public static void main ()
  12. {
  13. // Create a client socket
  14. Tcpclient tclient = new tcpclient ();
  15. // Connect to the server
  16. Tclient. Connect ("127.0.0.1", 8001 );
  17. Console. writeline ("Enter the message to be sent ");
  18. // Read the characters to be transmitted
  19. String STR = console. Readline ();
  20. // Get the stream
  21. Stream STM = tclient. getstream ();
  22. // Send a string
  23. Asciiencoding ASEN = new asciiencoding ();
  24. Byte [] DATA = Asen. getbytes (STR );
  25. STM. Write (data, 0, Data. Length );
  26. // Accept the message returned by the server
  27. Byte [] Back = new byte [100];
  28. Int K = STM. Read (back, 0,100 );
  29. // Output the message returned by the server
  30. For (INT I = 0; I <K; I ++)
  31. {
  32. Console. Write (convert. tochar (back [I]);
  33. }
  34. // Close the connection
  35. Tclient. Close ();
  36. }
  37. }
  38. }

 

According to the original design intention of the TCP/IP designer, when mysock. Close () is used to view the local connection with the command netstat-a-n, the corresponding link status is time_wait. The system automatically closes the connection.

Server code

[C-sharp]
View plaincopy
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. Using system. net;
  5. Using system. net. Sockets;
  6. Using system. Threading;
  7. Namespace TCP Communication
  8. {
  9. Class Program
  10. {
  11. Static void main (string [] ARGs)
  12. {
  13. Try
  14. {
  15. // Convert an IP address to an instance
  16. IPaddress IPA = IPaddress. parse ("127.0.0.1 ");
  17. // Listening port 8001
  18. Tcplistener mylsn = new tcplistener (IPA, 8001 );
  19. // Enable the listener
  20. Mylsn. Start ();
  21. // Output successful listening Information
  22. Console. writeline ("starting service in 8001, waiting for connection ");
  23. // Wait for processing the Access Connection Request
  24. While (true)
  25. {
  26. Socket mysock = mylsn. acceptsocket ();
  27. Console. writeline ("connected, connected from" + mysock. remoteendpoint );
  28. Work W = new work ();
  29. W. mysock = mysock;
  30. W. mylsn = mylsn;
  31. Thread t = new thread (New threadstart (W. Main ));
  32. T. Start ();
  33. }
  34. }
  35. Catch
  36. {}
  37. Finally
  38. {}
  39. }
  40. }
  41. Public class work
  42. {
  43. Public socket mysock;
  44. Public tcplistener mylsn;
  45. Public void main ()
  46. {
  47. // Receives client messages
  48. Byte [] DATA = new byte [100];
  49. Mysock. Receive (data );
  50. String RT = system. Text. utf8encoding. utf8.getstring (data );
  51. Console. writeline (RT );
  52. // Send a message to the client
  53. Mysock. Send (utf8encoding. utf8.getbytes ("send back to client "));
  54. // Release resources
  55. Mysock. Close ();
  56. Mylsn. Stop ();
  57. }
  58. }
  59. }

 

Client code

[C-sharp]
View plaincopy
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. Using system. IO;
  5. Using system. net;
  6. Using system. net. Sockets;
  7. Namespace TCP communication Client
  8. {
  9. Public class class1
  10. {
  11. Public static void main ()
  12. {
  13. // Create a client socket
  14. Tcpclient tclient = new tcpclient ();
  15. // Connect to the server
  16. Tclient. Connect ("127.0.0.1", 8001 );
  17. Console. writeline ("Enter the message to be sent ");
  18. // Read the characters to be transmitted
  19. String STR = console. Readline ();
  20. // Get the stream
  21. Stream STM = tclient. getstream ();
  22. // Send a string
  23. Asciiencoding ASEN = new asciiencoding ();
  24. Byte [] DATA = Asen. getbytes (STR );
  25. STM. Write (data, 0, Data. Length );
  26. // Accept the message returned by the server
  27. Byte [] Back = new byte [100];
  28. Int K = STM. Read (back, 0,100 );
  29. // Output the message returned by the server
  30. For (INT I = 0; I <K; I ++)
  31. {
  32. Console. Write (convert. tochar (back [I]);
  33. }
  34. // Close the connection
  35. Tclient. Close ();
  36. }
  37. }
  38. }

 

According to the original design intention of the TCP/IP designer, when mysock. Close () is used to view the local connection with the command netstat-a-n, the corresponding link status is time_wait. The system automatically closes the connection.

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.