C ++ socket programming example

Source: Internet
Author: User
Tags htons
Server:
  1. # Include "stdafx. H"
  2. # Include <winsock2.h>
  3. # Pragma comment (Lib, "ws2_32.lib ")
  4. # Define port_number 11234
  5. # Define max_buf_size 1024
  6. Int _ tmain (INT argc, _ tchar * argv [])
  7. {
  8. Wsadata;
  9. Word dwsockversion = makeword (2, 2 );
  10. If (0! = Wsastartup (dwsockversion, & wsadata) // negotiate the version number
  11. {
  12. Printf ("arrange version failure ");
  13. Return-1;
  14. }
  15. Socket nsocket;
  16. Nsocket = socket (af_inet, sock_stream, ipproto_tcp); // create a TCP socket
  17. If (invalid_socket = nsocket)
  18. {
  19. Printf ("invalid socket ");
  20. Wsacleanup ();
  21. Return-1;
  22. }
  23. Sockaddr_in SA;
  24. SA. sin_family = af_inet;
  25. SA. sin_addr.s_addr = inet_addr ("127.0.0.1 ");
  26. SA. sin_port = htons (port_number); // select a port number.
  27. If (BIND (nsocket, (sockaddr *) & SA, sizeof (SA) <0) // bind
  28. {
  29. Closesocket (nsocket );
  30. Return-1;
  31. }
  32. Listen (nsocket, 2 );
  33. Sockaddr_in addr_client;
  34. Int naddrlen = sizeof (addr_client );
  35. While (true)
  36. {
  37. // Socket nsock = accept (nsocket, (sockaddr *) & addr_client, & naddrlen );
  38. Socket nsock = accept (nsocket, null, null );
  39. If (invalid_socket = nsock)
  40. {
  41. Printf ("failure to accept ");
  42. Continue;
  43. }
  44. Char TMP [max_buf_size] = {0 };
  45. Char Buf [max_buf_size] = {0 };
  46. Strcpy (TMP, "this is server ");
  47. Send (nsock, TMP, (INT) strlen (TMP), 0 );
  48. Int nrecv = Recv (nsock, Buf, max_buf_size, 0 );
  49. If (nrecv> 0)
  50. {
  51. Printf ("% s/n", Buf );
  52. }
  53. Closesocket (nsock );
  54. Return 0;
  55. }
  56. }

 

Client:

  1. # Include "stdafx. H"
  2. # Include <winsock2.h>
  3. # Pragma comment (Lib, "ws2_32.lib ")
  4. # Define max_buf_size 1024
  5. # Define port_number 11234
  6. Int _ tmain (INT argc, _ tchar * argv [])
  7. {
  8. Wsadata;
  9. Word dwsockversion = makeword (2, 2 );
  10. If (0! = Wsastartup (dwsockversion, & wsadata) // negotiate the version number
  11. {
  12. Printf ("arrange version failure ");
  13. Return-1;
  14. }
  15. Socket nsocket;
  16. Nsocket = socket (af_inet, sock_stream, ipproto_tcp); // create a TCP socket
  17. If (invalid_socket = nsocket)
  18. {
  19. Printf ("invalid socket ");
  20. Wsacleanup ();
  21. Return-1;
  22. }
  23. Sockaddr_in SA;
  24. SA. sin_family = af_inet;
  25. SA. sin_addr.s_addr = inet_addr ("127.0.0.1 ");
  26. SA. sin_port = htons (port_number );
  27. If (0! = Connect (nsocket, (const sockaddr *) & SA, sizeof (SA )))
  28. Return-1;
  29. Char Buf [max_buf_size] = {0 };
  30. Char TMP [max_buf_size] = {0 };
  31. Strcpy (TMP, "this is client! ");
  32. Int nrecv = 0;
  33. Nrecv = Recv (nsocket, Buf, max_buf_size, 0 );
  34. If (nrecv> 0)
  35. {
  36. Printf ("% s/n", Buf );
  37. Send (nsocket, TMP, (INT) strlen (TMP), 0 );
  38. }
  39. Closesocket (nsocket );
  40. Wsacleanup ();
  41. Return 0;
  42. }

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.