Forums » Software Development »
How to specify port in client/server application
Added by Mary Frantz almost 12 years ago
Hello,
I am running Linux on the ARM and DSP/BIOS on the DSP. I need to implement a client and server on a particular port on the ARM side. I am using MDK_2012-08-10.
How do you specify the port to listen on or connect to? There doesn't seem to be a field for that in the socket address structure (sockaddr). Am I using the correct include file?
How do you set sockaddr so the server will connect to any address?
This runs in its own thread. Here's the code for the server so far:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include "../host.h"
bool gQuitHost = true;
sockaddr sockAddr;
void * HOST_commandServer(void *pArg)
{
bool done;
sockaddr conAddress;
size_t len;
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.sa_family = AF_INET;
sockAddr.sa_data = INADDR_ANY; // this gives a compiler error "INADDR_ANY was not declared in this scope"
printf("Host command start.\n");
while(!gQuitHost)
{
int sFd = socket(AF_INET, SOCK_STREAM, 0);
bind(sFd, &sockAddr, sizeof(sockAddr));
listen(sFd, 1);
done = false;
while(!done)
{
int cFd;
cFd = accept(sFd, &conAddress, &len);
// read ... write ...etc until done
}
shutdown(sFd, SHUT_RDWR);
close(sFd);
printf("Host command loop.\n");
sleep(1);
}
return NULL;
}
Replies (4)
RE: How to specify port in client/server application - Added by Tim Iskander almost 12 years ago
here's a pretty decent tutorial on linux sockets...
http://www.linuxhowtos.org/C_C++/socket.htm
RE: How to specify port in client/server application - Added by Mary Frantz almost 12 years ago
Yes, I was just reading that. They use slightly different elements:
struct sockaddr_in serv_addr, cli_addr;
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
I have seen this elsewhere as well.
sockaddr_in is not recognized by the compiler and neither is INADDR_ANY
RE: How to specify port in client/server application - Added by Michael Williamson almost 12 years ago
Hi Mary,
sockaddr_in is most certainly supported by the compiler. We use it all the time. The man page suggests you need a couple of additional headers, from the man page you need:
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
-Mike
RE: How to specify port in client/server application - Added by Mary Frantz almost 12 years ago
I knew there was a simple answer. Thanks!
Here's the updated code, which compiles.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include "../inc/host.h" // my local include file
bool gQuitHost = true;
char buffer256;
void * HOST_commandServer(void *pArg)
{
bool done;
struct sockaddr_in servAddress;
struct sockaddr_in clientAddress;
int sFd, cFd;
size_t len;
int n;
// Server Address
memset(&servAddress, 0, sizeof(servAddress));
servAddress.sin_family = AF_INET;
servAddress.sin_addr.s_addr = INADDR_ANY;
servAddress.sin_port = htons(PORT_COMMAND); // PORT_COMMAND is defined in host.h
printf("Host command start.\n");
while(!gQuitHost)
{
// Create socket, bind, listen
sFd = socket(AF_INET, SOCK_STREAM, 0);
if (sFd < 0)
printf("Error opening socket.\n");
if (bind(sFd, (struct sockaddr *) &servAddress, sizeof(servAddress)) < 0)
printf("Error binding to socket.\n");
listen(sFd, 1);
done = false;
while(!done)
{
// Accept connection client
len = sizeof(clientAddress);
cFd = accept(sFd, (struct sockaddr *)&clientAddress, &len);
if (cFd < 0)
printf("Error on accept.\n");
bzero(buffer,256);
n = read(cFd,buffer,255);
if (n < 0)
printf("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(cFd,"I got your message",18);
if (n < 0)
printf("ERROR writing to socket");
}
shutdown(cFd, SHUT_RDWR);
close(cFd);
shutdown(sFd, SHUT_RDWR);
close(sFd);
printf("Host command loop.\n");
sleep(1);
}
return NULL;
}