Windows Socket instance

Source: Internet
Author: User

Socket programming in Windows mainly includes the following parts:
Server
1. initialize the Windows Socket library.
2. Create a socket.
3. Bind a socket.
4. Listen.
5. Accept.
6. receive and send data.
Client
1. initialize the Windows Socket library.
2. Create a socket.
3. Connect to the socket.
4. receive and send data.
Each time the server receives a socket from a client, a thread is created. Allows a server to connect multiple clients.

1 // server. cpp
2 # include <iostream>
3 # include <windows. h>
4
5 using namespace STD;
6
7 # Define port 4000
8 # define ip_address "192.168.1.145"
9
10 DWORD winapi clientthread (lpvoid lpparameter)
11 {
12 socket cientsocket = (socket) lpparameter;
13 int ret = 0;
14 char recvbuffer [max_path];
15
16 while (true)
17 {
18 memset (recvbuffer, 0x00, sizeof (recvbuffer ));
19 ret = Recv (cientsocket, recvbuffer, max_path, 0 );
20 if (ret = 0 | ret = socket_error)
21 {
22 cout <"client exited! "<Endl;
23 break;
24}
25 cout <"the customer information received is:" <recvbuffer <Endl;
26}
27
28 return 0;
29}
30
31 int main (INT argc, char * argv [])
32 {
33 wsadata ws;
34 socket serversocket, cientsocket;
35 struct sockaddr_in localaddr, clientaddr;
36 int ret = 0;
37 int addrlen = 0;
38 handle hthread = NULL;
39
40 // init Windows Socket
41 if (wsastartup (makeword (2, 2), & ws )! = 0)
42 {
43 cout <"init Windows Socket failed:" <getlasterror () <Endl;
44 return-1;
45}
46
47 // create socket
48 serversocket = socket (af_inet, sock_stream, ipproto_tcp );
49 If (serversocket = invalid_socket)
50 {
51 cout <"create socket failed:" <getlasterror () <Endl;
52 return-1;
53}
54
55 localaddr. sin_family = af_inet;
56 localaddr. sin_addr.s_addr = inet_addr (ip_address );
57 localaddr. sin_port = htons (port );
58 memset (localaddr. sin_zero, 0x00, 8 );
59
60 // bind socket
61 ret = BIND (serversocket, (struct sockaddr *) & localaddr, sizeof (localaddr ));
62 if (Ret! = 0)
63 {
64 cout <"bind socket failed:" <getlasterror () <Endl;
65 return-1;
66}
67
68 ret = listen (serversocket, 10 );
69 If (Ret! = 0)
70 {
71 cout <"Listen socket failed:" <getlasterror () <Endl;
72 return-1;
73}
74
75 cout <"server started" <Endl;
76
77 while (true)
78 {
79 addrlen = sizeof (clientaddr );
80 cientsocket = accept (serversocket, (struct sockaddr *) & clientaddr, & addrlen );
81 If (cientsocket = invalid_socket)
82 {
83 cout <"Accept failed:" <getlasterror () <Endl;
84 break;
85}
86
87 cout <"client connection:" <inet_ntoa (clientaddr. sin_addr) <":" <clientaddr. sin_port <Endl;
88
89 hthread = createthread (null, 0, clientthread, (lpvoid) cientsocket, 0, null );
90 if (hthread = NULL)
91 {
92 cout <"create thread failed! "<Endl;
93 break;
94}
95
96 closehandle (hthread );
97}
98
99 closesocket (serversocket );
100 closesocket (cientsocket );
101 wsacleanup ();
102
103 return 0;
104}

1 // client. cpp
2 # include <iostream>
3 # include <windows. h>
4
5 using namespace STD;
6
7 # Define port 4000
8 # define ip_address "192.168.1.145"
9
10
11 int main (INT argc, char * argv [])
12 {
13 wsadata ws;
14 socket cientsocket;
15 struct sockaddr_in serveraddr;
16 int ret = 0;
17 int addrlen = 0;
18 handle hthread = NULL;
19 char sendbuffer [max_path];
20
21 // init Windows Socket
22 if (wsastartup (makeword (2, 2), & ws )! = 0)
23 {
24 cout <"init Windows Socket failed:" <getlasterror () <Endl;
25 return-1;
26}
27
28 // create socket
29 cientsocket = socket (af_inet, sock_stream, ipproto_tcp );
30 if (cientsocket = invalid_socket)
31 {
32 cout <"create socket failed:" <getlasterror () <Endl;
33 return-1;
34}
35
36 serveraddr. sin_family = af_inet;
37 serveraddr. sin_addr.s_addr = inet_addr (ip_address );
38 serveraddr. sin_port = htons (port );
39 memset (serveraddr. sin_zero, 0x00, 8 );
40
41 ret = connect (cientsocket, (struct sockaddr *) & serveraddr, sizeof (serveraddr ));
42 if (ret = socket_error)
43 {
44 cout <"Connect error:" <getlasterror () <Endl;
45 return-1;
46}
47 else
48 {
49 cout <"connection successful! "<Endl;
50}
51
52 while (true)
53 {
54 cin. Getline (sendbuffer, sizeof (sendbuffer ));
55 ret = Send (cientsocket, sendbuffer, (INT) strlen (sendbuffer), 0 );
56 If (ret = socket_error)
57 {
58 cout <"send info error:" <getlasterror () <Endl;
59 break;
60}
61}
62
63 closesocket (cientsocket );
64 wsacleanup ();
65
66 return 0;
67}

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.