C中的UDP套接字:客户端1到服务器到客户端2
问题描述:
我对网络编程非常陌生,所以对我来说很裸露。到目前为止,我有一个UDP客户端/服务器以大写或小写形式向服务器发送消息。服务器收到该消息并将其中继给交换案例。我无法弄清楚的是我将如何让服务器将其转发回第一个客户端,而不是将其发送给客户端2。如果我的客户端2发送一条消息,则服务器收到并将其发送回客户端2,与客户端1发生同样的事情。我想要什么client1说被发送到服务器,然后服务器发送到client2。我试过了我能想到的一切,但无法弄清楚。C中的UDP套接字:客户端1到服务器到客户端2
服务器:
/*
Simple udp server
*/
#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>
#include<ctype.h>
#define BUFLEN 512 //Max length of buffer
#define PORT 8888 //The port on which to listen for incoming data
void die(char *s)
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_me, si_other, si_other2;
int s, i, slen = sizeof(si_other) , recv_len;
char buf[BUFLEN];
//create a UDP socket
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die("socket");
}
// zero out the structure
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
//bind socket to port
if(bind(s , (struct sockaddr*)&si_me, sizeof(si_me)) == -1)
{
die("bind");
}
//keep listening for data
while(1)
{
printf("Waiting for data...");
fflush(stdout);
//try to receive some data, this is a blocking call
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1) // read datagram from server socket
{
die("recvfrom()");
}
//print details of the client/peer and the data received
printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port)); printf("Data: %s\n" , buf);
//now reply to server socket/the client with the same data
if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == -1)
{
die("sendto()");
}
}
close(s);
return 0;
}
客户:
/*
Simple udp client
*/
#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>
#include<ctype.h>
#define SERVER "192.x.x.x"
#define BUFLEN 512 //Max length of buffer
#define PORT 8888 //The port on which to send data
void die(char *s)
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_other;
int s, s2, i, slen=sizeof(si_other);
char buf[BUFLEN];
char message[BUFLEN];
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) // create a client socket
{
die("socket");
}
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
if (inet_aton(SERVER , &si_other.sin_addr) == 0) // Create datagram with server IP and port.
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
while(1)
{
printf("Enter message : ");
gets(message);
int a;
char message2[BUFLEN];
for(a=0;a<=BUFLEN-1;a++)
{
if(message[a] >= 97 && message[a] <= 122)
message2[a] = toupper(message[a]);
else
message2[a] = tolower(message[a]);
}
if (sendto(s, message2, strlen(message2) , 0 , (struct sockaddr *) &si_other, slen)==-1)
{
die("sendto()");
}
//receive a reply and print it
//clear the buffer by filling null, it might have previously received data
memset(buf,'\0', BUFLEN);
//try to receive some data, this is a blocking call
if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == -1) // read datagram from client socket
{
die("recvfrom()");
}
puts(buf);
}
close(s);
return 0;
}
答
这似乎不是一个UDP服务器,你想拥有像客户端 - >服务器 - >客户端2包的转发,所以你需要告诉你的服务器client2的ip地址/端口,因为这样可以使用命令行参数或者读取任何输入文件来在服务器上配置client2 ip/port,然后在服务器代码中的sendto语句之前,填充si_other结构,其client2信息为从命令行或输入文件中取消。
答
后您read()
或recvfrom()
你的消息,你处理你的数据,然后sendto()
两次:一个向其他客户端的地址由recvfrom()
(原始发件人)的返回地址等(必须通过提供或检测某种程度上你服务器)。
沿着这样的东西(适当的错误检查没有正在执行):
char data[100];
struct sockaddr_in address;
socklen_t length = sizeof address;
/* Receive data from any client. */
ssize_t result = recvfrom(server, data, sizeof data, 0, &address, &length);
/* Process the data (change cases). */
process_data(data, result);
/* Send back to the first client. */
sendto(server, data, result, 0, &address, sizeof address);
/* Check who's the sender and relay to the other. */
if (address.sin_addr.s_addr == CLIENT1_ADDRESS)
address.sin_addr.s_addr = CLIENT2_ADDRESS;
else
address.sin_addr.s_addr = CLIENT1_ADDRESS;
/* Send to the other client. */
sendto(server, data, result, 0, &address, sizeof address);
在本例中,地址被静态定义。
您希望服务器如何跟踪client1和client2的地址?换句话说,当服务器从client1接收到一些东西时,你如何期望它知道client2的地址,以便它可以将它发送到那里?例如,它可以在服务器的命令行或配置文件中提供,或者服务器可以记住它收到消息的前两个(或最近两个)远程地址等。 – Celada 2013-02-24 18:26:35