Linux基础——socket简单实现与其他IP的聊天

进程实现版服务器端:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <unistd.h>
 5 #include <sys/types.h>
 6 #include <sys/socket.h>
 7 #include <netinet/in.h>
 8 #include <arpa/inet.h>
 9 #include <signal.h>
10 int main(int argc,char *argv[])
11 {
12     int fd_socket;
13     fd_socket=socket(AF_INET,SOCK_STREAM,0);
14     if(fd_socket == -1)
15     {
16         perror("socket");
17         exit(1);
18     }
19     struct sockaddr_in my_addr;
20     memset(&my_addr,0,sizeof(my_addr));
21     my_addr.sin_family = AF_INET;
22     my_addr.sin_addr.s_addr=INADDR_ANY;
23     my_addr.sin_port=htons(atoi(argv[1]));
24     if(-1 == bind(fd_socket,(struct sockaddr*)&my_addr,sizeof(my_addr)))
25     {
26         perror("bind");
27         close(fd_socket);
28         exit(1);
29     }
30     if(listen(fd_socket,5)==-1)
31     {
32         perror("listen");
33         close(fd_socket);
34         exit(1);
35     }
36     int new_fd,len;
37     struct sockaddr_in client_addr;
38     memset(&client_addr,0,sizeof(client_addr));
39     len=sizeof(client_addr);
40     new_fd=accept(fd_socket,(struct sockaddr*)&client_addr,&len);
41     if(new_fd==-1)
42     {
43         perror("accept");
44         close(fd_socket);
45         exit(1);
46     }
47     printf("client : ip : %s   port : %d\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
48     char buf[1024],msg[1024];
49     if(fork()==0)
50     {
51         while(memset(msg,0,1024),recv(new_fd,msg,1024,0)!=0)
52             printf("recv  :  %s  \n",msg);
53     }
54     while(memset(buf,0,1024),fgets(buf,1024,stdin)!=NULL)
55         send(new_fd,buf,strlen(buf),0);
56     close(fd_socket);
57     close(new_fd);
58     wait(NULL);
59     return 0;
60 }
View Code

客户端:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <sys/types.h>
 5 #include <sys/socket.h>
 6 #include <netinet/in.h>
 7 #include <arpa/inet.h>
 8 #include <unistd.h>
 9 #define IP "192.168.0.149"
10 int main(int argc,char *argv[])
11 {
12     int fd;
13     fd=socket(AF_INET,SOCK_STREAM,0);
14     if(fd==-1)
15     {
16         perror("socket");
17         exit(1);
18     }
19     struct sockaddr_in server_addr;
20     memset(&server_addr,0,sizeof(server_addr));
21     server_addr.sin_family=AF_INET;
22     server_addr.sin_addr.s_addr=inet_addr(IP);
23     server_addr.sin_port=htons(8080);
24     if(connect(fd,(struct sockaddr*)&server_addr,sizeof(server_addr))==-1)
25     {
26         perror("connect");
27         close(fd);
28         exit(1);
29     }
30     printf("connect succesed!!\n");
31     char buf[1024],msg[1024];
32     while(1)
33     {
34         if(fork()>0)
35         {
36             while(memset(buf,0,1024),fgets(buf,1024,stdin)!=NULL)
37                 send(fd,buf,strlen(buf),0);
38             exit(1);
39         }
40         while(memset(msg,0,1024),recv(fd,msg,1024,0)!=0)
41             printf("recv  : %s\n",msg);
42         wait(NULL);
43     }
44     close(fd);
45     return 0;
46 }
View Code

 

 

线程实现版服务器客户端一体:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <unistd.h>
 5 #include <sys/types.h>
 6 #include <sys/socket.h>
 7 #include <netinet/in.h>
 8 #include <arpa/inet.h>
 9 #include <pthread.h>
10 #include <netdb.h>
11 #define A 1
12 #define P 0
13 #define S 8888
14 #define C 9999
15 void *hand(void*arg)
16 {
17     int fd=(int )arg;
18     char msg[1024];
19     while(memset(msg,0,1024),recv(fd,msg,1024,0)>0)
20         printf("recv : %s\n",msg);
21     close(fd);
22     exit(1);
23 }
24 int main(int argc,char *argv[])
25 {
26     int flag;
27     if(argc==1)
28         flag=P;
29     if(argc==2)
30         flag=A;
31     int fd_server,fd_client;
32     fd_server=socket(AF_INET,SOCK_STREAM,0);
33     if(fd_server==-1)
34     {
35         perror("socket");
36         exit(1);
37     }
38     char name[1024];
39     memset(name,0,1024);
40     struct hostent *p;
41     gethostname(name,1024);
42     p=gethostbyname(name);
43     struct sockaddr_in addr;
44     memset(&addr,0,sizeof(addr));
45     addr.sin_family=AF_INET;
46     if(flag==A)
47         addr.sin_port=htons(C);
48     else if(flag==P)
49         addr.sin_port=htons(S);
50     addr.sin_addr=*(struct in_addr *)(p->h_addr_list)[0];
51     if(-1 == bind(fd_server,(struct sockaddr*)&addr,sizeof(addr)))
52     {
53         perror("bind");
54         close(fd_server);
55         exit(1);
56     }
57     if(flag == P)
58     {
59         if(listen(fd_server,10)==-1)
60         {
61             perror("listen");
62             close(fd_server);
63             exit(1);
64         }
65         struct sockaddr_in addr_client;
66         int len;
67         memset(&addr_client,0,sizeof(addr_client));
68         len=sizeof(addr_client);
69         fd_client=accept(fd_server,(struct sockaddr*)&addr_client,&len);
70         printf("%s : %d is on!!\n",inet_ntoa(addr_client.sin_addr),ntohs(addr_client.sin_port));
71     }
72     else if(flag == A)
73     {
74         fd_client = fd_server;
75         struct sockaddr_in addr_server;
76         memset(&addr_server,0,sizeof(addr_server));
77         addr_server.sin_family=AF_INET;
78         addr_server.sin_port=htons(S);
79         addr_server.sin_addr.s_addr=inet_addr(argv[1]);
80         while(connect(fd_server,(struct sockaddr*)&addr_server,sizeof(addr_server))==-1)
81         {
82             printf("connecting...\n");
83             sleep(1);
84         }
85         printf("connect success!!\n");
86     }
87     pthread_t thd;
88     pthread_create(&thd,NULL,hand,(void*)fd_client);
89     char msg[1024];
90     while(memset(msg,0,1024),fgets(msg,1024,stdin))
91         send(fd_client,msg,strlen(msg),0);
92     pthread_join(thd,NULL);
93     close(fd_server);
94     close(fd_client);
95     return 0;
96 }
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。