Forum Discussion
URGENT : Nichestack TCP/IP in NIOS II
I am a beginner in this field. My project is to send and receive real time data from one system to another. A server/client program must run on NIOS II EDS on a startix board and a client/server program must run on a PC for the transfer to take place through an Ethernet cable. I have to use Nichestack TCP/IP and MicroC/OS-II.
The simple socket server example given in NIOS II EDS needs modification for this task to be done. I am unable to do it and need help badly. I have attached a server and client program that works fine on VC++ but on nios II eclipse it gives error. I have also attached the simple socket server example with all the functions. PLEASE GUIDE ME TO SOLVE IT AS I HAVE VERY LESS TIME FOR SUBMISSION !!!12 Replies
- Altera_Forum
Honored Contributor
Please stop posting the same question over and over again, it won't get you more answers. At least tell us what your problem is, we won't do your homework for you. "it gives error" is rather vague.
- Altera_Forum
Honored Contributor
The reason why i have posted the same question so many times is my project submission date getting close and i havnt been able to find any help in solving out the problem and i apologize for the same .
I am not worried about the socket program which i have attached (server and client program) since i have made successful connection between two PC'c running it on VC++. But as my project topic is " implementation of radar controller and display interface using nichestack and nios ii development system" . So according to my guide i have to make modification in the simple socket program. But the header file used in the server and client program shows error in Nios II EDS. As i am new to this so kinda worried without any help. Any help from your side will be appreciated. thank you - Altera_Forum
Honored Contributor
As Daixiwen wrote before we can't help you if you don't post the failure messages. Giving us the source code and not telling us what the problem really is doesn't help us to help you!
Urmel - Altera_Forum
Honored Contributor
if you expect help it would be really useful to post the error.
You expect other to help you but you don't even give them any information what is wrong. - Altera_Forum
Honored Contributor
Hi... I undertand you... I agree with Urmel and karsten. But, on the other hand, I have the same problem that you have. I'm developing a distributed data acquiring system, which uses a Ciclone 3 (NEEK) and have no ideia about how to perform a client socket, because the example shows how to do a server socket. If you get any results, shows me... ([email protected]). I already know how to draw into LCD, I may help you with that... (see my youtube video youtube.com/user/dmpeletronic#p/a/u/0/hDX___9-7II )
Sorry my bad english.. Danilo Pagano danilopagano.co - Altera_Forum
Honored Contributor
The bsd sockets (http://en.wikipedia.org/wiki/bsd_socket) wikipedia page shows code examples with both server and client sockets, using UDP or TCP.
- Altera_Forum
Honored Contributor
@dmpelectronic:
Writing a client socket is not so difficult. First of all you have to read a socket tutorial on the internet. The article that Daixiwen posted is definitely a good start for it. But there also some implementations here in the forum for a client socket. And you can also work with the client code djbmsce posted. There are probably only some header files he forgot to implement. The most important part/difference to server sockets are these lines of code
where you define the ip address you want to connect to and the port which is opened on the server (is this example port 55555). Urmel... clientService.sin_addr.s_addr = inet_addr("127.0.0.1"); clientService.sin_port = htons(55555); ... - Altera_Forum
Honored Contributor
Hello Daixiwen and Umel. I studied the BSD socket and thanks to your suggestion i managed to make my NIOS board as server. i Run the server program in NIOS II eclipse. I could successfully send data to the client with the following code.
The above code successfully send the string "This is a test string from server" shows the number of bytes sent/received. But i have to read data stored in a file. Please read my next post.I have mentioned my problem. Thank you.!!#include <stdio.h> # include <string.h> # include <ctype.h> /* MicroC/OS-II definitions */ # include "includes.h" /* Simple Socket Server definitions */ # include "simple_socket_server.h" # include "alt_error_handler.h" /* Nichestack definitions */ # include "ipport.h" # include "tcpport.h" # define MAXDATASIZE 200 void SSSSimpleSocketServerTask() { int sockfd, acceptsocket, addrlen; struct sockaddr_in server_addr, client_addr; addrlen = sizeof(client_addr); if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE," Socket creation failed"); } else { printf("Socket created"); } server_addr.sin_family = AF_INET; server_addr.sin_port = htons(SSS_PORT); server_addr.sin_addr.s_addr = INADDR_ANY; memset(&(server_addr.sin_zero), '\0', 8); if ((bind(sockfd,(struct sockaddr *)&server_addr,sizeof(server_addr))) < 0) { alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE," Bind failed"); } else { printf("Bind is OK\n"); } if ((listen(sockfd,5)) < 0) { alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE," Listen failed"); } else { printf("Listen is OK\n"); } while(1) { if ((acceptsocket = accept(sockfd,(struct sockaddr *)&client_addr,&addrlen)) < 0) { alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE,"accept failed"); } else { printf("accepted connection request\n"); } sockfd = acceptsocket; break; } int long bytes_sent; int long bytes_recv; char sendbuf = "This is a test string from server"; char recvbuf = ""; printf("Server: Sending some test data to client...\n"); bytes_sent = send(sockfd, sendbuf, strlen(sendbuf), 0); if((bytes_sent = send(acceptsocket,sendbuf,strlen(sendbuf),0)) < 0) { printf("SERVER : send failed.\n"); } else { printf("SERVER : Send is OK \n"); printf("SERVER : Bytes sent:%ld.\n",bytes_sent); } if((bytes_recv=recv(acceptsocket,recvbuf,200,0))<0) { printf("SERVER : receive failed.\n"); } else { printf("SERVER: recv() is OK.\n"); printf("SERVER: Received data is: \"%s\"\n", recvbuf); printf("SERVER: Bytes received: %ld.\n", bytes_recv); } return ; } - Altera_Forum
Honored Contributor
My project is to send real time data captured by radar to the client. The data is saved in a .txt file which may contain both characters and integers. I have added the following code for file open and read on SERVER side.
for CLIENT side the received string will be saved in the receive buffer before it is written in to another file:char sendbuf; char recvbuf = ""; fp = fopen("/mnt/host/test.txt", "r"); if(fp==NULL) { printf("cannot open file\n"); } else { fgets(sendbuf,199,fp); } bytes_sent = send(sockfd, sendbuf, strlen(sendbuf), 0); if((bytes_sent = send(acceptsocket,sendbuf,strlen(sendbuf),0)) < 0) { printf("SERVER : send failed.\n"); } else { printf("SERVER : Send is OK \n"); printf("SERVER : Bytes sent:%ld.\n",bytes_sent); } if((bytes_recv=recv(acceptsocket,recvbuf,200,0))<0) { printf("SERVER : receive failed.\n"); } else { printf("SERVER: recv() is OK.\n"); printf("SERVER: Received data is: \"%s\"\n", recvbuf); printf("SERVER: Bytes received: %ld.\n", bytes_recv); } fclose(fp);
The file read and write code works fine when i run both server and client on VC++. But when i run the server on NIOS II board it doesnt do anything afterFILE *fp; fp = fopen ("c:\\test1.txt","w"); if(fp==NULL) { printf("cannot open file\n"); } else { fputs(recvbuf,fp); } fclose(fp);
sometimes it just writes some junk data to a file. I dont know what the problem is. Is there any problem in my code NIOS can not read the file. I have placed the .txt file in the folder which have my c code for server according to Host file system. I have enabled the altera_hostfs in the BSP editor. Please assist.Socket created Bind is OK Listen is OK accepted connection request - Altera_Forum
Honored Contributor
Probably a silly point, but the hostfs filesystem only works when "debugging" the code, not just executing it. The filesystem, itself, relies on the gdb debug channel to connect to the host.
A quick and dirty way to place a "file" on any embedded system is to just compile it into your code as a char array. Regards, slacker