1 #include
2 #include
3 #include
4 #include
5 #include
6 // int socket(int domain, int type, int protocol);
7 // int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
8 // int accept(int sockfd, struct sockaddr *restrict addr,socklen_t *restrict addrlen);
9 //int inet_aton(const char *cp, struct in_addr *inp);
10 // void *memset(void *s, int c, size_t n);
11
12 int main()
13 {
14 struct sockaddr_in s_addr;
15 struct sockaddr_in c_addr;
16 memset(&s_addr,0, sizeof(struct sockaddr_in));
17 memset(&c_addr,0, sizeof(struct sockaddr_in));
18 // struct in_addr inp;
19 //1.socket
20 int s_fd = socket(AF_INET,SOCK_STREAM,0);
21 //2.bind
22 s_addr.sin_family = AF_INET;
23 s_addr.sin_port =htons(7980);
24 // inet_aton("192.168.5.128",&inp);
25 inet_aton("192.168.5.128",&s_addr.sin_addr);
26 bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
27 //3.listen
28 listen(s_fd,3);
29 //4.accept
30 int c_len = sizeof(struct sockaddr_in);
31 int c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&c_len);
32 if(c_fd==-1){
33 perror("c_fd why");
34 }
35 //printf to client ip address
36 printf("client ip addr is %s\n",inet_ntoa(c_addr.sin_addr));
37
38 return 0;
39 }
socket完整服务端实现:能够实现客户端接入,并且能够读取客户端信息,给客户端返回消息。
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 // int socket(int domain, int type, int protocol);
8 // int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
9 // int accept(int sockfd, struct sockaddr *restrict addr,socklen_t *restrict addrlen);
10 //int inet_aton(const char *cp, struct in_addr *inp);
11 // void *memset(void *s, int c, size_t n);
12
13 int main()
14 {
15 struct sockaddr_in s_addr;
16 struct sockaddr_in c_addr;
17 memset(&s_addr,0, sizeof(struct sockaddr_in));
18 memset(&c_addr,0, sizeof(struct sockaddr_in));
19 // struct in_addr inp;
20 //1.socket
21 int s_fd = socket(AF_INET,SOCK_STREAM,0);
22 //2.bind
23 s_addr.sin_family = AF_INET;
24 s_addr.sin_port =htons(7971);
25 // inet_aton("192.168.5.128",&inp);
26 inet_aton("192.168.5.128",&s_addr.sin_addr);
27 bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
28 //3.listen
29 listen(s_fd,3);
30 //4.accept
31 int c_len = sizeof(struct sockaddr_in);
32 int c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&c_len);
33 if(c_fd==-1){
34 perror("c_fd why");
35 }
36 char readBuf[128] = {0};
37 int n_read;
38
39 n_read = read(c_fd,readBuf,sizeof(readBuf));
40 if(n_read == -1){
41 perror("n_read why\n");
42 }
43 printf("strlen = %d\n",strlen(readBuf));
44 printf("sizeof = %d\n",sizeof(readBuf));
45 //printf to client ip address
46 printf("client ip addr is %s\n",inet_ntoa(c_addr.sin_addr));
47 printf("n_read = %d ,readBuf = %s\n",n_read,readBuf);
48
49 char *writeBuf = "I got you connect!";
50 write(c_fd,writeBuf,strlen(writeBuf));
51
52 return 0;
53 }
~
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 // int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
8
9 int main()
10 {
11 struct sockaddr_in c_addr;
12 memset(&c_addr,0, sizeof(struct sockaddr_in));
13
14 int c_fd = socket(AF_INET,SOCK_STREAM,0);
15 c_addr.sin_family = AF_INET;
16 c_addr.sin_port =htons(7987);
17 inet_aton("192.168.5.128",&c_addr.sin_addr);
18 // const struct sockaddr *
19 connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr_in));
20
21 char *writeBuf = "hello,I'm client";
22 write(c_fd,writeBuf,strlen(writeBuf));
23
24 char readBuf[128] = {0};
25 read(c_fd,readBuf,sizeof(readBuf));
26 printf("readBuf context from sever is %s\n",readBuf);
27 return 0;
28 }
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 // int socket(int domain, int type, int protocol);
9 // int bind(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
10 // int accept(int sockfd, struct sockaddr *restrict addr,socklen_t *restrict addrlen);
11 //int inet_aton(const char *cp, struct in_addr *inp);
12 // void *memset(void *s, int c, size_t n);
13
14 int main(int argc,char*argv[])
15 {
16 if( argc != 3){
17 perror("param why\n");
18 }
19
20 char readBuf[128] = {0};
21 int n_read;
22 // char *writeBuf = "I got you connect,I'm from server";
23 char writeBuf[128] = {0};
24
25 struct sockaddr_in s_addr;
26 struct sockaddr_in c_addr;
27 memset(&s_addr,0, sizeof(struct sockaddr_in));
28 memset(&c_addr,0, sizeof(struct sockaddr_in));
29 // struct in_addr inp;
30 //1.socket
31 int s_fd = socket(AF_INET,SOCK_STREAM,0);
32 //2.bind
33 s_addr.sin_family = AF_INET;
34 s_addr.sin_port =htons(atoi(argv[1]));
35 // inet_aton("192.168.5.128",&inp);
36 inet_aton(argv[2],&s_addr.sin_addr);
37 bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
38 //3.listen
39 listen(s_fd,3);
40 //4.accept
41 int c_len = sizeof(struct sockaddr_in);
42 int c_fd;
43
44 while(1){
45 c_fd = accept(s_fd,(struct sockaddr *)&c_addr,&c_len);
46 if(c_fd==-1){
47 perror("c_fd why");
48 }
49 printf("client ip addr is %s\n",inet_ntoa(c_addr.sin_addr));
50 if(fork()==0){
51 //if(fork()==0){
52 if(fork()==0){
53 while(1){
54 memset(writeBuf,0,sizeof(writeBuf));
55 printf("input: ");
56 scanf("%s",writeBuf);
57 write(c_fd,writeBuf,strlen(writeBuf));
58 }
59 }
60
61 while(1){
62 memset(readBuf,0,sizeof(readBuf));
63 n_read = read(c_fd,readBuf,128);
64 if(n_read == -1){
65 perror("n_read why\n");
66 }
67 //printf to client ip address
68 printf("n_read = %d ,readBuf = %s\n",n_read,readBuf);
69 }
70 // }
71 /* if(fork()==0){
72 while(1){
73 memset(writeBuf,0,sizeof(writeBuf));
74 printf("input: ");
75 scanf("%s",writeBuf);
76 write(c_fd,writeBuf,strlen(writeBuf));
77 }
78 }
79 */ break;
80 }
81 }
82
83 return 0;
84 }
~
~
~
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8
9 // int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);
10
11 int main(int argc,char *argv[])
12 {
13 if(argc != 3){
14 perror("param why\n");
15 }
16 // char *writeBuf = "hello,I'm client";
17 char writeBuf[128]= {0};
18 char readBuf[128] = {0};
19 int c_fd;
20 struct sockaddr_in c_addr;
21 memset(&c_addr,0, sizeof(struct sockaddr_in));
22
23 c_fd = socket(AF_INET,SOCK_STREAM,0);
24 c_addr.sin_family = AF_INET;
25 c_addr.sin_port =htons(atoi(argv[1]));
26 inet_aton(argv[2],&c_addr.sin_addr);
27 // const struct sockaddr *
28 connect(c_fd,(struct sockaddr *)&c_addr,sizeof(struct sockaddr_in));
29 // printf("father PID=%d\n",getpid());
30 while(1){
31 if(fork()==0){
32 // printf("child PID = %d\n",getpid());
33 while(1){
34 printf("123456789");
35 memset(writeBuf,0,sizeof(writeBuf));
36 printf("input ");
37 // gets(writeBuf);
38 scanf("%s",writeBuf);
39 write(c_fd,writeBuf,strlen(writeBuf));
40 // sleep(1);
41 }
42 }
43 while(1){
44 // sleep(1);
45 // printf("=================\n");
46 memset(readBuf,0,sizeof(readBuf));
47 read(c_fd,readBuf,128);
48 printf("readBuf context from sever is %s\n",readBuf);
49 }
50
51 }
52 return 0;
53 }
~