Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12748 Discussions

Problem with a small network program

Altera_Forum
Honored Contributor II
1,185 Views

Dear everyone: 

I'm learning uClinux network programming. After download the kernel and filesystem to the board successfully, Now I am trying to run a small server and client program, but after running the programs, there is no react at all! 

I can't find where the problem is...Can anyone help me?Thanks. 

The program is: 

 

**************** server program:********************** 

int main(int argc,char *argv[]) 

{  

int listensock,connsock;  

struct sockaddr_in serveraddr;  

const char buff[] = "Hello! Welcome here!\r\n";  

printf("enter!\n"); 

listensock = socket(AF_INET,SOCK_STREAM,0);  

bzero(&serveraddr,sizeof(struct sockaddr_in));  

serveraddr.sin_family = AF_INET;  

serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);  

serveraddr.sin_port = htons(5000);  

bind(listensock,(struct sockaddr_in *)&serveraddr,sizeof(serveraddr));  

listen(listensock,1024);  

connsock = accept(listensock,(struct sockaddr_in *)NULL, NULL);  

send(connsock,buff,sizeof(buff), 0);  

close(connsock);  

close(listensock);  

************************************************ 

 

**************** client program:********************** 

int main(int argc,char *argv[])  

int sockfd;  

char recvbuff[100]={0};  

struct sockaddr_in serveraddr;  

 

if(argc!=2){ 

printf("Usage: echo ip address"); 

exit(0); 

sockfd = socket(AF_INET,SOCK_STREAM,0);  

bzero(&serveraddr,sizeof(struct sockaddr_in)); 

serveraddr.sin_family=AF_INET; 

serveraddr.sin_port=htons(5000); 

inet_pton(AF_INET,argv[1],&(serveraddr.sin_addr)); 

connect(sockfd,((struct sockaddr_in*)&serveraddr),sizeof(serveraddr)); 

recv(sockfd,recvbuff,sizeof(recvbuff),0); 

printf("%s\n" , recvbuff); 

close(sockfd);  

}  

************************************************ 

 

-------------------------------------------------------------------------- 

some clue: 

1,I am not sure whether my run parameter is right, I run the program as below(in the command line): 

server program: main 

client program: main ×××.×××.×.××(The server's IP address) 

 

2, I am not sure whether the assigned port(serveraddr.sin_port = htons(5000); listensock,1024); ) is right. 

 

3, I just run the program after download the kernel and filesystem into the board without any modification. Is the driver for the network is necessary? My uClinux edtion is Microtronix Nios II Linux Distribution V1.4, and my board is Cyclone II 2C35. 

 

4, There are some errors in the program.. 

-------------------------------------------------------------------------- 

 

Maybe some of my question is stupid, this is my first step to learn uClinux programing and I'm so confused, wish you can help me, thanks again!
0 Kudos
2 Replies
Altera_Forum
Honored Contributor II
472 Views

you should check the return values of each function. 

 

try 

serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); 

 

without htonl just INADDR_ANY. 

 

 

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

Is the driver for the network is necessary[/b] 

--- Quote End ---  

 

 

 

drivers are necessary
0 Kudos
Altera_Forum
Honored Contributor II
472 Views

 

--- Quote Start ---  

originally posted by helmchen@Jan 18 2007, 05:33 PM 

you should check the return values of each function. 

 

try 

serveraddr.sin_addr.s_addr = htonl(inaddr_any); 

 

without htonl just inaddr_any. 

 

 

<div class='quotetop'>quote </div> 

--- quote start ---  

is the driver for the network is necessary 

--- Quote End ---  

 

 

 

drivers are necessary 

<div align='right'><{post_snapback}> (index.php?act=findpost&pid=20682)</div> 

[/b] 

--- Quote End ---  

 

 

Helmchen,Thank you very much!!
0 Kudos
Reply