學習Linux上C編程練習
程序沒什么東西,比較簡單,就是想把看的一些東西都用上。客戶端程序還要修改,連接超時沒設(shè)置。
服務(wù)器:
客戶端:
服務(wù)器:
??1
/**//**
??2
*?sock編程測試,多線程,server端
??3
**/
??4
#include?<stdio.h>
??5
#include?<stdlib.h>
??6
#include?<unistd.h>
??7
#include?<string.h>
??8
#include?<sys/socket.h>
??9
#include?<arpa/inet.h>
?10
#include?<netinet/in.h>
?11
#include?<pthread.h>
?12
?13
#define?MAX_LENGTH?1024?//?緩沖區(qū)最大長度
?14
#define?MAX_THREAD?2?//?定義允許的最大線程數(shù)
?15
#define?MAX_LISTEN?20?//?定義最大允許監(jiān)聽連接數(shù)
?16
?17
pthread_mutex_t?p_lock;
?18
char?buf[MAX_LENGTH];
?19
char?data[MAX_LENGTH];
?20
int?thread_count?=?0;?//當前線程數(shù)量
?21
/**//**
?22
*?線程處理函數(shù)
?23
*?向來源信息發(fā)送數(shù)據(jù)
?24
**/
?25
void?*deal(void?*arg)
?26

{
?27
????int?c_fd?=?*((int?*)arg);
?28
????char?buf_s[MAX_LENGTH];
?29
????snprintf(buf_s,MAX_LENGTH,"thread?id?is?:?%u\n",pthread_self());
?30
????pthread_mutex_lock(&p_lock);
?31
????if(write(c_fd,buf_s,strlen(buf_s))?<?0)
?32
????
{
?33
????????printf("write?sock(%u)?error,thread?id?is?:",c_fd,pthread_self());
?34
????}
?35
????sleep(5);
?36
????thread_count--;
?37
????pthread_mutex_unlock(&p_lock);
?38
}
?39
?40
int?main(int?argc,char*?argv[])
?41

{
?42
????int?port;?//?監(jiān)聽的端口
?43
????int?sock_fd;?//sock描述符
?44
????int?i,*connect_fd;
?45
????pthread_t?thread_id;
?46
????struct?sockaddr_in?server;
?47
????struct?sockaddr_in?client;
?48
????socklen_t?len;
?49
????/**//**
?50
????*?參數(shù)個數(shù)不正確
?51
????**/
?52
????if(?argc?!=?2?)
?53
????
{
?54
????????printf("\nUsage:%s?[port]\n",argv[0]);
?55
????????exit(1);
?56
????}
?57
????port?=?atoi(argv[1]);
?58
????
?59
????if((sock_fd?=?socket(AF_INET,SOCK_STREAM,0))?<?0)
?60
????
{
?61
????????printf("create?sock?error.\n");
?62
????????exit(1);
?63
????}
?64
????//?開始填充server數(shù)據(jù)0
?65
????memset(&server,0,sizeof(server));
?66
????server.sin_family?=?AF_INET;
?67
????server.sin_addr.s_addr?=?htonl(INADDR_ANY);
?68
????server.sin_port?=?htons(port);
?69
????if(bind(sock_fd,(struct?sockaddr?*)&server,sizeof(server))?<?0)
?70
????
{
?71
????????printf("bind?sock?error.\n");
?72
????????exit(1);
?73
????}
?74
????if(listen(sock_fd,MAX_LISTEN)?<?0)
?75
????
{
?76
????????printf("listen?sock?error.\n");
?77
????????exit(1);
?78
????}
?79
????printf("\n?starting?listen
\n");
?80
????while(1)
?81
????
{
?82
????????len?=?sizeof(client);
?83
????????connect_fd?=?new?int;
?84
????????if((*connect_fd?=?accept(sock_fd,(struct?sockaddr?*)&client,&len))?<?0)
?85
????????
{
?86
????????????printf("accept?sock?error.\n");
?87
????????????exit(1);
?88
????????}
?89
????????printf("\n?connect?from?%s,?port?%d.\n",inet_ntop(AF_INET,&client.sin_addr,buf,MAX_LENGTH),ntohs(client.sin_port));
?90
????????//?創(chuàng)建線程去處理這個連接
?91
????????pthread_mutex_lock(&p_lock);
?92
????????//?檢測線程數(shù)量,如果達到最大線程數(shù)量的話,3秒內(nèi)如果無法繼續(xù)創(chuàng)建新線程,則放棄當前連接
?93
????????for(i=0;i<3;i++)
?94
????????
{
?95
????????????printf("thread_count?:?%d\n",thread_count);
?96
????????????if(thread_count?<?MAX_THREAD)
?97
????????????????break;
?98
????????????else
?99
????????????
{
100
????????????????printf("第?%d?次檢測數(shù)量已滿\n",i+1);
101
????????????????sleep(1);
102
????????????}
103
????????}
104
????????if(thread_count?>=?MAX_THREAD)
105
????????
{
106
????????????printf("give?up?connect,?ID?%d\n",*connect_fd);
107
????????????pthread_mutex_unlock(&p_lock);
108
????????????continue;
109
????????}
110
????????if(pthread_create(&thread_id,NULL,deal,connect_fd)?!=?0)
111
????????
{
112
????????????printf("thread?create?error.\n");
113
????????????exit(1);
114
????????}
115
????????printf("create?thread?%u\n",thread_id);
116
????????thread_count++;
117
????????pthread_mutex_unlock(&p_lock);
118
????}
119
}
120


??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

?99



100

101

102

103

104

105



106

107

108

109

110

111



112

113

114

115

116

117

118

119

120

客戶端:
?1
/**//**
?2
*?sock編程測試,多線程,client端
?3
**/
?4
#include?<stdio.h>
?5
#include?<stdlib.h>
?6
#include?<string.h>
?7
#include?<unistd.h>
?8
#include?<errno.h>
?9
#include?<sys/socket.h>
10
#include?<arpa/inet.h>
11
#include?<netinet/in.h>
12
13
#define?MAX_LENGTH?1024
14
char?buf[MAX_LENGTH];
15
16
int?main(int?argc,char*?argv[])
17

{
18
????//?檢查參數(shù)
19
????int?sock_fd,port,n;
20
????struct?sockaddr_in?server;
21
????if(argc?!=?3)
22
????
{
23
????????printf("Usage:%s?[ip]?[port]\n",argv[0]);
24
????????exit(1);
25
????}
26
????port?=?atoi(argv[2]);
27
????if((sock_fd?=?socket(AF_INET,SOCK_STREAM,0))?<?0)
28
????
{
29
????????printf("sock?error.\n");
30
????????exit(1);
31
????}
32
????bzero(&server,sizeof(server));
33
????server.sin_family?=?AF_INET;
34
????server.sin_port?=?htons(port);
35
????if(inet_pton(AF_INET,argv[1],&server.sin_addr)?<=?0)
36
????
{
37
????????printf("set?address?error.\n");
38
????????exit(1);
39
????}
40
????if(connect(sock_fd,(struct?sockaddr?*)&server,sizeof(server))?<?0)
41
????
{
42
????????printf("connect?error:%s\n",strerror(errno));
43
????????exit(1);
44
????}
45
????if((n?=?read(sock_fd,buf,MAX_LENGTH))?>?0)
46
????
{
47
????????printf("receive?data?:?%s\n",buf);
48
????}
49
????else
50
????
{
51
????????printf("read?data?error\n");
52
????????exit(1);
53
????}
54
????close(sock_fd);
55
}
56


?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

posted on 2006-09-21 15:22 編程之道 閱讀(586) 評論(0) 編輯 收藏 引用 所屬分類: C/C++