服务器端代码:
- /**************************************************
- ##filename : server.c
- ##author : GYZ
- ##e-mail : 1746902011@qq.com
- ##create time : 2018-07-23 10:41:42
- ##last modified : 2018-07-26 13:27:19
- ##description : NA
- **************************************************/
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #include <pthread.h>
-
- void pthread_func(int*);
-
- int main(int argc,char *argv[])
- {
- int server_sockfd;
- int client_sockfd;
- int ret;
- struct sockaddr_in server_addr;
- struct sockaddr_in client_addr;
- socklen_t server_addrlen;
- socklen_t client_addrlen;
- int on = 1;
-
- /*1,create a socket*/
- /*IPPROTO=0,dumserver protocol for TCP*/
- server_sockfd = socket(AF_INET,SOCK_STREAM,0);
- if(-1 == server_sockfd)
- {
- perror("socket"),exit(-1);
- }
- /*1.5 enable address reuse*/
- ret = setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
- /*2,bind the socket with an ip*/
- memset(&server_addr,0,sizeof(struct sockaddr_in));
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(3490);
- server_addr.sin_addr.s_addr = inet_addr("172.23.7.179");
- server_addrlen = sizeof(struct sockaddr);
- ret = bind(server_sockfd,(const struct sockaddr*)&server_addr,server_addrlen);
- if(-1 == ret)
- {
- perror("bind"),exit(-1);
- }
- /*3,listen the socket*/
- ret = listen(server_sockfd,6);
- if(-1 == ret)
- {
- perror("listen"),exit(-1);
- }
- while(1)
- {
- /*4,accept the data from the client*/
- client_addrlen = sizeof(struct sockaddr);
- client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_addr,&client_addrlen);
- if(client_sockfd > 0)
- {
- printf("IP is %s\n",inet_ntoa(client_addr.sin_addr));
- printf("Port is %d\n",ntohs(client_addr.sin_port));
- pthread_t pt;
- printf("%d\n",client_sockfd);
- ret = pthread_create(&pt,NULL,(void *)pthread_func,&client_sockfd);
- if(0 != ret)
- {
- printf("ip:%s connect failed",inet_ntoa(client_addr.sin_addr));
- }
- }
- }
- close(server_sockfd);
- return 0;
- }
- void pthread_func(int* p_client_sockfd)
- {
- int client_sockfd = *p_client_sockfd;
- printf("%d\n",client_sockfd);
- int ret;
- char data[100];
- char data1[100];
- while(1)
- {
- /*5,read the data from client*/
- ret = recv(client_sockfd,data,100,0);
- if(-1 == ret)
- {
- perror("recv"),exit(-1);
- }
- printf("data from client:%s\n",data);
- /*6,send the data to client*/
- printf("server send data:");
- scanf("%s",data1);
- ret = send(client_sockfd,data1,100,0);
- if(-1 == ret)
- {
- perror("send"),exit(-1);
- }
- }
- close(client_sockfd);
- }
- 客户端代码:
-
- /**************************************************
- ##filename : client.c
- ##author : GYZ
- ##e-mail : 1746902011@qq.com
- ##create time : 2018-07-23 16:36:34
- ##last modified : 2018-07-26 13:14:36
- ##description : NA
- **************************************************/
-
- /#include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #include <pthread.h>
-
- int main(int argc,char *argv[])
- {
- int client_sockfd;
- struct sockaddr_in client_addr;
- socklen_t client_addrlen;
- char data[20];
- char data1[20];
- int ret;
-
- /*1,create a socket*/
- client_sockfd = socket(AF_INET,SOCK_STREAM,0);
- if(-1 == client_sockfd)
- {
- perror("socket"),exit(-1);
- }
- /*2,connect to the server*/
- memset(&client_addr,0,sizeof(struct sockaddr));
- client_addr.sin_family = AF_INET;
- client_addr.sin_port = htons(3490);
- client_addr.sin_addr.s_addr = inet_addr("172.23.7.179");
- client_addrlen = sizeof(struct sockaddr);
- ret = connect(client_sockfd,(const struct sockaddr *)&client_addr,client_addrlen);
- if(-1 == ret)
- {
- perror("connect"),exit(-1);
- }
- while(1)
- {
- /*3,send the data to server*/
- printf("client data are :");
- scanf("%s",data);
- ret = send(client_sockfd,data,20,0);
- if(-1 == ret)
- {
- perror("write"),exit(-1);
- }
- /*4,receive the data from server*/
- ret = recv(client_sockfd,data1,100,0);
- if(-1 == ret)
- {
- perror("recv"),exit(-1);
- }
- printf("data from server:%s\n",data1);
- }
- close(client_sockfd);
- return 0;
- }
-
-
- makefile文件:
-
- all:server client
-
- CC = gcc
- SRC1 = server.c
- SRC2 = client.c
- OBJE1 = server
- OBJE2 = client
-
- ${OBJE1}:
- ${CC} ${SRC1} -o ${OBJE1} -pthread
- ${OBJE2}:
- ${CC} ${SRC2} -o ${OBJE2} -pthread
-
- .PHONY:clean
- clean :
- rm ${OBJE1} ${OBJE2}
复制代码
|