Got the ground station converted to UDP as planned. A UDP client in Java is straightforward,
Send to server:
dgram = new DatagramSocket();
inetAddress = InetAddress.getByName(hostname);
dpIn = new DatagramPacket(buffer, buffer.length, inetAddress, port);
dgram.send(dpIn);
Read response from server after sending:
dpOut = new DatagramPacket(buffer, buffer.length);
dgram.receive(dpOut);
A UDP server in C is convoluted:
Wait for client to send something:
socket_fd = socket(PF_INET, SOCK_DGRAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(socket_fd, (struct sockaddr*)&addr, sizeof(addr)) >= 0)
struct sockaddr_in si_other;
int slen = sizeof(si_other);
recvfrom(socket_fd, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&si_other, &slen);
Send response to client:
sendto(socket_fd, data, size, 0, (struct sockaddr *)&si_other, slen);
A UDP client in C is a bit more bearable:
struct sockaddr_in name;
struct hostent *hostinfo;
name.sin_family = AF_INET;
name.sin_port = htons(port);
hostinfo = gethostbyname(hostname);
name.sin_addr = *(struct in_addr *)hostinfo->h_addr;
connect(socket, (struct sockaddr*)&name, sizeof(name))
write(socket, buffer, BUFFER_SIZE);
read(socket, buffer, BUFFER_SIZE);
The read call needs to have enough buffer space to hold a complete packet or it truncates the packet.
The key is using recvfrom for the server to get the port to send the response to, because even
...Continue Reading