Good morning,
I finished reading Beej’s network programming guide to refresh my memory on C networking concepts.
For the next part of the application that I’m building I want the user to be able to connect to their iPhone/iDevice via their web browser using a port, say 8080.
So for testing purposes I tried it on myself, but I get some unexpected issues. I bind a port to the kernel and wait for incoming connections. To simulate a remote user, I go to my web browser and type in localhost:8080
The NSLog and printf() messages I set up throughout the application show that I did connect and I even verified it by showing the IP address of the connecting user, but the client side of the application isn’t behaving as expected. I’m sending HTML to the client in my server code, but nothing shows up. Here’s the code I have so far
- (void) start
{
struct addrinfo hints ,*res;
struct sockaddr_storage connectionInfo;
socklen_t connectionSize;
memset(&hints,0,sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int status;
if((status = getaddrinfo(NULL,"8080",&hints,&res) )!= 0)
{
NSLog(@"Error inding getaddrinfo..");
printf("%s",gai_strerror(status));
return;
}
int sockFd = socket(res->ai_family,res->ai_socktype,res->ai_protocol);
int yes = 1;
setsockopt(sockFd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int));
if(sockFd < 0)
{
NSLog(@"Error on the socket() call");
printf("%s",gai_strerror(sockFd));
freeaddrinfo(res);
return;
}
NSLog(@"Made the socket connection!");
if(bind(sockFd , res->ai_addr, res->ai_addrlen) < 0)
{
NSLog(@"Bind error!");
freeaddrinfo(res);
return;
}
if(listen(sockFd,10) < 0)
{
NSLog(@"Unable to listen!");
freeaddrinfo(res);
return;
}
printf("Listening for a connection\n");
connectionSize = sizeof(connectionInfo);
int new_fd = accept(sockFd,(struct sockaddr*)&connectionInfo,&connectionSize);
const char* data = "<html><head><title> hi </title><body><h1> Test</h1>
</body></head></html>";
send(new_fd,data,strlen(data),0);
if(new_fd < 0)
{
NSLog(@"Cannot communicate with the new_fd");
freeaddrinfo(res);
printf("%s",gai_strerror(new_fd));
return;
}
NSLog(@"Communication with the new_fd!!!");
struct sockaddr_in whoAreThey;
socklen_t size = sizeof(whoAreThey);
getpeername(new_fd, (struct sockaddr*)&whoAreThey, &size);
char theirIp[16];
void* addrPtr = &(whoAreThey.sin_addr);
inet_ntop(AF_INET, addrPtr, theirIp, sizeof(theirIp));
NSLog(@"IP Address ");
printf("%s Connected\n",theirIp);
NSLog(@"The data were sent");
close(sockFd);
freeaddrinfo(res);
}
and inside my viewDidLoad method I call start as such:
- (void) viewDidLoad
{
[super viewDidLoad];
[self start];
}
Why isn’t the client ( web browser) displaying the HTML? Thanks
Note: I know the data IS Being sent because using telnet localhost 8080 feeds me the HTML
You’re not reading the browser’s request. The browser might not read your data unless you fully read the data it sent to you.
So a
recvloop (until there’s no more data from the browser) just before thesendshould fix your problem.EDIT: Final solution was that the response line was not sent to the browser. Offending line is:
and should be: