先决条件:
- C / C++中的套接字编程,
- 使用select的TCP和UDP服务器
- UDP Server-Client在C中的实现
- C语言中的TCP客户端-服务器实现
本文介绍了客户端和服务器的设置,客户端在其中连接,将字符串发送到服务器,服务器显示原始字符串,并使用套接字连接向客户端发送该字符串是否为回文的确认。
例子:
Input: naman
Output: Palindrome
Input: geek
Output: Not Palindrome
方法:
- 在此,首先建立一个客户端-服务器连接。
- 建立连接后,客户端将通过发送系统调用将用户输入的字符串发送到服务器。
- 在服务器端,服务器将等待客户端发送的字符串。
- 服务器通过读取系统调用读取字符串。
- 此后,服务器将检查字符串是否为回文,并将确认发送回客户端。
编译:
- 首先,运行服务器程序为
gcc server.c -o server ./server
- 在另一个终端上运行客户端程序
gcc client.c -o client ./client
- 服务器程序正在等待客户端发送的字符串。
- 在客户端输入字符串。
- 服务器程序将打印原始字符串。
- 客户端程序将打印结果。
下面是上述方法的实现:
TCP Server
// defines in_addr structure
#include
// contains constants and structures
// needed for internet domain addresses
#include
// standard input and output library
#include
// contains string functions
#include
// for socket creation
#include
// contains constructs that facilitate getting
// information about files attributes.
#include
// contains a number of basic derived types
// that should be used whenever appropriate
#include
main()
{
struct sockaddr_in client, server;
int s, n, sock, g, j, left, right, flag;
char b1[20], b2[10], b3[10], b4[10];
// creating socket
s = socket(AF_INET, SOCK_STREAM, 0);
// assign IP, PORT
server.sin_family = AF_INET;
// this is the port number of running server
server.sin_port = 2000;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
// Binding newly created socket
// to given IP and verification
bind(s, (struct sockaddr*)&server, sizeof server);
listen(s, 1);
n = sizeof client;
sock = accept(s, (struct sockaddr*)&client, &n);
for (;;) {
recv(sock, b1, sizeof(b1), 0);
// whenever a request from a client came.
// It will be processed here.
printf("\nThe string received is:%s\n", b1);
if (strlen(b1) == 0)
flag = 1;
else {
left = 0;
right = strlen(b1) - 1;
flag = 1;
while (left < right && flag) {
if (b1[left] != b1[right])
flag = 0;
else {
left++;
right--;
}
}
}
send(sock, &flag, sizeof(int), 0);
break;
}
close(sock);
// close the socket
close(s);
}
TCP Client
// defines in_addr structure
#include
// contains constants and structures
// needed for internet domain addresses
#include
// standard input and output library
#include
// contains string functions
#include
// for socket creation
#include
// contains constructs that facilitate getting
// information about files attributes.
#include
// contains a number of basic derived types
// that should be used whenever appropriate
#include
main()
{
struct sockaddr_in client;
int s, flag;
char buffer[20];
// socket create
s = socket(AF_INET, SOCK_STREAM, 0);
// assign IP, PORT
client.sin_family = AF_INET;
client.sin_port = 2000;
client.sin_addr.s_addr = inet_addr("127.0.0.1");
// connect the client socket to server socket
connect(s, (struct sockaddr*)&client, sizeof client);
for (;;) {
printf("\nEnter a string to check palindrome: ");
scanf("%s", buffer);
printf("\nClient: %s", buffer);
send(s, buffer, sizeof(buffer), 0);
recv(s, &flag, sizeof(int), 0);
if (flag == 1) {
printf("\nServer: The string is a Palindrome.\n");
break;
}
else {
printf("\nServer: The string is not a palindrome.\n");
break;
}
}
// close the socket
close(s);
}
输出:
- 服务器端:
- 客户端:
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。