2 ** Copyright 2002, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
5 #include <sys/syscalls.h>
6 #include <newos/errors.h>
11 #include <socket/socket.h>
14 static sem_id exit_sem
;
23 static int read_thread(void *args
)
30 len
= socket_read(fd
, buf
, sizeof(buf
));
32 printf("\nsocket_read returns %ld\n", (long)len
);
40 _kern_sem_release(exit_sem
, 1);
45 static int write_thread(void *args
)
52 len
= socket_write(fd
, &c
, sizeof(c
));
54 printf("\nsocket_write returns %ld\n", len
);
59 _kern_sem_release(exit_sem
, 1);
69 fd
= socket_create(SOCK_PROTO_TCP
, 0);
70 printf("created socket, fd %d\n", fd
);
74 memset(&addr
, 0, sizeof(addr
));
76 addr
.addr
.type
= ADDR_TYPE_IP
;
78 // NETADDR_TO_IPV4(addr.addr) = IPV4_DOTADDR_TO_ADDR(63,203,215,73);
80 // NETADDR_TO_IPV4(addr.addr) = IPV4_DOTADDR_TO_ADDR(192,168,0,3);
82 NETADDR_TO_IPV4(addr
.addr
) = IPV4_DOTADDR_TO_ADDR(209,131,227,242);
84 err
= socket_connect(fd
, &addr
);
85 printf("socket_connect returns %d\n", err
);
89 exit_sem
= _kern_sem_create(0, "exit");
91 _kern_thread_resume_thread(_kern_thread_create_thread("read_thread", &read_thread
, NULL
));
92 _kern_thread_resume_thread(_kern_thread_create_thread("write_thread", &write_thread
, NULL
));
95 _kern_sem_acquire(exit_sem
, 1);
105 fd
= socket_create(SOCK_PROTO_TCP
, 0);
106 printf("created socket, fd %d\n", fd
);
110 memset(&addr
, 0, sizeof(addr
));
112 addr
.addr
.type
= ADDR_TYPE_IP
;
114 NETADDR_TO_IPV4(addr
.addr
) = IPV4_DOTADDR_TO_ADDR(63,203,215,73);
116 err
= socket_connect(fd
, &addr
);
117 printf("socket_connect returns %d\n", err
);
123 socket_write(fd
, buf
, sizeof(buf
));
133 fd
= socket_create(SOCK_PROTO_TCP
, 0);
134 printf("created socket, fd %d\n", fd
);
138 memset(&addr
, 0, sizeof(addr
));
140 addr
.addr
.type
= ADDR_TYPE_IP
;
142 NETADDR_TO_IPV4(addr
.addr
) = IPV4_DOTADDR_TO_ADDR(63,203,215,73);
144 err
= socket_connect(fd
, &addr
);
145 printf("socket_connect returns %d\n", err
);
153 len
= socket_read(fd
, buf
, sizeof(buf
));
154 printf("socket_read returns %ld\n", len
);
159 err
= socket_close(fd
);
160 printf("socket_close returns %d\n", err
);
170 fd
= socket_create(SOCK_PROTO_TCP
, 0);
171 printf("created socket, fd %d\n", fd
);
175 memset(&addr
, 0, sizeof(addr
));
177 addr
.addr
.type
= ADDR_TYPE_IP
;
179 NETADDR_TO_IPV4(addr
.addr
) = IPV4_DOTADDR_TO_ADDR(63,203,215,73);
181 err
= socket_connect(fd
, &addr
);
182 printf("socket_connect returns %d\n", err
);
188 err
= socket_close(fd
);
189 printf("socket_close returns %d\n", err
);
200 fd
= socket_create(SOCK_PROTO_TCP
, 0);
201 printf("created socket, fd %d\n", fd
);
205 memset(&addr
, 0, sizeof(addr
));
207 addr
.addr
.type
= ADDR_TYPE_IP
;
209 NETADDR_TO_IPV4(addr
.addr
) = IPV4_DOTADDR_TO_ADDR(0,0,0,0);
211 err
= socket_bind(fd
, &addr
);
212 printf("socket_bind returns %d\n", err
);
216 err
= socket_listen(fd
);
217 printf("socket_listen returns %d\n", err
);
221 new_fd
= socket_accept(fd
, &addr
);
222 printf("socket_accept returns %d\n", new_fd
);
226 err
= socket_write(new_fd
, "hello world!\n", strlen("hello world!\n"));
227 printf("socket_write returns %d\n", err
);
229 printf("sleeping for 5 seconds\n");
232 printf("closing fd %d\n", new_fd
);
233 socket_close(new_fd
);
234 printf("closing fd %d\n", fd
);
246 fd
= socket_create(SOCK_PROTO_TCP
, 0);
247 printf("created socket, fd %d\n", fd
);
251 memset(&addr
, 0, sizeof(addr
));
253 addr
.addr
.type
= ADDR_TYPE_IP
;
255 NETADDR_TO_IPV4(addr
.addr
) = IPV4_DOTADDR_TO_ADDR(0,0,0,0);
257 err
= socket_bind(fd
, &addr
);
258 printf("socket_bind returns %d\n", err
);
262 err
= socket_listen(fd
);
263 printf("socket_listen returns %d\n", err
);
268 int saved_stdin
, saved_stdout
, saved_stderr
;
271 new_fd
= socket_accept(fd
, &addr
);
272 printf("socket_accept returns %d\n", new_fd
);
276 saved_stdin
= dup(0);
277 saved_stdout
= dup(1);
278 saved_stderr
= dup(2);
286 argv
= "/boot/bin/shell";
287 err
= _kern_proc_create_proc("/boot/bin/shell", "shell", &argv
, 1, 5, 0);
289 dup2(saved_stdin
, 0);
290 dup2(saved_stdout
, 1);
291 dup2(saved_stderr
, 2);
295 printf("_kern_proc_create_proc returns %d\n", err
);
299 int main(int argc
, char **argv
)