在服务器上使用Vim编写客户端和服务端代码,实现一对一聊天功能,使用UDP套接字编程模型,C语言具体实现
时间: 2023-12-29 17:03:11 浏览: 82
好的,下面给您提供一个基本的UDP一对一聊天代码示例(使用Vim编写),请参考:
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BUF_SIZE 1024
void error_handling(char *message);
int main(int argc, char *argv[])
{
int sock;
char message[BUF_SIZE];
int str_len;
socklen_t addr_size;
struct sockaddr_in serv_addr, from_addr;
if(argc != 3) {
printf("Usage : %s <IP> <port>\n", argv[0]);
exit(1);
}
sock = socket(PF_INET, SOCK_DGRAM, 0);
if(sock == -1)
error_handling("socket() error");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(atoi(argv[2]));
while(1) {
fputs("Input message(Q to quit): ", stdout);
fgets(message, BUF_SIZE, stdin);
if(!strcmp(message, "q\n") || !strcmp(message, "Q\n"))
break;
sendto(sock, message, strlen(message), 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
addr_size = sizeof(from_addr);
str_len = recvfrom(sock, message, BUF_SIZE, 0, (struct sockaddr*)&from_addr, &addr_size);
message[str_len] = 0;
printf("Message from server: %s", message);
}
close(sock);
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
```
服务端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BUF_SIZE 1024
void error_handling(char *message);
int main(int argc, char *argv[])
{
int serv_sock;
char message[BUF_SIZE];
int str_len;
socklen_t clnt_addr_size;
struct sockaddr_in serv_addr, clnt_addr;
if(argc != 2) {
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
serv_sock = socket(PF_INET, SOCK_DGRAM, 0);
if(serv_sock == -1)
error_handling("UDP socket creation error");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
if(bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == -1)
error_handling("bind() error");
while(1) {
clnt_addr_size = sizeof(clnt_addr);
str_len = recvfrom(serv_sock, message, BUF_SIZE, 0, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
sendto(serv_sock, message, str_len, 0, (struct sockaddr*)&clnt_addr, sizeof(clnt_addr));
message[str_len] = 0;
printf("Message from client: %s", message);
}
close(serv_sock);
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
```
注意事项:
1. 编译命令为:`gcc -o client client.c` 和 `gcc -o server server.c`
2. 运行服务端的命令为:`./server <port>`
3. 运行客户端的命令为:`./client <IP> <port>`(其中,<IP> 是服务端的 IP 地址)
4. 在客户端输入消息后,按下回车键即可发送消息,当想退出时,在客户端输入 "q" 或 "Q" 即可
5. 代码中使用了 `recvfrom()` 和 `sendto()` 函数实现UDP通信,`recvfrom()` 函数从 socket 接收数据,`sendto()` 函数将数据发送到 socket
6. 客户端程序通过 socket 向服务端发送消息,服务端通过 socket 接收消息,并将消息原封不动地返回给客户端
希望这段代码对您有所帮助!
阅读全文