Download Embedded Xinu Documentation

Transcript
Embedded Xinu Documentation, Release master
char *dest = args[1];
struct netaddr dst;
struct netaddr *localhost;
struct netif *interface;
int len;
/* Allocate a new TCP device */
if ((ushort)SYSERR == (dev = tcpAlloc()))
{
fprintf(stderr, "Client: Failed to allocate a TCP device.");
return SYSERR;
}
/* Look up local ip info */
interface = netLookup((ethertab[0].dev)->num);
if (NULL == interface)
{
fprintf(stderr, "Client: No network interface found\r\n");
return SYSERR;
}
localhost = &(interface->ip);
/* Change the destination to ipv4 */
if (SYSERR == dot2ipv4(dest, &dst))
{
fprintf(stderr, "Client: Failed to convert ip address.");
return SYSERR;
}
/* Open the TCP device with the destination and echo port*/
if (SYSERR == open(dev, localhost, &dst, NULL, ECHO_PORT, TCP_ACTIVE))
{
fprintf(stderr, "Client: Could not open the TCP device\r\n");
return SYSERR;
}
/* Send the message to the destination*/
memcpy(buf, args[2], MSG_MAX_LEN);
if(SYSERR == write(dev, buf, MSG_MAX_LEN))
{
fprintf(stderr, "Client: Error writing packet to the network");
close(dev);
return SYSERR;
}
/* Read a response from the server */
if(SYSERR != (len = read(dev, buf, MSG_MAX_LEN)))
{
/* Manual null termination needed in case of bad/malicious response
buf[len] = ’\0’;
printf("Client: Got response - %s\r\n", buf);
}
*/
/* Close the device when done */
close(dev);
3.8. Networking
25