I need to connect to a UDP server that listens on port 9000 but then
redirects to another port to communicate with. The following code
snippet taken from db-sockets tests.lisp file does not
work. It just hangs, it works fine for example to talk udp with echo
server port 7 which does
communicate back on port 7. The following c code program does work
fine for echo server or the
server I need to communicate that redirects it port. What do i need
to do to get this to work in
db-sockets or any other lisp socket library. I'm using Debian and
CMUCL.
(defun simple-udp-client ()
(let ((s (make-instance 'inet-socket :type :datagram :protocol
(get-protocol-by-name "udp")))
(data (make-string 200)))
(format t "Socket type is ~A~%" (sockopt-type s))
(socket-connect s #(127 0 0 1) 9000)
(let ((stream (socket-make-stream s :input t :output t :buffering
:none)))
(format stream "here is some text")
(let ((data (subseq data 0 (read-buf-nonblock data stream))))
(format t "~&Got ~S back from UDP echo server~%" data)
(> (length data) 0)))))
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#define ADDRESS "127.0.0.1"
#define PORT 9000
int main()
{ struct sockaddr_in serveraddr;
int sd;
char buf[1024], sendmsg[]="test message";
sd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(PORT);
serveraddr.sin_addr.s_addr = inet_addr(ADDRESS);
sendto( sd, sendmsg, strlen(sendmsg), 0, (struct sockaddr *)
&serveraddr,
sizeof(serveraddr));
recvfrom( sd, buf, 1024, 0, NULL, NULL);
close(sd);
return 0; }
Hi Paul,
I am not sure about your Lisp part. But in the C part, I think you
need to call bind() for the udp socket before you can do sendto().
>
> sd = socket(AF_INET, SOCK_DGRAM, 0);
>
> memset(&serveraddr, 0, sizeof(serveraddr));
> serveraddr.sin_family = AF_INET;
> serveraddr.sin_port = htons(PORT);
> serveraddr.sin_addr.s_addr = inet_addr(ADDRESS);
=========
try add this few lines:
serveraddr.sin_port = 0;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));
==========
>
> sendto( sd, sendmsg, strlen(sendmsg), 0, (struct sockaddr *)
> &serveraddr,
> sizeof(serveraddr));
>
> recvfrom( sd, buf, 1024, 0, NULL, NULL);
>
> close(sd);
> return 0; }
I hope it will work.
cheer.
Willie