基于window系统,使用c语言,使用socket库实现client端到server端的通信综合系统功能,模拟KTV点歌系统,具体功能:Client 1向Server端发送查询请求,需要支持按照歌曲名字查找以及按照歌手名字查找,接收到Server端响应后,打印查询结果。 Server接收用户(Client1)的查询请求 Server接收管理员(Client2) 的添加/删除歌曲请求,操作成功后返回OK Client 2管理员向Server端发送添加和删除歌曲请求,请用代码实现Client1端,Client2端,Serve端功能
时间: 2023-11-18 10:06:17 浏览: 215
以下是基于Windows系统,使用C语言和Socket库实现的Client1、Client2和Server端功能代码。其中,Client1负责向Server端发送查询请求,Client2负责向Server端发送添加/删除歌曲请求,Server端接收并处理这些请求,返回相应结果。
### Client1代码
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define BUF_SIZE 1024
int main()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed.\n");
return 1;
}
// 创建socket
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
printf("socket failed.\n");
WSACleanup();
return 1;
}
// 设置服务器地址
struct sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // 服务器IP地址
servAddr.sin_port = htons(12345); // 服务器端口号
// 连接服务器
if (connect(sock, (struct sockaddr*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR) {
printf("connect failed.\n");
closesocket(sock);
WSACleanup();
return 1;
}
// 发送查询请求
char buf[BUF_SIZE];
printf("请输入查询类型(1:按歌曲名查询,2:按歌手名查询):");
int type;
scanf("%d", &type);
printf("请输入查询关键字:");
char keyword[BUF_SIZE];
scanf("%s", keyword);
sprintf(buf, "%d %s", type, keyword);
send(sock, buf, strlen(buf) + 1, 0);
// 接收服务器响应
memset(buf, 0, BUF_SIZE);
recv(sock, buf, BUF_SIZE, 0);
// 打印查询结果
printf("%s\n", buf);
// 关闭socket
closesocket(sock);
// 清理Winsock环境
WSACleanup();
return 0;
}
```
### Client2代码
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define BUF_SIZE 1024
int main()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed.\n");
return 1;
}
// 创建socket
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
printf("socket failed.\n");
WSACleanup();
return 1;
}
// 设置服务器地址
struct sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // 服务器IP地址
servAddr.sin_port = htons(12345); // 服务器端口号
// 连接服务器
if (connect(sock, (struct sockaddr*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR) {
printf("connect failed.\n");
closesocket(sock);
WSACleanup();
return 1;
}
// 发送添加/删除歌曲请求
char buf[BUF_SIZE];
printf("请输入操作类型(1:添加歌曲,2:删除歌曲):");
int type;
scanf("%d", &type);
printf("请输入歌曲名:");
char songName[BUF_SIZE];
scanf("%s", songName);
printf("请输入歌手名:");
char singerName[BUF_SIZE];
scanf("%s", singerName);
sprintf(buf, "%d %s %s", type, songName, singerName);
send(sock, buf, strlen(buf) + 1, 0);
// 接收服务器响应
memset(buf, 0, BUF_SIZE);
recv(sock, buf, BUF_SIZE, 0);
// 打印操作结果
printf("%s\n", buf);
// 关闭socket
closesocket(sock);
// 清理Winsock环境
WSACleanup();
return 0;
}
```
### Server代码
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define BUF_SIZE 1024
struct Song {
char name[BUF_SIZE];
char singer[BUF_SIZE];
};
struct Song songList[] = {
{"歌曲1", "歌手1"},
{"歌曲2", "歌手2"},
{"歌曲3", "歌手3"},
{"歌曲4", "歌手4"},
{"歌曲5", "歌手5"}
};
char* searchSongByName(char* name)
{
int i;
for (i = 0; i < sizeof(songList) / sizeof(songList[0]); i++) {
if (strcmp(name, songList[i].name) == 0) {
return songList[i].singer;
}
}
return "未找到该歌曲";
}
char* searchSongBySinger(char* singer)
{
int i;
char* result = (char*)malloc(BUF_SIZE * sizeof(char));
memset(result, 0, BUF_SIZE);
for (i = 0; i < sizeof(songList) / sizeof(songList[0]); i++) {
if (strcmp(singer, songList[i].singer) == 0) {
strcat(result, songList[i].name);
strcat(result, " ");
}
}
if (strlen(result) == 0) {
strcat(result, "未找到该歌手的歌曲");
}
return result;
}
char* addSong(char* songName, char* singerName)
{
int i;
for (i = 0; i < sizeof(songList) / sizeof(songList[0]); i++) {
if (strcmp(songName, songList[i].name) == 0 && strcmp(singerName, songList[i].singer) == 0) {
return "该歌曲已存在";
}
}
struct Song newSong;
strcpy(newSong.name, songName);
strcpy(newSong.singer, singerName);
if (sizeof(songList) / sizeof(songList[0]) < BUF_SIZE) {
songList[sizeof(songList) / sizeof(songList[0])] = newSong;
return "添加成功";
}
else {
return "歌曲列表已满,无法添加";
}
}
char* deleteSong(char* songName, char* singerName)
{
int i, j;
for (i = 0; i < sizeof(songList) / sizeof(songList[0]); i++) {
if (strcmp(songName, songList[i].name) == 0 && strcmp(singerName, songList[i].singer) == 0) {
for (j = i; j < sizeof(songList) / sizeof(songList[0]) - 1; j++) {
songList[j] = songList[j + 1];
}
return "删除成功";
}
}
return "未找到该歌曲";
}
int main()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed.\n");
return 1;
}
// 创建socket
SOCKET servSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (servSock == INVALID_SOCKET) {
printf("socket failed.\n");
WSACleanup();
return 1;
}
// 绑定端口号和IP地址
struct sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY); // 使用任意可用的IP地址
servAddr.sin_port = htons(12345); // 端口号为12345
if (bind(servSock, (struct sockaddr*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR) {
printf("bind failed.\n");
closesocket(servSock);
WSACleanup();
return 1;
}
// 监听客户端连接请求
if (listen(servSock, SOMAXCONN) == SOCKET_ERROR) {
printf("listen failed.\n");
closesocket(servSock);
WSACleanup();
return 1;
}
// 接收并处理客户端请求
while (1) {
// 等待客户端连接
struct sockaddr_in clntAddr;
int clntAddrLen = sizeof(clntAddr);
SOCKET clntSock = accept(servSock, (struct sockaddr*)&clntAddr, &clntAddrLen);
if (clntSock == INVALID_SOCKET) {
printf("accept failed.\n");
continue;
}
// 接收客户端请求
char buf[BUF_SIZE];
memset(buf, 0, BUF_SIZE);
recv(clntSock, buf, BUF_SIZE, 0);
// 处理客户端请求
if (buf[0] == '1') { // 按歌曲名查询
char* singerName = searchSongByName(buf + 2);
send(clntSock, singerName, strlen(singerName) + 1, 0);
}
else if (buf[0] == '2') { // 按歌手名查询
char* songList = searchSongBySinger(buf + 2);
send(clntSock, songList, strlen(songList) + 1, 0);
free(songList);
}
else if (buf[0] == '3') { // 添加歌曲
char* result = addSong(buf + 2, strstr(buf + 2, " ") + 1);
send(clntSock, result, strlen(result) + 1, 0);
}
else if (buf[0] == '4') { // 删除歌曲
char* result = deleteSong(buf + 2, strstr(buf + 2, " ") + 1);
send(clntSock, result, strlen(result) + 1, 0);
}
// 关闭客户端socket
closesocket(clntSock);
}
// 关闭服务器socket
closesocket(servSock);
// 清理Winsock环境
WSACleanup();
return 0;
}
```
需要注意的是,以上代码没有进行错误处理和异常情况处理,仅为演示功能实现。在实际开发中,应该对各种错误和异常情况进行处理,保证程序的健壮性和稳定性。
阅读全文