Winsock Select Learning Code (1)

Source: Internet
Author: User
Tags connect socket htons


SelectCli.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <winsock2.h> #include <conio.h> #pragma comment (lib, "ws2_32") #define SOCKET _maxcnt64#define thread_num2//Experiment code with server cannot change the number of connection threads # define Connect_port8773#define connect_addr "127.0.0.1" DWORD WINAPI Socketfunc (lpvoid pm) {Socketsock;char buf[100] = {0};int result;struct sockaddr_in cli_addr;int num = * (int*) PM ; sock = socket (af_inet,sock_stream,0); cli_addr.sin_family = Af_inet;cli_addr.sin_port = Htons (connect_port); cli_ ADDR.SIN_ADDR.S_ADDR = inet_addr (connect_addr); result = CONNECT (sock, (struct sockaddr*) &cli_addr,sizeof (cli_ addr)); if (result = = Socket_error) {printf ("%d thread:connect error\n", num); exit (1);} while (1) {sprintf (buf, "%d thread:%s", num, "Hello world!\n");p rintf (buf); result = Send (sock,buf,100,0); if (result = = Socket_error) {printf ("2,%s\n", "Send ERROR");} Sleep (3000);} Exit (1); return 0;} int _tmain (int argc, _tchar* argv[]) {int num[socket_maxcnt] = {0}; wsadatawsa_data;//initializes the array to the thread that distinguishes each thread number for (int i = 0; i < socket_maxcnt;i++) {Num[i] = i;} WSAStartup (Winsock_version,&wsa_data); for (int i = 0; i < thread_num;i++) {HANDLE hThread1 = CreateThread (null,0, Socketfunc,&num[i],0,null);} while (1) {Sleep (100000);} return 0;}
SelectSrv.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <winsock2.h> #include <conio.h> #pragma comment (lib, "ws2_32") #define SOCKET _maxcnt64#define connect_port8773#define connect_addr "127.0.0.1" int _tmain (int argc, _tchar* argv[]) {wsadata wsa_data ; Fd_set Read_all; Socketsrv_listen = invalid_socket;struct sockaddr_in srv_addr;int result,ready_cnt;int addr_len = sizeof (SRV_ADDR); Socketrecv_sock[2] = {Invalid_socket,invalid_socket}; WSAStartup (Makeword (2,2), &wsa_data); Srv_listen = socket (af_inet,sock_stream,ipproto_tcp); srv_addr.sin_family = Af_inet;srv_addr.sin_port = Htons (connect_port); srv_addr.sin_addr.s_addr = Inaddr_any;result = Bind (Srv_listen, ( struct sockaddr*) &srv_addr,addr_len), if (result = = Socket_error) {perror ("err");p rintf ("line%d \ n", __line__); return-1;} Listen (Srv_listen,somaxconn); Fd_zero (&read_all); Fd_set (Srv_listen,&read_all); Fd_set read_set;while (1) {read_set = read_all;ready_cnt = Select (0,&read_set, Null,null,null); if (ready_cnt = = SoCket_error) {perror ("err");p rintf ("line%d \ n", __line__); return-1;} if (Fd_isset (srv_listen,&read_set)) {struct sockaddr_in faddr;int addr_len = sizeof (FADDR); static int sock_num = 0;if (Sock_num > 1) {perror ("Connect socket is too much\n");p rintf ("line%d \ n", __line__); return-1;} Recv_sock[sock_num] = Accept (Srv_listen, (struct sockaddr*) &faddr,&addr_len); if (recv_sock[sock_num] = = Invalid_socket) {perror ("err");p rintf ("line%d \ n", __line__); return-1;} Fd_set (recv_sock[sock_num],&read_all); sock_num++;continue;} for (int i = 0; i < 2;i++) {char buf[100] = {0};if (! Fd_isset (Recv_sock[i],&read_set)) {continue;} result = recv (Recv_sock[i],buf,100,null), if (Result! = Socket_error) {printf ("recv thread.%s\n", buf);}} Getch (); return 0;}

Learn the Select notation under Winsock

It would be helpful to be familiar with the original writing of the various libraries that have been encapsulated.

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.