我基于以下做了一个简单的TCP服务器和客户端 https://www.geeksforgeeks.org/tcp-server-client-implementation-in-c/
如果我按原样编译示例代码,我发现编译错误。 所以我对原来的代码做了一点修改。 这是服务器代码。
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#define PORT 12345
#define MAX 80
/* Function designed for chat between client and server */
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;)
{
bzero(buff, MAX);
/* read the message from client and copy it in buffer */
read(sockfd, buff, sizeof(buff));
/* print buffer which contains the client contents */
printf("From client : %s\t To client : ", buff);
bzero(buff, MAX);
n = 0;
/* copy server message in the buffer */
while ((buff[n++] = getchar()) != '\n')
;
/* and send that buffer to client */
write(sockfd, buff, sizeof(buff));
/* if msg contains "Exit" then server exit and chat ended */
if (strncmp("exit", buff, 4) == 0)
{
printf("Server Exit ....\n");
break;
}
}
}
int main()
{
int sockfd, connfd, len;
struct sockaddr_in servaddr, cli;
struct sockaddr_in *sa;
struct ifaddrs *ifaddr, *ifa;
char *addr;
int family;
if (getifaddrs(&ifaddr) == -1)
{
perror("getifaddrs");
exit(0);
}
for (ifa = ifaddr ; ifa != NULL ; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
if (family == AF_INET)
{
sa = (struct sockaddr_in *)ifa->ifa_addr;
addr = inet_ntoa(sa->sin_addr);
printf("Interface : %s, Address : %s\n", ifa->ifa_name, addr);
}
}
/* socket creation and verification */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
printf("socket creation failed...\n");
exit(0);
}
else
{
printf("socket successfully created...\n");
}
bzero(&servaddr, sizeof(servaddr));
/* assign IP and PORT */
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
if ((bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) != 0)
{
printf("socket bind failed...\n");
exit(0);
}
else
{
printf("Socket successfully binded...\n");
}
/* Now server is ready to listen and verification */
if ((listen(sockfd, 5)) != 0)
{
printf("Listen failed ...\n");
exit(0);
}
else
{
printf("Server listening ...\n");
}
len = sizeof(cli);
/* Accept the data packet from client and verification */
connfd = accept(sockfd, (struct sockaddr *)&cli, &len);
if (connfd < 0)
{
printf("Server accept failed...\n");
exit(0);
}
else
{
printf("Server accept the client...\n");
}
/* Function for chatting between client and server */
func(connfd);
/* After chatting, close the socket */
close(sockfd);
}
这是客户端代码。
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#define MAX 80
#define PORT 12345
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;)
{
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0)
{
printf("Client Exit ...\n");
break;
}
}
}
int main()
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
/* socket create and verification */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
printf("socket creation failed...\n");
exit(0);
}
else
{
printf("socket successfully created...\n");
}
bzero(&servaddr, sizeof(servaddr));
/* assign IP, PORT */
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("192.168.1.203");
servaddr.sin_port = htons(PORT);
/* connect the client socket to server socket */
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0)
{
printf("connection with the server failed ...[%s]\n", strerror(errno));
exit(0);
}
else
{
printf("connected to the server ...\n");
}
/* function for chat */
func(sockfd);
/* close the socket */
close(sockfd);
return 0;
}
我在 192.168.1.203 上运行服务器,在 192.168.1.219 上运行客户端。 我可以在 192.168.1.203 和 192.168.1.219 之间 ping 通。 并且iptables都被清空了。 (运行 iptables -F)。
但是从客户端,我看到“没有到主机的路由”错误消息。 这是我执行服务器程序时来自服务器端的消息。
# ./server
Interface : lo, Address : 127.0.0.1
Interface : ens37, Address : 192.168.1.203
socket successfully created...
Socket successfully binded...
Server listening ...
以下是客户端发来的消息
# ./client
# ./client
socket successfully created...
connection with the server failed ...[No route to host]
如果有人指出我做错了什么,那就太好了。
我测试了几个想法,终于找到了原因。 当我使用“systemctl”从 CentOS8 系统停止防火墙时 停止防火墙”。 然后,一切顺利。