Altera_Forum
Honored Contributor
15 years agoproblem with re-building TCP/IP connection on Nios
Dear all,
Currently in my design I want to make a function to change the Nios' ip address. It is almost completed, but there is still a problem. So here are the progress : 1. I can change the Nios ip address successfully. It has been proven by ping function from other devices / PC to the Nios' new ip address. 2. The Nios can initialize a new socket using its new ip address. There is no error in TCP/IP socket initiation (create socket, bind, listen). In the new TCP/IP connection, I have to make Nios use different port, otherwise it will be error (I'm not sure what the reason is). The problem is.. when I try to establish a TCP/IP connection from my PC, somehow the Nios can't accept any request from other devices. It just keep listening and my pc seems that it couldn't find the Nios socket with its new ip address. Here is my code :
void SocketComTask()
{
int fd_listen, max_socket, new_fd_listen;
struct sockaddr_in addr;
static SCConn conn;
fd_set readfds;
AccData acc;
NetInfo ip_data;
while(1)
{
if(ip_data.ip_chg==1)
{
change_ip(ip_data); // function to change ip address
}
// socket creation
// ------------------------------------------------
if((fd_listen = socket(AF_INET, SOCK_STREAM, 0)) < 0) // socket function returns a descriptor referencing the new socket
{
alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE," Socket creation failed");
}
// bind
// ------------------------------------------------
addr.sin_family = AF_INET;
if(ip_data.ip_chg == 0) addr.sin_port = htons(SC_PORT);
else addr.sin_port = htons(SC_PORT2);
addr.sin_addr.s_addr = INADDR_ANY;
if ((bind(fd_listen,(struct sockaddr *)&addr,sizeof(addr)))<0)
{
alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE," Bind failed");
}
// listen
// ------------------------------------------------
if((listen(fd_listen,1))<0)
{
alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE," Listen failed");
}
sc_reset_conn(&conn);
if(ip_data.ip_chg == 0) printf(" Socket Com listening on port %d\n", SC_PORT);
else
{
printf(" Socket Com listening on port %d\n", SC_PORT2);
ip_data.ip_chg = 0;
}
while(1)
{
FD_ZERO(&readfds); // FD_ZERO(*set) : initializes the set to the null set
FD_SET (fd_listen, &readfds); // FD_SET(s,*set) : add descriptor s to set
max_socket = fd_listen+1;
if (conn.fd != 1)
{
FD_SET(conn.fd, &readfds);
if(max_socket <= conn.fd)
{
max_socket = conn.fd+1;
}
}
select(max_socket, &readfds, NULL, NULL, NULL);
if(FD_ISSET(fd_listen, &readfds)) // FD_ISSET(s,*set) : nonzero if s is a member of the set, otherwise zero
{
acc = sc_handle_accept(fd_listen, &conn);
}
else
{
if((conn.fd != -1) && FD_ISSET(conn.fd, &readfds) && acc.index==0)
{
sc_handle_receive(&conn);
}
else if((conn.fd != -1) && FD_ISSET(conn.fd, &readfds) && acc.index==1)
{
ip_data = sc_handle_receive_ctrl(&fd_listen, &conn, acc.client_ip);
if(ip_data.ip_chg==1) // check indicator whether there is ip change command or not
{
conn.close = 1;
close(conn.fd);
close(fd_listen);
break;
}
}
}
}
}
}
int change_ip (NetInfo new_ip)
{
NET p_net;
ip_addr ipaddr, netmask, gw;
int iface = 0;
p_net = nets; // for multi network interface, check file alt_iniche_dev.c
// Get the new ip address
printf(" Get the new ip address\n");
if(new_ip.dhcp_actv)
{
p_net -> n_flags |= NF_DHCPC;
dhc_state_init(iface, FALSE); // put DHCP client in INIT reboot
printf(" DHCP IP has been assigned\n");
}
else
{
p_net -> n_flags &= ~NF_DHCPC;
dhc_set_state(iface, 0); // put DHCP client in DHCS_UNUSED state
IP4_ADDR( ipaddr, new_ip.ipaddr_in, new_ip.ipaddr_in, new_ip.ipaddr_in, new_ip.ipaddr_in );
IP4_ADDR( netmask, new_ip.netmask_in, new_ip.netmask_in, new_ip.netmask_in, new_ip.netmask_in );
IP4_ADDR( gw, new_ip.gw_in, new_ip.gw_in, new_ip.gw_in, new_ip.gw_in );
p_net -> n_ipaddr = ipaddr;
p_net -> snmask = netmask;
p_net -> n_defgw = gw;
printf(" Static IP has been assigned\n");
}
int e = dhc_second();
if(e) printf("DHCP returned error %i.\n", e);
return 0;
}
Does anyone know what's wrong with my program? Anybody know how to completely close socket and flush its configuration? I'm wondering that the problem is related with select function, somehow Nios maybe can't find its socket that is ready for reading/writing...:confused: Or if you have any suggestion for my problem, please kindly to tell me :(