Forum Discussion
Altera_Forum
Honored Contributor
15 years agoHello 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.
#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 ;
} 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.!!