I am able to connect to the localhost from my TCP client.

Please try with the attached code. This works for me on DM6467. It should be 
the same for DM6446 also.

If it still doesn’t work, the problem will be in the network configuration. You 
can try using 255.255.255.0 as the network mask. Also, please inspect the 
routing table on your testboard.

Regards,
Anand Balagopalakrishnan

________________________________
From: [email protected] [mailto:[email protected]]
Sent: Tuesday, April 07, 2009 3:10 PM
To: Balagopalakrishnan, Anand; 
[email protected]
Subject: 回复: RE: help: how to config kernel to support tcp udp AF_INET


Hi,

   Thanks very much for your help.

   I tried use IPPROTO_TCP instead of 0 as the protocol for both the client and 
server.

      But the client can not connect on the server, the log is the same as 
before.

      In my bootargs, i used ip=10.1.1.11:10.1.1.5:10.1.1.1:255.0.0.0 instead 
ip = DHCP.



Best Regards.

====== 下面是回复邮件 ======

________________________________

发件人: Balagopalakrishnan, Anand
收件人: [email protected],
抄 送:
主 题: RE: help: how to config kernel to support tcp udp AF_INET
时 间: 2009-04-07 15:38
When you create the socket, try using IPPROTO_TCP instead of 0 as the protocol 
for both the client and server. I believe this should solve your problem.

Regards,
Anand Balagopalakrishnan
Texas Instruments Inc
India

________________________________
From: [email protected] 
[mailto:[email protected]] On Behalf Of 
[email protected]
Sent: Friday, April 03, 2009 11:43 AM
To: [email protected]
Subject: help: how to config kernel to support tcp udp AF_INET


Hi;

   Can anyone tell me how to configure kernel to support AF_INET.

   Now  i work with Monavista 2.6.10 on dm6446 evm.

    When i run the server on the testboard

     the server print

      bind start
      bind end
      listen start
      listen end
      server waiting
      accept start



    When i run the client on the testboard,

     the client print

      connect start

    the client can not connect on the server

But when i run the the same test code on the pc

the server print

bind start
bind end
listen start
listen end
server waiting
accept start



the client print

connect start
accept end
read start
connect end
write start
read end
write start
write end
server waiting
accept start
write end
read start
read end
char from server = B

    The client can connect the server .



Here is the  server code

#include
#include
#include
#include
#include

#include
#include

int main()
{
 int server_sockfd, client_sockfd;
 int server_len, client_len;

// struct sockaddr_un server_address;
// struct sockaddr_un client_address;

 struct sockaddr_in server_address;
 struct sockaddr_in client_address;


 unlink("server_socket");
// server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

// server_address.sun_family = AF_UNIX;
// strcpy(server_address.sun_path, "server_socket");

 server_address.sin_family = AF_INET;
// server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
// server_address.sin_port = 9734;
 server_address.sin_addr.s_addr = htonl(INADDR_ANY);
 server_address.sin_port = htons(9734);
 server_len = sizeof(server_address);

 printf("bind start\n");
 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
 printf("bind end\n");

 printf("listen start\n");
 listen(server_sockfd, 5);
 printf("listen end\n");

 while(1)
 {
  char ch;

  printf("server waiting\n");

  client_len = sizeof(client_address);
  printf("accept start\n");
  client_sockfd = accept(server_sockfd, (struct sockaddr *)&client_address, 
&client_len);
  printf("accept end\n");

  printf("read start\n");
  read(client_sockfd, &ch, 1);
  printf("read end\n");

  ch++;
  printf("write start\n");
  write(client_sockfd, &ch, 1);
  printf("write end\n");

  close(client_sockfd);

 }
}

Here is the client code



#include
#include
#include
#include
#include

#include
#include

int main()
{
 int sockfd;
 int len;
// struct sockaddr_un address;
 struct sockaddr_in address;

 int result;
 char ch= 'A';

// sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
 sockfd = socket(AF_INET, SOCK_STREAM, 0);

// address.sun_family = AF_UNIX;
// strcpy(address.sun_path, "server_socket");

 address.sin_family = AF_INET;
 address.sin_addr.s_addr = inet_addr("127.0.0.1");
// address.sin_port = 9734;
 address.sin_port = htons(9734);
 len = sizeof(address);

 printf("connect start\n");
 result = connect(sockfd, (struct sockaddr *)&address, len);
 printf("connect end\n");

 if(result == -1)
 {
  perror("oops: client1");
  exit(1);

 }

 printf("write start\n");
 write(sockfd, &ch, 1);
 printf("write end\n");

 printf("read start\n");
 read(sockfd, &ch, 1);
 printf("read end\n");

 printf("char from server = %c\n", ch);

 close(sockfd);

 exit(0);
}

Thanks.
#include "channel.h"

int main(int argc, char *argv[])
{
  Channel_Info ch;
  char temp[256];

  memset(&ch, 0, sizeof(ch));
  memset(temp, 0, 256);

  ch.type = CHANNEL_TCP;
  ch.role = CHANNEL_SERVER;
  Channel_create(&ch);

  Channel_setMyIp(&ch, CHANNEL_ANY_IF);
  Channel_setMyPort(&ch, atoi(argv[1]));
  Channel_open(&ch);

  printf("Enter:\n");
  scanf("%s", temp);
  Channel_send(&ch, temp, strlen(temp));

  Channel_close(&ch);
}
#include "channel.h"

static Channel_RetCode Channel_openTcpServer(Channel_Info *channel)
{
  Channel_Handle newHandle;
  Channel_RetCode status;
  char ipaddr[MAX_IP_ADDR_LEN];
  socklen_t sockLen=sizeof(Channel_Address);

  memset(ipaddr, 0, MAX_IP_ADDR_LEN);

  channel->me.sin_family = AF_INET;
  status = bind(channel->handle, (SA*)&channel->me, SOCK_LEN); 
  CHANNEL_CHECKSTATUS(status < 0, "Channel TCP bind");

  Channel_getMyIp(channel, ipaddr);
  printf("Channel TCP bound to %s:%d\n", ipaddr, Channel_getMyPort(channel));

  status = listen(channel->handle, 1); 
  CHANNEL_CHECKSTATUS(status < 0, "Channel TCP listen");

  printf("Channel TCP waiting for new connections ...\n");
  newHandle = accept(channel->handle, (SA*) &channel->other, &sockLen);
  CHANNEL_CHECKSTATUS(newHandle < 0, "Channel TCP accept");

  close(channel->handle);
  channel->handle = newHandle;

  Channel_getOtherIp(channel, ipaddr);
  printf("Channel TCP accepted connection from %s:%d\n", ipaddr, 
Channel_getMyPort(channel));

  return CHANNEL_SUCCESS;
}


static Channel_RetCode Channel_openTcpClient(Channel_Info *channel)
{
  Channel_RetCode status;
  char ipaddr[MAX_IP_ADDR_LEN];

  memset(ipaddr, 0, MAX_IP_ADDR_LEN);

  channel->other.sin_family = AF_INET;
  Channel_getOtherIp(channel, ipaddr);
  printf("Channel TCP connecting to %s:%d ...\n", ipaddr, 
Channel_getOtherPort(channel));

  status = connect(channel->handle, (SA*) &channel->other, SOCK_LEN);
  CHANNEL_CHECKSTATUS(status < 0, "Channel TCP connect");

  printf("Channel TCP connected to %s:%d\n", ipaddr, 
Channel_getOtherPort(channel));

  return CHANNEL_SUCCESS;
}

static Channel_RetCode Channel_openMulticastServer(Channel_Info *channel)
{
  Channel_RetCode status;
  struct in_addr iaddr;
  unsigned char ttl=64;
  unsigned char loop=1;

  channel->me.sin_family = AF_INET;
  channel->other.sin_family = AF_INET;

  channel->me.sin_port = htons(0);
  channel->me.sin_addr.s_addr = INADDR_ANY;

  status = bind(channel->handle, (SA*)&channel->me, SOCK_LEN);
  CHANNEL_CHECKSTATUS(status < 0, "Channel UDP Multicast bind\n");

  iaddr.s_addr = INADDR_ANY;

  // Set the outgoing interface to DEFAULT
  // Set multicast packet TTL to 10; default TTL is 1
  // send multicast traffic to myself too
  setsockopt(channel->handle, IPPROTO_UDP, IP_MULTICAST_IF, &iaddr, 
sizeof(struct in_addr));
  setsockopt(channel->handle, IPPROTO_UDP, IP_MULTICAST_TTL, &ttl, 
sizeof(unsigned char));
  status = setsockopt(channel->handle, IPPROTO_UDP, IP_MULTICAST_LOOP, &loop, 
sizeof(unsigned char));

  printf("Channel UDP Multicast ready\n");

  return CHANNEL_SUCCESS;
}

static Channel_RetCode Channel_openMulticastClient(Channel_Info *channel)
{
  Channel_RetCode status;
  struct ip_mreq imreq;
  char ipaddr[MAX_IP_ADDR_LEN];

  channel->me.sin_family = AF_INET;
  channel->me.sin_addr.s_addr = INADDR_ANY;
  channel->me.sin_port = channel->other.sin_port;

  status = bind(channel->handle, (SA*)&channel->me, SOCK_LEN);
  CHANNEL_CHECKSTATUS(status < 0, "Channel UDP Multicast bind\n");

  memset(&imreq, 0, sizeof(struct ip_mreq));
  imreq.imr_interface.s_addr = INADDR_ANY;
  imreq.imr_multiaddr.s_addr = channel->other.sin_addr.s_addr;

  status = setsockopt(channel->handle, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imreq, 
sizeof(struct ip_mreq));
  CHANNEL_CHECKSTATUS(status < 0, "Channel UDP Multicast add membership");

  memset(ipaddr, 0, MAX_IP_ADDR_LEN);
  Channel_getOtherIp(channel, ipaddr);

  printf("Channel UDP Multicast joined to %s:%d\n", ipaddr, 
Channel_getMyPort(channel));

  return CHANNEL_SUCCESS;
}

Channel_RetCode Channel_create(Channel_Info *channel)
{
  int type, protocol;
  int option=1;

  switch (channel->type) {
    case CHANNEL_TCP:
      type = SOCK_STREAM;
      protocol = IPPROTO_TCP;
      break;

    case CHANNEL_UDP:
      type = SOCK_DGRAM;
      protocol = IPPROTO_UDP;
      break;

    case CHANNEL_MULTICAST:
      type = SOCK_DGRAM;
      protocol = IPPROTO_IP;
      break;

    default:
      printf("Incorrect channel type\n");
      return CHANNEL_FAILURE;
  }

  channel->handle = socket(AF_INET, type, protocol);
  CHANNEL_CHECKSTATUS(channel->handle < 0, "Channel create");

  printf("Channel created successfully\n");

  setsockopt(channel->handle, IPPROTO_TCP, SO_REUSEADDR, &option, 
sizeof(option));
  return CHANNEL_SUCCESS;
}

Channel_RetCode Channel_open(Channel_Info *channel)
{
  Channel_RetCode status=CHANNEL_FAILURE;

  if (channel->type == CHANNEL_TCP && channel->role == CHANNEL_SERVER) {
    status = Channel_openTcpServer(channel);
  }
  else if (channel->type == CHANNEL_TCP && channel->role == CHANNEL_CLIENT) {
    status = Channel_openTcpClient(channel);
  }
  else if (channel->type == CHANNEL_MULTICAST && channel->role == 
CHANNEL_SERVER) { 
    status = Channel_openMulticastServer(channel);
  }
  else if (channel->type == CHANNEL_MULTICAST && channel->role == 
CHANNEL_CLIENT) { 
    status = Channel_openMulticastClient(channel);
  }
  else if (channel->type == CHANNEL_UDP) {
  }
  else {
  }

  return status;
}

Channel_RetCode Channel_send(Channel_Info *channel, void *data, int numBytes)
{
  Channel_RetCode status;
  int nBytes = numBytes;

  if (channel->type == CHANNEL_TCP) {
    status = write(channel->handle, &nBytes, sizeof(nBytes));
    CHANNEL_CHECKSTATUS(status < 0, "Cannot send length of packet");

    status = write(channel->handle, data, numBytes);
    CHANNEL_CHECKSTATUS(status < 0, "Channel TCP send");
  }
  else if (channel->type == CHANNEL_MULTICAST && channel->role == 
CHANNEL_SERVER) {
    status = sendto(channel->handle, data, numBytes, 0, (SA*) &channel->other, 
SOCK_LEN);
    CHANNEL_CHECKSTATUS(status < 0, "Channel UDP Multicast Send");
  }
  else {
    printf("Send not allowed for this channel\n");
    return CHANNEL_FAILURE;
  }

  return status;
}

Channel_RetCode Channel_recv(Channel_Info *channel, void *data, int maxBytes)
{
  Channel_RetCode status;
  int nBytes=0;

  if (channel->type == CHANNEL_TCP) {

    status = recv(channel->handle, &nBytes, sizeof(nBytes), MSG_PEEK);
    CHANNEL_CHECKSTATUS(status < 0, "Channel TCP receive");
    if (status == 0) {
      printf("Channel TCP closed\n");
      return CHANNEL_CLOSED;
    }

    if (nBytes > maxBytes) {
      printf("Size of packet[%d] in channel exceeds maxBytes[%d]\n", nBytes, 
maxBytes);
      return CHANNEL_INSUFFICIENT_BUFFER;
    }

    status = recv(channel->handle, &nBytes, sizeof(nBytes), 0);
    status = recv(channel->handle, data, nBytes, MSG_WAITALL);
    CHANNEL_CHECKSTATUS(status < 0, "Channel TCP receive");
    if (status == 0) {
      printf("Channel TCP closed\n");
      return CHANNEL_CLOSED;
    }
  }
  else if (channel->type == CHANNEL_MULTICAST && channel->role == 
CHANNEL_CLIENT) {
    status = recvfrom(channel->handle, data, maxBytes, 0, NULL, NULL);
    CHANNEL_CHECKSTATUS(status < 0, "Channel UDP Multicast receive");
    if (status == 0) {
      printf("Channel UDP Multicast closed\n");
      return CHANNEL_CLOSED;
    }
  }
  else
  {
    printf("Receive not allowed for this channel\n");
    return CHANNEL_FAILURE;
  }

  return status;
}

void Channel_close(Channel_Info *channel)
{
  shutdown(channel->handle, SHUT_RDWR);
  close(channel->handle);
  memset(channel, 0, sizeof(Channel_Info));
}

void Channel_setMyIp(Channel_Info *channel, char *ipaddr)
{
  inet_pton(AF_INET, ipaddr, &channel->me.sin_addr);
}

void Channel_setOtherIp(Channel_Info *channel, char *ipaddr)
{
  inet_pton(AF_INET, ipaddr, &channel->other.sin_addr);
}

void Channel_getMyIp(Channel_Info *channel, char *ipaddr)
{
  inet_ntop(AF_INET, &channel->me.sin_addr, ipaddr, MAX_IP_ADDR_LEN);
}

void Channel_getOtherIp(Channel_Info *channel, char *ipaddr)
{
  inet_ntop(AF_INET, &channel->other.sin_addr, ipaddr, MAX_IP_ADDR_LEN);
}

void Channel_setMyPort(Channel_Info *channel, unsigned short port)
{
  channel->me.sin_port = htons(port);
}

void Channel_setOtherPort(Channel_Info *channel, unsigned short port)
{
  channel->other.sin_port = htons(port);
}

unsigned short Channel_getMyPort(Channel_Info *channel)
{
  return ntohs(channel->me.sin_port);
}

unsigned short Channel_getOtherPort(Channel_Info *channel)
{
  return ntohs(channel->other.sin_port);
}
#ifndef _CHANNEL_H_
#define _CHANNEL_H_

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <errno.h>

#define MAX_IP_ADDR_LEN         (20)

#define CHANNEL_ANY_IF          ("0.0.0.0")

#define SOCK_LEN                (sizeof(struct sockaddr_in))

#define CHANNEL_CHECKSTATUS(COND, MSG)  { \
                                            if (COND) { \
                                              perror(MSG); \
                                              return CHANNEL_FAILURE; \
                                            } \
                                        } 

typedef int Channel_Handle;
typedef struct sockaddr_in Channel_Address;
typedef struct sockaddr SA;

typedef enum
{
  CHANNEL_TCP,
  CHANNEL_UDP,
  CHANNEL_MULTICAST
} Channel_Type;

typedef enum
{
  CHANNEL_INSUFFICIENT_BUFFER,
  CHANNEL_CLOSED,
  CHANNEL_FAILURE,
  CHANNEL_SUCCESS=0,
} Channel_RetCode;

typedef enum
{
  CHANNEL_SERVER=0,
  CHANNEL_CLIENT,
  CHANNEL_PEER
} Channel_Role;

typedef struct
{
  Channel_Handle handle;

  Channel_Type type;
  Channel_Role role;
  Channel_Address me;
  Channel_Address other;

} Channel_Info;

Channel_RetCode Channel_create(Channel_Info *channel);

Channel_RetCode Channel_open(Channel_Info *channel);

Channel_RetCode Channel_send(Channel_Info *channel, void *data, int numBytes);

Channel_RetCode Channel_recv(Channel_Info *channel, void *data, int maxBytes);

void Channel_close(Channel_Info *channel);

void Channel_getMyIp(Channel_Info *channel, char *ipaddr);

void Channel_getOtherIp(Channel_Info *channel, char *ipaddr);

void Channel_setMyIp(Channel_Info *channel, char *ipaddr);

void Channel_setOtherIp(Channel_Info *channel, char *ipaddr);

void Channel_setMyPort(Channel_Info *channel, unsigned short port);

void Channel_setOtherPort(Channel_Info *channel, unsigned short port);

unsigned short Channel_getMyPort(Channel_Info *channel);

unsigned short Channel_getOtherPort(Channel_Info *channel);

#endif
#include "channel.h"

int main(int argc, char *argv[])
{
  Channel_Info ch;
  char temp[256];

  memset(&ch, 0, sizeof(ch));
  memset(temp, 0, 256);

  ch.type = CHANNEL_TCP;
  ch.role = CHANNEL_CLIENT;
  Channel_create(&ch);

  Channel_setOtherIp(&ch, argv[1]);
  Channel_setOtherPort(&ch, atoi(argv[2]));
  Channel_open(&ch);

  Channel_recv(&ch, temp, 256);
  printf("Received : %s\n", temp);

  Channel_close(&ch);
}
_______________________________________________
Davinci-linux-open-source mailing list
[email protected]
http://linux.davincidsp.com/mailman/listinfo/davinci-linux-open-source

Reply via email to