The procedure of using standard socket function to program under VC __php

Source: Internet
Author: User

The procedure of using standard socket function in VC
Socket is a very popular in Linux or UNIX network programming way, this way is simple and effective, can be very flexible to complete a variety of complex protocol control, personal feeling than using MFC network related class library to come more comfortable, because all is oneself controllable.
But the use of socket programming has a very inconvenient place is that, because there is no such as a VC in Linux as a powerful debugging platform, network communication in the process of error tracking will appear slightly more complex. Helpless, can only try under VC can use standard socket function to complete network communication, with the help of VC powerful debugging tools to achieve efficient coding purposes.
Looking for some information on the internet, it seems that the use of VC standard socket function is not very difficult, so the process records, so that after use ~ ~
The first step, the use of VC winsock.h header files, instead of Linux under the socket-related header files.
The second step, for the VC Project link Winsock library file ws2_32.lib
The two steps above can be achieved by using the following code:
1: #ifdef _WIN32
2: #include <winsock.h>
3: #pragma comment (lib, "Ws2_32.lib")
4: #else
5: #include <sys/socket.h>
6: #include <netinet/in.h>
7: #include <arpa/in.h>
8: #endif
Third, before you use any of the Sochet functions, first initialize the Winsock with the WSAStartup function and use the WSACleanup function to clean up Winsock before exiting the application. You can use the following code to implement:
1:word wversionrequested;
2:wsadata Wsadata;
3:int err;
4:wversionrequested = Makeword (1, 1);
5://init Winsock before using socket functions
6:err = WSAStartup (wversionrequested, &wsadata);
7:if (Err!= 0) {
8:return 0;
9:}
Ten://Check Winsock version
11:if (Lobyte (wsadata.wversion)!= 1 | |
12:hibyte (wsadata.wversion)!= 1) {
13:wsacleanup ();
14:return 0;
15:}
16:
://Use the socket function here
18:
://Clean up Winsock before exit
20:wsacleanup ();
OK, after the above three-step processing, you can use the standard socket function in the program to complete network communication.
Finally, a simple example is given:
1: #include <stdio.h>
2: #include <stdlib.h>
3:
4: #ifdef _WIN32
5: #include <winsock.h>
6: #pragma comment (lib, "Ws2_32.lib")
7: #else
8: #include <sys/socket.h>
9: #include <netinet/in.h>
#include <arpa/in.h>
One: #endif
12:
13:int Main (int argc, char *argv[])
14: {
15:int err;
16:socket sockclient;
17:sockaddr_in addrsrv;
18:char RECVBUF[50];
19:
#ifdef _WIN32
21:word wversionrequested;
22:wsadata Wsadata;
23:wversionrequested = Makeword (1, 1);
24:
25:err = WSAStartup (wversionrequested, &wsadata);
26:if (Err!= 0) {
27:return 0;
28:}
29:
30:if (Lobyte (wsadata.wversion)!= 1 | |
31:hibyte (wsadata.wversion)!= 1) {
32:wsacleanup ();
33:return 0;
34:}
#endif
36:
37:sockclient=socket (af_inet,sock_stream,0);
38:
39:addrsrv.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");
40:addrsrv.sin_family=af_inet;
41:addrsrv.sin_port=htons (6000);
42:connect (Sockclient, (sockaddr*) &addrsrv,sizeof (sockaddr));
43:send (sockclient, "Hello", strlen ("Hello") +1,0);
44:RECV (sockclient,recvbuf,50,0);
45:printf ("%s/n", recvbuf);
46:
_win32: #ifdef
48:closesocket (sockclient);
49:wsacleanup ();
: #endif
51:return 0;
52:}
Http://hi.baidu.com/hiroada/blog/item/9ece37d6083a32d5a044df7e.html

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.