Simple Example of Visual C ++ 6.0 socket

Source: Internet
Author: User
  1. # Include <iostream>
  2. # Include <Winsock. h>
  3. # Pragma comment (Lib, "wsock32.lib ")
  4. Using namespace STD;
  5. Int main ()
  6. {
  7. Wsadata data;
  8. // Load the class library
  9. Wsastartup (makeword (1, 1), & data );
  10. // Initialize the socket
  11. Socket serversocket = socket (af_inet, sock_stream, 0 );
  12. // Set the bound information to bind this socket
  13. Sockaddr_in serveraddress;
  14. Serveraddress. sin_family = af_inet;
  15. Serveraddress. sin_port = htons (9999 );
  16. Serveraddress. sin_addr.s_un.s_addr = htonl (inaddr_any );
  17. // Bind this socket
  18. BIND (serversocket, (sockaddr *) & serveraddress, sizeof (serveraddress ));
  19. // Start listening
  20. Listen (serversocket, 5 );
  21. Sockaddr_in clientaddress;
  22. Int Len = sizeof (clientaddress );
  23. While (1)
  24. {
  25. Cout <"Wait..." <Endl;
  26. Socket clientsocket = accept (serversocket, (sockaddr *) & clientaddress, & Len );
  27. Char sendbuffer [20];
  28. Strcpy (sendbuffer, "Hi, client I'm server, welcome you! ");
  29. Send (clientsocket, sendbuffer, strlen (sendbuffer), 0 );
  30. Char recvbuffer [20];
  31. Recv (clientsocket, recvbuffer, strlen (recvbuffer), 0 );
  32. Cout <recvbuffer <Endl;
  33. Cout <"bye" <Endl;
  34. Closesocket (clientsocket );
  35. }
  36. Return 0;
  37. }

This is a simple server code. In VC ++ 6.0, you only need to copy the code to run it completely.

The first time I saw this writing, I always felt incredible and too troublesome. After reading the instructions last night, the basic idea is clear and you can write the code yourself. C ++ is still a little difficult to get started.

I don't know why the socket in it is like this.

1: First load the class library

2: Initialize socket

3: bind the port and IP address (to form a class related to the port and IP address, and then set the attribute)

4: Start listening ......

 

In Java, you only need to set the IP address and port through the class constructor, and then call accept to enter the listening mode.

 

But fortunately, the idea is the same, but the rest of Java helps you. The following figure shows the effect of using telnet after the server runs.

 

 

 

 

I don't know why so many hot buzzwords come out. I know it's probably a char [] problem.

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.