[Prev][Next][Index][Thread]
Help with BSD socket interface
Hi,
I am trying to make a simple test kernel with oskit. I got a basic
"hello world" kernel to work. Now I am trying to test network
functionality. The attached source is loosely based on the hello.c and
socket_bsd.c examples. It is designed to open a TCP connection to the
local host and read from it indefinately, printing out what is read to
the "console." When I run it, I get the following:
> NetTest Version 0.1
> Built Using OSKit/UNIX Version 20020317 (compiled Oct 22 2002)
> Hostname: node0.localdomain
> Connected... (3)
> error: 107
> Unknown error: 0x6b: Socket not connected
In other words, socket() and connect() appear to succeed, but read()
does not. Am I doing something stupid here, or attempting something
that is not implemented (or not fully implemented) in oskit? Any ideas
what's going on?
Note that when I use the same program, but with oskit stuff commented
out and linked normally, it works as expected.
BTW, I'm using the Unix User-Mode Environment if that makes a difference.
Thanks for your input,
--Nathan Davis
#include <stdio.h>
#include <oskit/clientos.h>
#include <oskit/startup.h>
#include <oskit/version.h>
//#include <oskit/net/freebsd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
int init_connection(char *host, int port);
int main()
{
char localhost[256];
int fd;
#ifndef KNIT
oskit_clientos_init();
start_clock();
//start_fs_bmod();
start_network_native();
#endif
#ifdef GPROF
start_fs_bmod();
start_gprof();
#endif
printf("NetTest Version 0.1\n");
printf("Built Using ");
oskit_print_version();
gethostname(localhost, sizeof(localhost));
printf("Hostname: %s\n", localhost);
fd = init_connection("127.0.0.1", 8080);
if(fd == -1){
perror("Error connecting...");
}
printf("Connected... (%d)\n", fd);
/* write(fd, "help\r\n", 6); */
while(1){
char buffer[1025];
int bytes_read = read(fd, buffer, 1024);
if(bytes_read == -1){
printf("error: %d\n", errno);
perror(strerror(errno));
break;
}
if(bytes_read > 0){
buffer[bytes_read] = '\0';
printf(buffer);
}
}
return 0;
}
int init_connection(char *host, int port){
int so;
int namelen, retval, msg_len;
struct sockaddr_in addr;
if((so = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1){
perror("socket");
return -1;
}
if(inet_addr(host) == -1){
printf("Invalid host\n");
}
/* fill in the sin_addr structure */
memset(&addr, 0, namelen = sizeof(addr));
addr.sin_addr.s_addr = inet_addr(host);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if(connect(so, (struct sockaddr*)&addr, sizeof(addr)) == -1){
perror("connect");
return -1;
}
return so;
}