You are not logged in.
Pages: 1
Hi!
I am trying to write a simple UDP echo server in C.
The server should listen on a specific port and every incoming udp package has to be redirected to another IP-address on the same port. My first try was to write a TCP echo server that simply echos all incoming data on port 6666 using a while loop:
while( (bytesrecv = recv(socketid, buffer, sizeof(buffer), 0)) > 0)
send(socketid, buffer, bytesrecv, 0);
and it worked. But I think I am to stupid to manage this with an udp socket.
Instead of recv() and send() I use recvfrom() and sendto() with this sockaddr_in struct as parameter. recvfrom() works fine but the sendto() function always returns -1 (Invalid parameter). I think it has something to to with the sockaddr_in struct.
I'd like to post a part of my code but at the moment I cannot access the server my soucecode is on.
Can someone provide me a very simple example?
Thank you very much!!!!!
Thorsten
void
dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen)
{
int n;
socklen_t len;
char mesg[MAXLINE];
for ( ; ; ) {
len = clilen;
n = Recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
Sendto(sockfd, mesg, n, 0, pcliaddr, len);
}
}
Offline
Pages: 1