linux socket编程
1 #include <stdio.h>
2 #include <iostream>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <netinet/in.h>
8 #include <sys/socket.h>
9 #include <sys/wait.h>
10 using namespace std;
11 /* 服务器要监听的本地端口 */
12 #define MYPORT 8900
13 /* 能够同时接受多少没有 accept 的连接 */
14 #define BACKLOG 10
15 int main()
16 {
17 int sock_fd,new_fd;
18 struct sockaddr_in my_addr;
19 struct sockaddr_in their_addr;
20 socklen_t sin_size;
21 if((sock_fd=socket(AF_INET,SOCK_STREAM,0))==-1)
22 {
23 cout<<"error"<<endl;
24 exit(1);
25 }
26 my_addr.sin_family = AF_INET;
27 my_addr.sin_port = htons(MYPORT);
28 my_addr.sin_addr.s_addr = htons(INADDR_ANY);
29 bzero(&(my_addr.sin_zero),8);
30 if(bind(sock_fd,(sockaddr *)&my_addr,sizeof(sockaddr))==-1)
31 {
32 cout<<"bind error"<<endl;
33 exit(1);
34 }
35 if(listen(sock_fd,BACKLOG)==-1)
36 {
37 cout<<"listen error"<<endl;
38 exit(1);
39 }
40 while(true)
41 {
42 sin_size = sizeof(sockaddr_in);
43 if ((new_fd = accept(sock_fd, (sockaddr *)&their_addr, &sin_size)) == -1)
44 {
45 /* 如果调用 accept()出现错误,则给出错误提示,进入下一个循环 */
46 perror("accept error");
47 continue;
48 }
49 if(!fork())
50 {
51 if (send(new_fd, "Hello, world!\n",14, 0) == -1)
52 {
53 perror("send error");
54 close(new_fd);
55 exit(0);
56 }
57 }
58 close(new_fd);
59
60
61 }
62 while(waitpid(-1,NULL,WNOHANG) > 0);
63 return 0;
64 }
65
2 #include <iostream>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <netinet/in.h>
8 #include <sys/socket.h>
9 #include <sys/wait.h>
10 using namespace std;
11 /* 服务器要监听的本地端口 */
12 #define MYPORT 8900
13 /* 能够同时接受多少没有 accept 的连接 */
14 #define BACKLOG 10
15 int main()
16 {
17 int sock_fd,new_fd;
18 struct sockaddr_in my_addr;
19 struct sockaddr_in their_addr;
20 socklen_t sin_size;
21 if((sock_fd=socket(AF_INET,SOCK_STREAM,0))==-1)
22 {
23 cout<<"error"<<endl;
24 exit(1);
25 }
26 my_addr.sin_family = AF_INET;
27 my_addr.sin_port = htons(MYPORT);
28 my_addr.sin_addr.s_addr = htons(INADDR_ANY);
29 bzero(&(my_addr.sin_zero),8);
30 if(bind(sock_fd,(sockaddr *)&my_addr,sizeof(sockaddr))==-1)
31 {
32 cout<<"bind error"<<endl;
33 exit(1);
34 }
35 if(listen(sock_fd,BACKLOG)==-1)
36 {
37 cout<<"listen error"<<endl;
38 exit(1);
39 }
40 while(true)
41 {
42 sin_size = sizeof(sockaddr_in);
43 if ((new_fd = accept(sock_fd, (sockaddr *)&their_addr, &sin_size)) == -1)
44 {
45 /* 如果调用 accept()出现错误,则给出错误提示,进入下一个循环 */
46 perror("accept error");
47 continue;
48 }
49 if(!fork())
50 {
51 if (send(new_fd, "Hello, world!\n",14, 0) == -1)
52 {
53 perror("send error");
54 close(new_fd);
55 exit(0);
56 }
57 }
58 close(new_fd);
59
60
61 }
62 while(waitpid(-1,NULL,WNOHANG) > 0);
63 return 0;
64 }
65
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <netdb.h>
6 #include <sys/types.h>
7 #include <netinet/in.h>
8 #include <sys/socket.h>
9 #include <iostream>
10 using namespace std;
11 /* 服务器程序监听的端口号 */
12 #define MYPORT 8900
13 /* 我们一次所能够接收的最大字节数 */
14 #define MAXDATASIZE 100
15 int main() {
16 char buffer[MAXDATASIZE];
17 int sock_fd;
18 struct sockaddr_in their_addr;
19 if ((sock_fd = socket(AF_INET,SOCK_STREAM, 0)) == -1) {
20 cout << "error" << endl;
21 exit(1);
22 }
23 their_addr.sin_family = AF_INET;
24 their_addr.sin_port = htons(MYPORT);
25 their_addr.sin_addr.s_addr = htons(INADDR_ANY);
26 bzero(&(their_addr.sin_zero), 8);
27 if(connect(sock_fd,(struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)
28 {
29 perror("connect");
30 exit(1);
31 }
32 int numberSize = 0;
33 if((numberSize=recv(sock_fd,buffer,MAXDATASIZE,0))==-1)
34 {
35 perror("error");
36 exit(1);
37 }
38 buffer[numberSize] = '\0';
39 cout<<buffer<<endl;
40
41 return 0;
42 }
43
44
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <netdb.h>
6 #include <sys/types.h>
7 #include <netinet/in.h>
8 #include <sys/socket.h>
9 #include <iostream>
10 using namespace std;
11 /* 服务器程序监听的端口号 */
12 #define MYPORT 8900
13 /* 我们一次所能够接收的最大字节数 */
14 #define MAXDATASIZE 100
15 int main() {
16 char buffer[MAXDATASIZE];
17 int sock_fd;
18 struct sockaddr_in their_addr;
19 if ((sock_fd = socket(AF_INET,SOCK_STREAM, 0)) == -1) {
20 cout << "error" << endl;
21 exit(1);
22 }
23 their_addr.sin_family = AF_INET;
24 their_addr.sin_port = htons(MYPORT);
25 their_addr.sin_addr.s_addr = htons(INADDR_ANY);
26 bzero(&(their_addr.sin_zero), 8);
27 if(connect(sock_fd,(struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)
28 {
29 perror("connect");
30 exit(1);
31 }
32 int numberSize = 0;
33 if((numberSize=recv(sock_fd,buffer,MAXDATASIZE,0))==-1)
34 {
35 perror("error");
36 exit(1);
37 }
38 buffer[numberSize] = '\0';
39 cout<<buffer<<endl;
40
41 return 0;
42 }
43
44