赞
踩
用TCP协议编写一个简单的服务器,客户端,服务器一直监听本机的6666端口。
server.cpp代码
- #include <errno.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <unistd.h>
-
- constexpr int MAXLNE = 4096;
-
- int main(int argc, char **argv)
- {
- int listenfd, connfd, n;
- struct sockaddr_in servaddr;
- char buff[MAXLNE];
-
- if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
- printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
- return 0;
- }
-
- memset(&servaddr, 0, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
- servaddr.sin_port = htons(6666);
-
- if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
- printf("bind socket error: %s(errno: %d)\n", strerror(errno), errno);
- return 0;
- }
-
- if (listen(listenfd, 10) == -1) {
- printf("listen socket error: %s(errno: %d)\n", strerror(errno), errno);
- return 0;
- }
-
- printf("========waiting for client's request========\n");
- while (true) {
- if ((connfd = accept(listenfd, (struct sockaddr *)nullptr, nullptr)) == -1) {
- printf("accept socket error: %s(errno: %d)\n", strerror(errno), errno);
- continue;
- }
- n = recv(connfd, buff, MAXLNE, 0);
- buff[n] = '\0';
- printf("recv msg from client: %s\n", buff);
- close(connfd);
- }
-
- close(listenfd);
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
client.cpp代码
- #include <arpa/inet.h>
- #include <errno.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <unistd.h>
-
- constexpr int MAXLNE = 4096;
-
- int main(int argc, char **argv)
- {
- int sockfd, n;
- struct sockaddr_in servaddr;
- char sendline[MAXLNE], recvline[MAXLNE];
-
- if (argc != 2) {
- printf("usage: ./client <ipaddress>\n");
- return 0;
- }
-
- if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
- printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
- return 0;
- }
-
- memset(&servaddr, 0, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_port = htons(6666);
- if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
- printf("inet_pton error for %s\n", argv[1]);
- return 0;
- }
-
- if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
- printf("connect error: %s(errno: %d)\n", strerror(errno), errno);
- return 0;
- }
-
- printf("send msg to server: \n");
- fgets(sendline, MAXLNE, stdin);
- if (send(sockfd, sendline, strlen(sendline), 0) < 0) {
- printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
- return 0;
- }
-
- close(sockfd);
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
makefile代码
- all:server client
- server:server.o
- g++ -g -o server server.o
- client:client.o
- g++ -g -o client client.o
- server.o:server.cpp
- g++ -g -c server.cpp
- clinet.o:client.cpp
- g++ -g -c client.cpp
- clean:all
- rm all
执行make命令,打开两个终端一个执行./server一个执行./client 127.0.0.1
当前代码只实现了单客户端连接服务端,如何实现一个简单的并发服务器呢?请看一个简单的并发tcp server
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。