1 /* src/native/vm/cldc1.1/com_sun_cldc_io_j2me_socket_Protocol.c
3 Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: java_lang_VMRuntime.c 5900 2006-11-04 17:30:44Z michi $
35 #include <sys/types.h>
36 #include <sys/socket.h>
40 #include "mm/memory.h"
42 #include "native/jni.h"
44 #include "vm/global.h"
45 #include "vm/vm.h" /* REMOVE ME: temporarily */
49 * Class: com/sun/cldc/io/j2me/socket/Protocol
53 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_open0(JNIEnv *env, jclass clazz, java_bytearray *hostname, s4 port, s4 mode)
55 struct hostent *phostent;
56 struct sockaddr_in serv_addr;
61 /* The hostname byte-array is a NULL terminated C-string. */
63 name = (char *) &(hostname->data);
67 phostent = gethostbyname(name);
72 /* fill the sockaddr structure */
74 serv_addr.sin_family = AF_INET;
75 serv_addr.sin_port = htons(port);
77 MCOPY(&serv_addr.sin_addr, phostent->h_addr, u1, phostent->h_length);
79 /* create the socket */
81 sockfd = socket(AF_INET, SOCK_STREAM, 0);
86 /* connect the socket */
88 result = connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
98 * Class: com/sun/cldc/io/j2me/socket/Protocol
100 * Signature: (I[BII)I
102 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf(JNIEnv *env, jclass clazz, s4 handle, java_bytearray *b, s4 off, s4 len)
107 /* get pointer to the buffer */
109 buf = &(b->data[off]);
111 /* receive from the socket */
113 result = recv(handle, buf, len, 0);
116 /* the peer has performed an orderly shutdown */
120 else if (result < 0) {
121 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_readBuf: recv failed: %s", strerror(errno));
129 * Class: com/sun/cldc/io/j2me/socket/Protocol
133 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_readByte(JNIEnv *env, jclass clazz, s4 handle) {
138 /* receive from the socket */
140 result = recv(handle, &byte, 1, 0);
143 /* the peer has performed an orderly shutdown */
147 else if (result < 0) {
148 /* should throw an IOException */
150 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_readByte: recv failed: %s", strerror(errno));
158 * Class: com/sun/cldc/io/j2me/socket/Protocol
160 * Signature: (I[BII)I
162 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf(JNIEnv *env, jclass clazz, s4 handle, java_bytearray* b, s4 off, s4 len) {
167 /* get pointer to the buffer */
169 buf = &(b->data[off]);
171 /* send the given byte to the socket */
173 result = send(handle, buf, len, 0);
176 /* should throw an IOException */
178 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_writeBuf: send failed: %s", strerror(errno));
186 * Class: com/sun/cldc/io/j2me/socket/Protocol
190 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte(JNIEnv *env, jclass clazz, s4 handle, s4 b)
197 /* send the given byte to the socket */
199 result = send(handle, &byte, 1, 0);
202 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_writeByte: send failed: %s", strerror(errno));
209 * Class: com/sun/cldc/io/j2me/socket/Protocol
213 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_available0(JNIEnv *env, jclass clazz, s4 handle)
215 /* NOTE: Sun doesn't have an implementation too */
222 * Class: com/sun/cldc/io/j2me/socket/Protocol
226 JNIEXPORT void JNICALL Java_com_sun_cldc_io_j2me_socket_Protocol_close0(JNIEnv *env, jclass clazz, s4 handle)
230 /* close the file descriptor */
232 result = close(handle);
235 vm_abort("Java_com_sun_cldc_io_j2me_socket_Protocol_close0: close failed: %s", strerror(errno));
240 * These are local overrides for various environment variables in Emacs.
241 * Please do not remove this and leave it at the end of the file, where
242 * Emacs will automagically detect them.
243 * ---------------------------------------------------------------------
246 * indent-tabs-mode: t
250 * vim:noexpandtab:sw=4:ts=4: