Leetcode: 06/01

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

<br>// SelectCli.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include <winsock2.h>

#include <conio.h>

#pragma comment(lib,"ws2_32")

#define SOCKET_MAXCNT   64

#define THREAD_NUM      2       //  实验代码  与服务器配合 不可改变连接线程数

#define CONNECT_PORT    8773   

#define CONNECT_ADDR    "127.0.0.1"

DWORD WINAPI SocketFunc(LPVOID pM) 

    SOCKET  sock;

    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");

        printf(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};

    WSADATA wsa_data;

    // 初始化数组 传递给线程  区别各个线程号

    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;

}

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

// SelectSrv.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include <winsock2.h>

#include <conio.h>

#pragma comment(lib,"ws2_32")

#define SOCKET_MAXCNT   64

#define CONNECT_PORT    8773   

#define CONNECT_ADDR    "127.0.0.1"

int
_tmain(int
argc, _TCHAR* argv[])

{

    WSADATA wsa_data;

    fd_set read_all;

    SOCKET  srv_listen = INVALID_SOCKET;

    struct
sockaddr_in srv_addr;

    int
result,ready_cnt;

    int
addr_len = sizeof(srv_addr);

    SOCKET  recv_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 ");

        printf("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 ");

            printf("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 ");

                printf("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 ");

                printf("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;

}

学习了下 winsock  下的select写法

对于调用封装好的各类库  熟悉下原始写法还是很有裨益的

时间: 2024-11-08 12:16:50

Leetcode: 06/01的相关文章

JavaScript基础系列目录(2014.06.01~2014.06.08)

下列文章,转载请亲注明链接出处,谢谢! 链接地址: http://www.cnblogs.com/ttcc/tag/JavaScript%20%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%20%E6%80%BB%E7%BB%93/ 1. Javascript基础---语法(待完成) 2. JavaScript基础---数据类型(待完成) 3. JavaScript基础---Array数组(待完成) 4. JavaScript基础---正则表达式(待完成) 5. Jav

Cheatsheet: 2014 06.01 ~ 06.30

Mobile Developing iOS8 Apps Using Swift – Part 1- Hello World The Insider's Guide to Android Interviewing iOS8 – How to use Objective-C Classes in Swift Developing iOS8 Apps Using Swift – Part 2 – Connect to the iTunes Search API A Swift Tour Swift C

LeetCode 06 ZigZag Conversion

https://leetcode.com/problems/zigzag-conversion/ 水题纯考细心 题目:依照Z字形来把一个字符串写成矩阵,然后逐行输出矩阵. O(n)能够处理掉 记i为行数 第0行和第numRow-1行. ans += str[i+k*(numRows*2-2)], k=0,1,2,... 其它, 每一个Z字形(事实上仅仅是一竖一斜两条线)须要加上两个,下标见代码. 特殊处理:numRows=1的时候numRows*2-2==0 ,会死循环的.另外numRows=1

Day 10 (06/01) nginx服务 ndf服务 计划任务crond服务

nginx服务 源码安装: yum install gcc-* glibc-* openssl openssl-devel pcre pcre-devel zlib zlib-devel -ylstar xvf nginx-1.10.3.tar.gz cd nginx-1.10.3ls./configure./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-pat

[leetcode] 542. 01 Matrix (Medium)

给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改了半天---- Runtime: 132 ms, faster than 98.88% of C++ online submissions for 01 Matrix. class Solution { public: vector<vector<int>> updateMatrix(

Leetcode 542.01矩阵

01矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 0 0 0 示例 2: 输入: 0 0 0 0 1 0 1 1 1 输出: 0 0 0 0 1 0 1 2 1 注意: 给定矩阵的元素个数不超过 10000. 给定矩阵中至少有一个元素是 0. 矩阵中的元素只在四个方向上相邻: 上.下.左.右. 思路 先把所有0入队,把1置为MAX_VALUE,然

LeetCode 542. 01 Matrix

输入:只包含0,1的矩阵 输出:元素1到达最近0的距离 算法思想:广度优先搜索. 元素为0为可达区域,元素为1为不可达区域,我们的目标是为了从可达区域不断地扩展至不可达区域,在扩展的过程中,也就计算出了这些不可达区域到达最近可达区域的距离. 每个可达元素都记录了到当前位置的距离,因此在后续的遍历中,如果是经由当前节点到达的下一节点,这个距离会被累加. 1 #include <iostream> 2 #include <vector> 3 #include <queue>

LeetCode算法01 Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

Fitness - 06.01

倒计时213天 久违的瑜伽课,却发现生疏了很多,倒地不起TAT 要加强锻炼,不要松懈啊~~~! 期待黄金周的到来!!